restjenkinsjenkins-job-builder

Create Jenkins Job remotely with REST API


I can't create Job remotely (REST API + Postman), I try

POST:

http://localhost:8080/createItem?name=NEWJOB

OR

http://localhost:8080/job/NEWJOB/build

and always have 403 error(GET methods work).

Here I see that I need to create config.xml for POST :

https://issues.jenkins-ci.org/browse/JENKINS-9410?focusedCommentId=204821&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-204821

I don't understand how to create this config? Do I need this config? How to create jenkins Job remotely?


Solution

  • It takes me several steps:

    1) GET

    http://localhost:8080/crumbIssuer/api/json 
    

    +header Authorization ..................... Basic YWRtaW46YWRtaW4=

    Value of Authorization field was created when I put in login and password(admin,admin in my case).

    After this query I saw next:

    {
        "_class": "hudson.security.csrf.DefaultCrumbIssuer",
        "crumb": "2ad62df85d23d4c65a25c0b33fb0ef61",
        "crumbRequestField": "Jenkins-Crumb"
    }
    

    2)Next step -> create config.xml

    I just take it from another jenkins job (named test) manually when I :

    GET http://localhost:8080/job/test/config.xml

    +header header Authorization .............. Basic YWRtaW46YWRtaW4=

    3)Create job:

    POST http://10.14.48.107:8080/createItem?name=NEWJOBNAME

    +header Authorization ........ Basic YWRtaW46YWRtaW4=

    +header Content-Type ........... text/xml

    +header Jenkins-Crumb ......... 2ad62df85d23d4c65a25c0b33fb0ef61

    I take this header name and value from 1st query crumb

    +Body->raw->

    <project>
    <actions/>
    <description/>
    <keepDependencies>false</keepDependencies>
    <properties>
    <com.coravy.hudson.plugins.github.GithubProjectProperty plugin="github@1.29.0">
    <displayName/>
    </com.coravy.hudson.plugins.github.GithubProjectProperty>
    </properties>
    <scm class="hudson.scm.NullSCM"/>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers/>
    <concurrentBuild>false</concurrentBuild>
    <builders/>
    <publishers/>
    <buildWrappers/>
    </project>
    

    After this step I have had nothing as answer, but job was created remotely!