Skip to content

Instantly share code, notes, and snippets.

@janpapenbrock
Last active July 27, 2021 14:45
Show Gist options
  • Save janpapenbrock/084cb9c94af5f2759062b0494430dbbb to your computer and use it in GitHub Desktop.
Save janpapenbrock/084cb9c94af5f2759062b0494430dbbb to your computer and use it in GitHub Desktop.
Trigger Jenkins job from Bash script, works with multi branch pipeline
#!/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