Triggering Jenkins Build Remotely Using API
Any Jenkins Job can be triggered remotely through an API call. This article outlines how to do exactly that in just four easy steps.
Join the DZone community and get the full member experience.
Join For FreeAny Jenkins Job can be triggered remotely through an API call.
We can extract the API output either in XML(SOAP) or in JSON(REST) format.
STEP 1: Get the Authorization Token
1.Click on the Configure option under your username
2.Under API token Click on the Show Legacy API Token. See images below.
3.Copy the token and use it in the place of a password
STEP 2: Trigger the Build
API: https://JENKINS_URL/job_path/buildwithParemters?
Method: POST
Parameters: Username= “username”
Password= “Token”
OUTPUT:
Return the header with the queue number of the job.
The above two steps are used to trigger a Jenkins Parameterized build remotely. In order to get the status of the build, you must follow the three steps below. Jenkins build, once triggered, will have the below flow:
Job Triggered->Waits on Queue(Queue Number)->assign node for the build -> gets a Build Number
STEP 3: Get the Build Number
Pass the Location URL from the previous API output header to get the Build Number.
API: Locationurl/api/json?
Eg: https://jenkins.xxx.com/jenkins/queue/item/1691997/api/json
Method: GET
Parameters: Username= “username”
Password= “Token”
OUTPUT:
Returns the Build Number and the URL of the job
STEP 4: Get the Build Status
Pass the Build URL from the previous API output to get the Build Status.
API: BuildUrl/api/json?
Eg: https://jenkins.test.com/jenkins/job/test/job/access/63/api/json
Method: GET
Parameters: Username= “username”
Password= “Token”
OUTPUT:
Returns the Build Status of the job.
Opinions expressed by DZone contributors are their own.
Comments