JSON support for Jenkins createItem / create job

2,516 views
Skip to first unread message

Sajith Kariyawasam

unread,
Oct 9, 2014, 1:42:44 PM10/9/14
to jenkins...@googlegroups.com


Hi all,

I was trying to use Jenkins REST apis, and have a requirement to create items / jobs via REST api, but I prefer JSON over XML.

When I browse http://localhost:8080/api/ I can find following description

Create Job

To create a new job, post config.xml to this URL with query parameter name=JOBNAME. You need to send a Content-Type: application/xml header. You will get a 200 status code if the creation is successful, or 4xx/5xx code if it fails. config.xml is the format Jenkins uses to store the project in the file system, so you can see examples of them in the Jenkins home directory, or by retrieving the XML configuration of existing jobs from /job/JOBNAME/config.xml.

According to that, the input parameters for  /createItem need to be passed in an "xml" file. Is there anyway I can avoid an xml file but to use a "JSON" for that? Does Jenkins createItem / createJob API supports JSON inputs?


Thanks,

Sajith

Daniel Beck

unread,
Oct 9, 2014, 1:55:10 PM10/9/14
to jenkins...@googlegroups.com
> Is there anyway I can avoid an xml file but to use a "JSON" for that? Does Jenkins createItem / createJob API supports JSON inputs?


No. It's less of an XML based API, rather you're passing in the job's new config.xml, which just happens to be XML. Likewise, the /job/jobname/config.xml URL only serves XML.

Sajith Kariyawasam

unread,
Oct 9, 2014, 10:39:10 PM10/9/14
to jenkins...@googlegroups.com, m...@beckweb.net
Thanks Daniel for the quick response. 

According to https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API seems the  /job/JOB_NAME/build is a JSON based API. 
I was wondering why there is no JSON based API for creating a job.? For consistency I would expect a JSON based API. 
Any plans to implement such in a future release?

Naga Rakesh

unread,
Jul 24, 2015, 12:00:29 PM7/24/15
to Jenkins Users, m...@beckweb.net, saj...@gmail.com
Hello Sajith,

I was wondering if you are able to find the solution for this. 

I am trying to create new item/job through jenkins rest api but for the api call i need to upload a config.xml. I want to know if I can instead send json or upload a json 
based file as config in place of config.xml

Do you have any thoughts or recommend ways to get the json based config for jenkins job? 

Any help would be appreciated.

Thanks,
Rakesh

Khai Do

unread,
Jul 27, 2015, 12:20:30 PM7/27/15
to Jenkins Users, m...@beckweb.net, saj...@gmail.com, nagar...@gmail.com
We use the jenkins job builder (https://pypi.python.org/pypi/jenkins-job-builder) to manage jenkins jobs.  It supports yaml and can parse json as well so you can create your jobs in either and have it create the jobs for you.  Most of the examples in documentation are in yaml but json will work as well.

Here's an example:

create this simple.json file:
[
  {
    "job": {
      "name": "simple",
      "builders": [
        {
          "shell": "echo This is my job"
        }
      ]
    }
  }
]

run jenkins-job-builder to test the:  'jenkins-jobs test simple.json'
run update to upload the job to jenkins: 'jenkins-jobs --conf jenkins_master.ini update simple.json'

Hope this helps.
Reply all
Reply to author
Forward
0 new messages