Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create jobs and execute them in jenkins using REST

Tags:

I am trying to create a WCF REST client that will communicate to Jenkins and create a job from an XML file and then build the job. My understanding is that you can do that with Jenkins.

Can some one please provide some commands that you can type on a browser's address bar to create and build jobs? ie: http:localhost/jenkins/createItem?name=TESTJOB something along those lines.

like image 626
David Avatar asked Apr 09 '13 18:04

David


People also ask

Does Jenkins have a REST API?

It is up to the user to parse the JSON and extract the relevant pieces. Interestingly, Jenkins itself has a REST API, so a Pipeline DSL could use the http request plugin to talk to the master Jenkins instance from an agent. But the user still to parse the response.

How do I access REST API Jenkins?

Jenkins has a link to their REST API in the bottom right of each page. This link appears on every page of Jenkins and points you to an API output for the exact page you are browsing. That should provide some understanding into how to build the API URls.


2 Answers

Usually, when parsing through the documentation, it can take one or two days. It is helpful to be able to access code or curl commands to get you up and running in one hour. That is my objective with a lot of third party software.

See the post at http://scottizu.wordpress.com/2014/04/30/getting-started-with-the-jenkins-api/ which lists several of the curl commands. You will have to replace my.jenkins.com (ie JENKINS_HOST) with the your own url.

To create a job, for instance, try:

curl -X POST -H "Content-Type:application/xml" -d "<project><builders/><publishers/><buildWrappers/></project>" "http://JENKINS_HOST/createItem?name=AA_TEST_JOB2" 

This uses a generic config. You can also download a config from a manually created job and then use that as a template.

curl "http://JENKINS_HOST/job/MY_JOB_NAME/config.xml" > config.xml curl -X POST -H "Content-Type:application/xml" -d @config.xml "http://JENKINS_HOST/createItem?name=AA_TEST_JOB3"  

To execute the job (and set string parameters), use:

curl "http://JENKINS_HOST/job/MY_JOB_NAME/build" curl "http://JENKINS_HOST/job/MY_JOB_NAME/buildWithParameters?PARAMETER0=VALUE0&PARAMETER1=VALUE1" 
like image 58
Scott Izu Avatar answered Oct 20 '22 14:10

Scott Izu


See the Jenkins API Wiki page (including the comments at the end). You can fill in the gaps using the documentation provided by Jenkins itself; for example, http://JENKINS_HOST/api will give you the URL for creating a job and http://JENKINS_HOST/job/JOBNAME/api will give you the URL to trigger a build.

I highly recommend avoiding the custom creation of job configuration XML files and looking at something like the Job DSL plugin instead. This gives you a nice Groovy-based DSL to create jobs programmatically - much more concise and less error-prone.

like image 28
gareth_bowles Avatar answered Oct 20 '22 16:10

gareth_bowles