Jenkins API to handle Jobs Remotely

jenkins
Reading Time: 4 minutes

Hello Readers! I hope you are doing well. So, In this blog we will see how we can use Jenkins API to handle jobs remotely. In this blog we will create, update, trigger, delete, our Jenkins job through REST API. This is very quick and simple so let’s see.

Why do we need to handle a job remotely?

Jenkins is well known for its flexibility. It supports a wide range of configurations through its plugins. In some cases where multiple actions need to be triggered via jenkins. The situation comes where we have to create hundreds of pipelines So, we cannot do everything through its UI. We cannot perform everything manually as it will take a lot of time. Therefore, we need automation here. Here we use an API which helps us in communicating to Jenkins.

So, Let’s Get Started!

Prerequisites:

  1. Jenkins should be installed in your system. If not then you can follow the following steps from this blog: https://blog.knoldus.com/jenkins-installation-and-creation-of-freestyle-project/
  2. We must have an authentication token created in Jenkins which plays a very important role in managing jenkins remotely. If you want to know the steps for creating it then let’s see.

 Create an authentication token in Jenkins:

To create an authentication token is quite simple you only need to follow these steps:

  1. Login to Jenkins. Click on your username and Click on Configure.
jenkins api
  1. Clickon “Add new Token” button.
  2. Add the token name and click on the “Generate”.
jenkins api
  1. Copy the token name for further use. 

So, it’s created now. Let’s use this token for handling jenkins jobs remotely.

Create Pipeline using API:

For creating a pipeline using API in jenkins, we need a config.xml file in our local. So, for this we have to create a job in jenkins by using its UI. And from this job I will download the config.xml file for further usage. 

Therefore I will now create a simple pipeline in jenkins.

create pipeline

For downloading its config.xml file use the following command:

$ curl -X GET http://localhost:8080/job/<job_name>/config.xml -u <user_name>:

<api_token> -o jenkinsconfig.xml 

For me the command is:

$ curl -X GET http://localhost:8080/job/pipeline/config.xml -u 

admin:11d216387debb402b06d5731406c8f2d93 -o jenkinsconfig.xml

  

create job

The file jenkinsconfig.xml is downloaded now. So, let’s use this file in creating a new Jenkins job. Use the following command for creating a new job via API:

$ curl -s -XPOST 'http://localhost:8080/createItem?name=<new_job_name>' -u 

<user_name>:<api_token> --data-binary @jenkinsconfig.xml -H "Content-

Type:text/xml"
$ curl -s -XPOST 'http://localhost:8080/createItem?name=job1' -u 

admin:11d216387debb402b06d5731406c8f2d93 --data-binary @jenkinsconfig.xml -H 

"Content-Type:text/xml"
create job

My new jenkins job is created now you can see below:

jenkins job

Trigger job using API:

We mostly use the API for triggering our jenkins job. So, let’s add a parameter in the pipeline. 

jenkins job

Now we will trigger it via api using the below command:

$ curl http://<user_name>:

<api_token>@localhost:8080/job/<job_name>/buildWithParameters  --data 

name=value

For me the command is:

$ curl 

http://admin:11d216387debb402b06d5731406c8f2d93@localhost:8080/job/pipeline/b

uildWithParameters --data name=naincy

You can see that my job is triggered:

jenkins job

Updating job using api:

For updating the pipeline we have to update the config.xml file. The new_config.xml is the name of new config file. Use this command for updating it:

$ curl -X POST http://<user_name>:<api 

token>@localhost:8080/job/<job_name>/config.xml --header "Content-

Type:text/xml" --data-binary @new_config.xml

Deleting job using api:

Deleting a job via api is so easy. Use the following command for deleting a pipeline:

$  curl -X POST http://<user_name>:

<api_token>@localhost:8080/job/<job_name>/doDelete 

After adding values my command is:

$ curl -X POST 

http://admin:11d216387debb402b06d5731406c8f2d93@localhost:8080/job/job1/doDel

ete 

My job1 jenkins pipeline is deleted now. 

We are all done! 

Conclusion

Thank you for sticking to the end. In this blog we have seen how easily we can manage jenkins remotely through api. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.

HAPPY LEARNING!

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.