Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a JSON object back from a Jenkins build request

Similar to how http://localhost/jenkins/job/job_name/25/api/json would return a JSON object with the details of build 26, is there a way to get a similar object when first initiating the job, i.e., before you know what the build number is?

I noticed the output from a curl post request to the build url returns html that includes a build number; however, I would prefer not to have to parse this in favor of having a JSON object with the build number in it. Currently, I am using:

curl -v --data "param1=value&param2=value" \
http://localhost/jenkins/job/job_name/buildWithParameters

which initiates the job fine and outputs a bunch of html. Is there a way to start this job and receive a JSON object with the build number?

like image 985
markj Avatar asked Jan 14 '23 14:01

markj


2 Answers

The nextBuildNumber may not be the correct build number in all cases. If you have triggered two different builds of the same Job, we don't know which one got triggered first. There is a race condition here. Checking the build queue may not give the correct build number either.

like image 69
arangamani Avatar answered Jan 20 '23 15:01

arangamani


If you query http://localhost/jenkins/job/job_name/api/json you can fetch the nextBuildNumber field anytime that will give you the next build number.

When you trigger a build, you can rest assured the build will get exactly this number.

like image 30
favoretti Avatar answered Jan 20 '23 17:01

favoretti