Jenkins API Integration : Trigger Job Remotely via Jenkins API

04 / Nov / 2015 by Aditya Thakur 0 comments

Jenkins provide continuous integration to software development. Jenkins job builds can be started by various methods, like manual triggering, scheduled cron-like mechanism, dependency builds, and by Jenkins API calls.

This blog will cover Jenkins API Integration : Trigger Job Remotely via Jenkins API.

1.  First, we need Jenkins API TOKEN which will be ‘configure’ tab on Jenkins.

d

2.  Generate the URL for Jenkins Job.

Go to Jenkins website and add “api/json” to any page. If we get a JSON response on the browser, then that page can be accessed remotely. For converting it to readable format, use “api/json?pretty=true”

jenkins_url/some_page/api/json

For example :
http://jenkins.example.com/view/testing/job/sample_selenium_test/api/json?pretty=true

b

3. Make ‘HTTP POST’ request to Jenkins server.

NOTE: Jenkins only supports ‘HTTP POST’ request for remote requests.

‘HTTP POST’ request can be of two types.

(A) Without parameters : 

URL FORMAT -> jenkins_url/job/job_name/build?token=TOKEN.

For example :
http://jenkins.example.com/view/testing/job/sample_selenium_test/build?token=YOUR_TOKEN

(B) Parameterized :

URL FORMAT -> jenkins_url/job/job_name/buildWithParameters?token=TOKEN&parameter=VALUE

For Example :


http://jenkins.example.com/view/testing/job/sample_selenium_test/buildWithParameters?token=YOUR_TOKEN&parameter1=VALUE1&parameter2=VALUE2

After this, we also need to add {key:value} pair authorization to the ‘HTTP POST’ request. It will be in format
{auth: “jenkins_user_email:jenkins_user_password”}.

Final request will look like..

Curl Request : 

curl -v -X POST jenkins_url/job/job_name/buildWithParameters
--data token=your_token
--user jenkins_user_email:jenkins_user_password
--data-urlencode json= '{"parameter1":"value1", "parameter2":"value2"}'

Finally, we will get the response of ‘HTTP POST’ request with property ‘statusCode’ (201 for success, else for errors)

c

We have successfully triggered Jenkins job remotely. You may want to check it on Jenkins now.
We also track the Job Status and Console remotely using Jenkins API which will be covered in my next blog.
Stay tuned. :)

FOUND THIS USEFUL? SHARE IT

Leave a comment -