Jenkins API Integration : Trigger Job Remotely via Jenkins API
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.
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 :
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¶meter=VALUE
For Example :
[code language=”objc”]
http://jenkins.example.com/view/testing/job/sample_selenium_test/buildWithParameters?token=YOUR_TOKEN&parameter1=VALUE1&parameter2=VALUE2[/code]
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)
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. 🙂
Hi, I am trying to trigger my jenkins job remotely. The problem is my jenkins server is sso enabled. When I do the curl post request to the job with toker, it get redirected to the sso page and I end up getting redirect 302 error. Any help here pls?
thanks for you article
Hey nice article,
I want to create Jenkins jobs which will execute my application API, is that possible
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”
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 ?
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.
Can you please let me know the version of Jenkins you are using to make it work? I am using Jenkins 2.71.
I am having issues as it intermittently fails with
Caused: java.io.IOException: Failed to persist config.xml
After restarting Jenkins service it works for some time
and again fails.