How to get build number of lastbuild-1 in Jenkins via API?

74 Views Asked by At

trying to read Jenkins job status from RestAPIs. Lastbuild giving latest info. In Grafana I am trying to compare LastBuild & LastBuild-1 status. Anyway to get the lastbuild-1 info?

http://127.0.0.1:8080/job/Job2/***lastBuild***/api/json --> giving LastBuild info http://127.0.0.1:8080/job/Job2/***lastBuild-1***/api/json --> anyway in this format?

2

There are 2 best solutions below

0
ycr On

I don't think you can do this with a single API call. So you need to do two API calls for this, one to get the last build number and then to get lastBuild-1

Following API cal will give you the last build number.

http://JENKINS_HOST/job/Job1/lastBuild/buildNumber 
0
CodeMan On

Here is the process to get information on the .lastbuild via Jenkins API:

  1. Retrieve a list of all Jenkins jobs
https://jenkins_url/api/json
  1. Retrieve data for a specific job
https://jenkins_url/job/job_name/api/json
  1. Fetch build data for the build type '.lastbuild' using its build number
https://jenkins_url/job/job_name/build_number/api/json