Last active
July 27, 2021 14:45
-
-
Save janpapenbrock/084cb9c94af5f2759062b0494430dbbb to your computer and use it in GitHub Desktop.
Trigger Jenkins job from Bash script, works with multi branch pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
## Configuration | |
JENKINS="https://my-jenkins.localhost" | |
JENKINS_USER="user_name_here" | |
JENKINS_USER_TOKEN="api_token_here" | |
# nested path to job | |
JOB="AcmeCorp/job/acmecorp.com/job/master" | |
## Script | |
JENKINS_AUTH="$JENKINS_USER:$JENKINS_USER_TOKEN" | |
CRUMB=$(curl --user "$JENKINS_AUTH" "$JENKINS"'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') | |
CONTAINS_ERROR=$(echo "$CRUMB" | grep "<html>" || true) | |
if [ -n "$CONTAINS_ERROR" ]; then | |
echo "$CRUMB" | |
exit 1 | |
fi | |
curl -X POST \ | |
"$JENKINS"/job/"$JOB"/build \ | |
--user "$JENKINS_AUTH" \ | |
-H "$CRUMB" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment