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

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


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.


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.


As soon as the build has been triggered, you can get its URL back from:

http://localhost/jenkins/job/job_name/api/json?tree=lastBuild[url]

This will return the running build if there is one, or the latest completed build otherwise. You can then add "/api/json" to that URL to get your build's JSON object.