Trigger Jenkins Parameterised Job Via HTTP Request

jenkins logo
Reading Time: 4 minutes

Hi Folks! In this blog we will see how we can easily trigger Jenkins Parameterised job Via HTTP request. Jenkins is one of the oldest & most popular CI (Continuous Integration) tools, and many teams still configure and run their daily and ad-hoc build jobs with Jenkins.

Why do we need to trigger a job remotely?

Many times we want to trigger the build from outside, without manually interacting with the web interface. The most common use cases are triggering a build from a scheduler or another automated job. Here in this blog we’ll see how we can trigger a parameterised job on a remote Jenkins server.

Create an authentication token in Jenkins:

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

  • Go to username and then to configure as you can see in below image.

After that in API token section click on Add Token and generate a new token and copy that token for further use.This token can not be recovered again so copy that token and save it to somewhere else.

Configure a Parameterised job to trigger from remote in Jenkins

While configuring your job for triggering parameterised job you have to select check box This project is parameterised section. After that click on Add Parameter . As you can see on above image.

As you can see in above image there are different type of parameter you can choose several type of parameter as per your requirement.Here I am choosing two parameter of String type .

Now give name of parameter I have given first as name and the default value of this parameter is my name Jubair Ahmad . The second parameter name I have given as Email and the default value of this parameter is my email jubair.ahmed@knoldus.com.One thing to note here that these value can be override and when I will trigger this job through http request i will change this value.

Now go to Build Triggers section and select Trigger builds remotely (e.g., from scripts) and paste the Token we generated earlier.

After all I am printing those values in Build execute shell as you can see in below image.

Trigger the Jenkins parameterised job via HTTP request

With this information, you can now create a URL that looks like this:

http://admin:1125c0f4ba507a56920fb8e5c9cb2ed3ed@localhost:8080/job/My_job/buildWithParameters?name=jubair&Email=ahmadjubair878@gmail.com

Note that “JobName” should be replaced with the job name you’re triggering. The correctly escaped version of this URL is shown when you set the authentication token.

In order to trigger the job, you must send a POST request. You can test it out using cURL:

curl -X POST "http://admin:1125c0f4ba507a56920fb8e5c9cb2ed3ed@localhost:8080/job/My_job/buildWithParameters?name='jubair'&Email=ahmadjubair878@gmail.com

You can see that after running curl command your job will be triggered and also you can see in above image for the same.

As I already said that these parameter values can be override So you can see in output section the default value of the parameter has been changed.

Refrences:

https://www.jenkins.io/doc/book/using/remote-access-api/

Conclusion
In this blog we have seen how in a few easy steps we can trigger a jenkins parameterised job remotely with an HTTP request. Thanks for being with me till the end. If you find this blog helpful do share with your friends.Thank You!

knoldus

Written by 

Jubair Ahmad is a Software Consultant (DevOps)at Knoldus.Inc.He loves learning new technology and also have interest in playing cricket.