Jenkins API Integration : Trigger Job Remotely via Jenkins API

04 / Nov / 2015 by Aditya Thakur 3 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 :

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 :

(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

comments (3)

  1. Shan

    thanks, it is working :)… Is it possible to display the authenticated username, who triggered the job? Currently it is just displaying “started by remote host 90.XX.XX.XX”

    Reply
  2. Gopalakrishnan

    Can I access the parameter and values inside the build-job for a job triggered in this fashion using REST API ?would you have any examples ?

    Reply
    1. Aditya ThakurAditya Thakur

      Yes, you should be able to do that, I don’t have any example ready but if you are interested I can look for something.

      Reply

Leave a comment -