Create jobs and execute them in jenkins using REST

前端 未结 5 1077
借酒劲吻你
借酒劲吻你 2021-01-30 23:17

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

相关标签:
5条回答
  • 2021-01-30 23:54

    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
    
    0 讨论(0)
  • 2021-01-30 23:55

    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.

    0 讨论(0)
  • 2021-01-31 00:04

    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"
    
    0 讨论(0)
  • 2021-01-31 00:10

    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"
    
    0 讨论(0)
  • 2021-01-31 00:11

    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"
    
    0 讨论(0)
提交回复
热议问题