Get Jenkins Job Build ID from Queue ID

Viewed 29042

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?

4 Answers

Astonishing that Jenkins doesn't track a request to its conclusion with a unique ID.

One element that persists between the queued item and the build item is the list of parameters. If you have the ability/authority to change the config of the jobs, then add an optional parameter REQUEST_ID that the client plucks from the air (e.g. a UUID) and passes in over REST. Then you can quiz the queue and look for the one with your REQUEST_ID, and you can quiz the list of builds and look for the one with your REQUEST_ID.

There's an interesting thing about this that if you want to queue multiple builds in the loop, you might end up getting SAME QUEUE_ID, This happens especially when you have the same parameter and queued builds have not started up. Basically, Jenkins assumes that you are just trying to build this and instead of multiple builds it just assumes that it needs to spin up only a single build, hence the SAME QUEUE_ID, To overcome this, if you add an extra parameter while building the job like UQ_ID=CURRENT_TIMESTAMP (This param doesn't need to be in build) then you will get the unique build number

If there's no param at all in the build you should define at least one param, Because parameterless build won't allow ANY parameter to be passed in the api

Related