25

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
David
  • 617
  • 1
  • 6
  • 15

6 Answers6

26

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"
Scott Izu
  • 2,229
  • 25
  • 12
15

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.

gareth_bowles
  • 20,760
  • 5
  • 52
  • 82
  • 4
    Thanks, it took me a bit but I was finally able to figure it out. I just wish their API wiki page was more informative and with better examples. – David Apr 13 '13 at 04:39
4

Thanks to a GIST - https://gist.github.com/stuart-warren/7786892

Check if job exists

curl -XGET 'http://jenkins/checkJobName?value=yourJobFolderName' --user user.name:YourAPIToken

With folder plugin

curl -s -XPOST 'http://jenkins/job/FolderName/createItem?name=yourJobName' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken

Without folder plugin

curl -s -XPOST 'http://jenkins/createItem?name=yourJobName' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken

Create folder

curl -XPOST 'http://jenkins/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder&from=&json=%7B%22name%22%3A%22FolderName%22%2C%22mode%22%3A%22com.cloudbees.hudson.plugins.folder.Folder%22%2C%22from%22%3A%22%22%2C%22Submit%22%3A%22OK%22%7D&Submit=OK' --user user.name:YourAPIToken -H "Content-Type:application/x-www-form-urlencoded"
suryakrupa
  • 3,852
  • 1
  • 25
  • 34
2

If you want to create a job into a view given the view exists.

curl -X POST -H "Content-Type:application/xml" -d @build.xml "http://jenkins_host/view/viewName/createItem?name=itemName"

the build.xml filetemplate could be found in the root directory of a job's workspace

if you want to create a view:

curl  -X POST -H "Content-Type:application/xml"  -d @view.xml "http://jenkins_host/createView?name=viewName"

the content of the file view.xml could be:

<?xml version="1.0" encoding="UTF-8"?>
<hudson.model.ListView>
    <name>viewName</name>
    <filterExecutors>false</filterExecutors>
    <filterQueue>false</filterQueue>
    <properties class="hudson.model.View$PropertyList"/>
    <jobNames>
        <comparator class="hudson.util.CaseInsensitiveComparator"/>
    </jobNames>
    <jobFilters/>
    <columns>
        <hudson.views.StatusColumn/>
        <hudson.views.WeatherColumn/>
        <hudson.views.JobColumn/>
        <hudson.views.LastSuccessColumn/>
        <hudson.views.LastFailureColumn/>
        <hudson.views.LastDurationColumn/>
        <hudson.views.BuildButtonColumn/>
    </columns>
</hudson.model.ListView>

and to check if a view exists:

curl -X POST -H "Content-Type:application/xml" "http://jenkins_host/checkViewName?value=viewName"

to check if a job exists:

curl -X POST -H "Content-Type:application/xml" "http://jenkins_host/checkJobName?value=jobName"
Lucas Liu
  • 823
  • 1
  • 10
  • 12
0

To create a job:

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

To build a job:

curl -X POST -u username:API_TOKEN http://JENKINS_HOST/job/MY_JOB_NAME/build
RobC
  • 22,977
  • 20
  • 73
  • 80
0

In case you need to make the same HTTP calls using the Python requests library, instead of CURL...

Download a job config:

import requests
auth = ("username", "api_token")
url = "http://" + JENKINS_HOST + "/job/" + JOB_NAME + "/config.xml"
response = requests.get(url, auth=auth)
open('config.xml', 'wt').write(response.text)

Create a new job using same config:

url = "http://" + JENKINS_HOST + "/createItem?name=" + NEW_JOB_NAME
headers = {'content-type': 'text/xml'}
data = response.text
response = requests.post(url, auth=auth, headers=headers, data=data)  

Omit auth parameter when not needed.

jrbe228
  • 356
  • 3
  • 11