Is there any simple way to work with APIs or with scripting to get list of all builds performed on all jobs for last one year along with the user who triggered the build as a report?
Asked
Active
Viewed 2,893 times
1 Answers
5
This should do. Run from <JENKINS_URL>/script
or in a Jenkins job with an "Execute System Groovy Script" (not an "Execute Groovy script").
Updated: to include details from the subject line.
def jobNamePattern ='.*' // adjust to folder/job regex as needed
def daysBack = 365 // adjust to how many days back to report on
def timeToDays = 24*60*60*1000 // converts msec to days
println "Job Name: ( # builds: last ${daysBack} days / overall ) Last Status\n Number | Trigger | Status | Date | Duration\n"
Jenkins.instance.allItems.findAll() {
it instanceof Job && it.fullName.matches(jobNamePattern)
}.each { job ->
builds = job.getBuilds().byTimestamp(System.currentTimeMillis() - daysBack*timeToDays, System.currentTimeMillis())
println job.fullName + ' ( ' + builds.size() + ' / ' + job.builds.size() + ' ) ' + job.getLastBuild()?.result
// individual build details
builds.each { build ->
println ' ' + build.number + ' | ' + build.getCauses()[0].getShortDescription() + ' | ' + build.result + ' | ' + build.getTimestampString2() + ' | ' + build.getDurationString()
}
}
return
Sample Output
ITSuppt/sampleApplication ( 4 / 11 ) SUCCESS 13 | Started by user Ian W | SUCCESS | 2020-10-22T01:57:58Z | 30 sec 12 | Started by user Ian W | FAILURE | 2020-10-22T01:51:36Z | 45 sec 11 | Started by user Ian W | SUCCESS | 2020-10-15T18:26:22Z | 29 sec 10 | Started by user Ian W | FAILURE | 2020-10-15T18:14:13Z | 55 sec
It could take a long time if you have a lot of jobs and builds, so you might want to restrict to skip the details to start or use a job pattern name. Build Javadoc for additional info.
Or, according to this S/O answer, you can Get build details for all builds of all jobs from Jenkins REST API (additional examples elsewhere).

Ian W
- 4,559
- 2
- 18
- 37
-
This Groovy solution is the best way I think -- using the REST API is much less reliable and it will takes ages. Ian W's script can be improved by dropping all builds that do not fit the desired time frame in the first place -- just use `job.getBuilds().byTimestamp(start,end)` instead of `job.builds`. – Alex O Oct 24 '20 at 13:58
-
Thx @alex-o - overlooked the subject in answering the Q. Updated. – Ian W Oct 25 '20 at 07:09
-
build.result is the status. I was looking in the api for that for ages. – Peter Moore Oct 03 '21 at 19:21
-
@IanW How can I get details for all jobs I want to print only job name and its duration , I tried several API and groovy method but didnt get any luck, can you please help – Samurai Aug 01 '22 at 13:25
-
@Samurai, the information you are looking for is in my answer ( `job.fullName +build.getDurationString()` ); just drop the unwanted fields. – Ian W Aug 01 '22 at 21:08