Skip to content

Instantly share code, notes, and snippets.

@Peelz
Forked from vishnuhd/readme.md
Last active April 16, 2021 07:33
Show Gist options
  • Save Peelz/cce873a163fbfa9d9b984a39a8b1dce8 to your computer and use it in GitHub Desktop.
Save Peelz/cce873a163fbfa9d9b984a39a8b1dce8 to your computer and use it in GitHub Desktop.
Create Jenkins pipeline using REST api and CURL

Jenkins REST scripts

  • Set variable
export JENKINS_URL=

export COOKIES=
  • Get the config.xml from a pre-created job or create another one according to the need :
curl -X GET "$JENKINS_URL/job/test-pipeline/config.xml" -u admin:admin -o config.xml
  • Generate Crumb :
CRUMB=$(curl -s "$JENKINS_URL/crumbIssuer/api/json" --cookie "$COOKIES" | python3 -c "import sys, json;i=json.load(sys.stdin);print(i['crumbRequestField']+':'+i['crumb'])")

echo $CRUMB # display value
#output: Jenkins-Crumb:d11aea06df13cdeb17ab7a12f2832eae
  • Use xml to create another jenkins job :
curl -X POST "$JENKINS_URL/createItem?name=$jobName" --cookie "$COOKIES" --data-binary @config.xml -H "$CRUMB" -H "Content-Type:text/xml"
  • Or, use xml to update exists jenkins job :
curl -X POST "$JENKINS_URL/$jobName" --cookie "$COOKIES" --data-binary @config.xml -H "$CRUMB" -H "Content-Type:text/xml"
  • Reference :

https://support.cloudbees.com/hc/en-us/articles/220857567-How-to-create-a-job-using-the-REST-API-and-cURL-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment