Monday, October 10, 2022

[SOLVED] Get Jenkins Job Build ID from Queue ID

Issue

I'm successfully able to use this to kick off a Jenkins Job:

curl -X POST "http://jenkins_srv:8080/job/MY_JOB/buildwithParameters?this=1&that=2" --user name:pass

I can also get the consoleText from this job using:

curl -X POST "http://jenkins_srv:8080/job/MY_JOB/lastBuild/consoleText"

However, this doesn't scale if I run multiple jobs back to back. I've noticed that the first curl command has a return that includes:

Location: http://jenkins_srv:8080/queue/item/123/

I'm assuming that 123 is the job queue id.

My question is, if I queue jobs 121, 122, & 123 back to back, what do I use to check the status of job queue item 122? Also, what do I use to determine the actual build id that eventually resulted from job queue item 122?


Solution

Assuming that you have a location of http://jenkins_srv:8080/queue/item/123/, you can GET http://jenkins_srv:8080/queue/item/123/api/json?pretty=true to return information about that queue item (you can also not include ?pretty=true if you don't care about formatting, or use api/xml if you want the results in XML).

I don't know if there's standard documentation on the queue API, but it appears that if the queue item has completed (maybe also if it's currently being built?) it will have an executable node. One for my server looked like this:

"executable" : {
    "_class" : "org.jenkinsci.plugins.workflow.job.WorkflowRun",
    "number" : 10,
    "url" : "http://192.168.99.100:32769/job/configure/10/"
}

You can then GET the API for the URL specified in executable.url. In my case, GET http://192.168.99.100:32769/job/configure/10/api/json?pretty=true. From there, you should have all the information you need, including whether or not the build is currently being built, how long the build took/has taken, the result, etc.

Also, if you want information on your entire build queue, you can GET http://jenkins_srv:8080/queue/api/json?pretty=true



Answered By - Kdawg
Answer Checked By - David Marino (WPSolving Volunteer)