Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get build details for all builds of all jobs from Jenkins REST API

Tags:

java

rest

jenkins

I have a hosted Jenkins server and from there, I am trying to fetch the build details (like result, timestamp, duration etc.) for all the jobs using the Jenkins REST API and then save it in my database.

Right now, I am calling the following API from my Java code, to fetch all the jobs (about 200 jobs):

https://<JENKINS_HOST>/api/json

Then I fetch the job details and all the builds for each job by using:

https://<JENKINS_HOST>/job/MY_JOB/api/json

Then finally, for each of the builds (I have to fetch only last 50), I have to call this to fetch the build details:

https://<JENKINS_HOST>/job/MY_JOB/<BUILD_NUMBER>/api/json

So that makes it about a total of 50*200 + 201 = over 10000 API calls.

I am guessing, These many API calls would make the Jenkins server perform slow?

So, my question is is there a faster/more optimal way to do this, So that I do not have to make so many API calls?

Something like where I can fetch all the build details using one url like this: (hypothetically)

https://<JENKINS_HOST>/job/MY_JOB/api/json?fetchAllbuildDetails=True

like image 476
Sumit Avatar asked Jan 09 '19 23:01

Sumit


Video Answer


1 Answers

In case, anyone else is stuck with the same, I was able to get this done using the tree:

https://<JENKINS_HOST>/api/json?tree=jobs[name,url,builds[number,result,duration,url]]

Here, column names can be filtered to fetch only the data you need as the amount of data returned from this is huge.

You can also limit the number of records to be fetched like this:

https://<JENKINS_HOST>/api/json?tree=jobs[name,url,builds[number,result,duration,url]{0,50}]

This will fetch only the last 50 builds for all the jobs, which is exactly what I needed.

like image 104
Sumit Avatar answered Oct 03 '22 16:10

Sumit