I want to extract the job ID assigned from the build history, similar to how it’s displayed in the Jenkins GUI. Specifically, after triggering a build, I need to find and display the assigned job ID.
I get the output after using curl command. At the output Location similar to my jobid but when im trying to connect http below it gives me error.
job id almost similar as you can see
I have managed to extract the job ID with a bash script, but is there any other option instead of using bash? Is there any way to see the output job ID directly?
But this bash script just change the query id to job id so if query and jobid is not same it is not working.
I need to get the Build Number for me to query the status.
When you trigger a build via rest api you get in the response header a new location that points to the queue, e.g.
$ curl -X POST http://localhost:9090/job/testpipeline/build -u user:token
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0HTTP/1.1 201 Created
Date: Tue, 04 Jun 2024 09:01:04 GMT
X-Content-Type-Options: nosniff
Location: http://localhost:9090/queue/item/90/
Content-Length: 0
Server: Jetty(10.0.20)
You can now take the location and ask it’s rest API with http://localhost:9090/queue/item/90/api/json
as long as the build is in the queue you will get "blocked": true in the json answer. Once the build has started you get an executable in the json with number and url
You will need to poll the rest api until the build has started to get the build number. A few minutes after the build started the information about the build will be deleted from the queue and querying the api will return a 404