My Profile Photo

Ionut Gavrilut

DevOps Enthusiast | Linux System Administrator | Certified Jenkins Engineer

Create Jenkins Jobs with REST API and Postman

When you have two Jenkins instances and want to copy a job from one to the other, there are multiple ways you can do this. One of it is to create a config.xml file in a new directory that will be your new job. Put the config.xml that you want to want (copied from the other job) and restart the Jenkins.

But I don’t want to restart the Jenkins Master

You can create Jenkins jobs without restart, with:

  • Jenkins REST API
  • jenkins-cli

In the next steps, we will see how we can create bulk jobs with Postman (you can replace this with classic curl and integrate it in a script).

Jenkins REST API

Jenkins provide an API useful for retrieve and update information. You can see it by navigating in Jenkins console and adding /api in URL. There is a documentation about this.

To retrieve information about this, you can add in URL:

  • /api/json?pretty
  • /api/xml

So we assume that you have the perfect job that you are using every time and you don’t want to create it manually for new projects.

In this case, all you have to do is to have a config.xml (that job configuration that you use), and sending two requests to the new Jenkins where you want to create this job.

Request the crumb using crumbIssuer

Jenkins require (if you have this feature enabled, which you should) Jenkins crumb header in the request, because of Cross-Site Request Forgery (CSRF). So first of all, you need to issue this crumb.

In Postman,

  • Select GET request
  • Write your own Jenkins endpoint of the crumbIssuer:
  • Add Authorization with your credentials
  • Send (Returned status should be 200 OK)

This will return something like this:

"_class": "",
"crumb": "abcdefg",
"crumbRequestField": "Jenkins-Crumb"
  • Save the crumb for next step

Create a Jenkins job using your config.xml file

Now that you have the crumb, you are ready to deploy your new job configuration.

In Postman,

  • Select POST request
  • Write your own Jenkins endpoint:

This will create a job on root directory. If you want to create this job in a folder, you can use /job/myfolder/createItem?name=myjob

  • Add Authorization with your credentials
  • Add these headers:

Key: Content-Type

Value: text/xml

Key: Jenkins-Crumb

Value: abcdefg (from previous step)

  • Select Body and add raw text with your config.xml
  • Send (Returned status should be 200 OK)