1

I wanted to create a jenkins job which can list me all jenkins job with it is count of number of builds it has executed. Jenkins API or Groovy may work. Below API provide me list of all build ids for particular job but I'm looking for count for build executed for that job. http://[jenkins_server]/job/[jobname]/api/xml?tree=builds[id]

Also I'm refering this : https://stackoverflow.com/a/28039134/12897205

Let me know if I can go for any plugin

Jay2201
  • 21
  • 1
  • 7

2 Answers2

0

Using a build step in your pipeline.

To make it simple, I would simply put a simple bash in a sh step and get the number of builds for each jobs. Also please note that this will work only if you don't set a custom build number. :)

To get the list of jobs,

curl -s -g http://<jenkins-url>/api/json\?tree\=jobs\[name\] | jq -r '.jobs[].name' | awk -v ORS=, '{print $1}' | sed 's/,$/\n/'

To get the list of all builds associated pass the values from above command to the below for loop's jobname arguments.

for i in {jobname1,jobname2}; do count=$(curl -s http://<jenkins-url>/job/$i/api/json\?tree\=nextBuildNumber | jq .nextBuildNumber-1) && echo "\nJobName: $i \nNumber of builds:$count"; done;

EDIT-1 Copy the below script and call it in your Jenkinsfile. You can pass the number of jobs to keep as an argument. Change the build/job url as required within the job or just pass that as a parameter too.

#!/bin/bash

jobstokeep=$1

listofbuilds=$(curl -sg http://jenkins-url/job/jobName/api/json?tree=builds[number] | jq .[] | grep 'number' | sed 's/[^0-9]//g' | awk -v var=$jobstokeep 'NR>var' | awk -v RS='' -v OFS=',' '$1=$1')

echo $listofbuilds

for i in ${listofbuilds//,/ }; do curl -s $user:$pass -X POST http://jenkins-url/job/jobName/$i/doDelete && echo "Build $i is deleted"; done;

The above script will 1st print the number of jobs to delete as a comma separated value and then loop thru each job and delete them using curl command (Jenkins Rest API).

vijay v
  • 1,888
  • 1
  • 12
  • 19
  • Thanks vijay, Can fetch latest and oldest build number for a job out of this? Actually I want to create a job which should keep latest 10 or 20 latest builds and delete rest other old builds with visiting each job manually. – Jay2201 Apr 01 '20 at 08:08
  • If you don't mind me asking, Why do you want to create a new job for deleting old builds. Jenkins has a build discarder plugin that let's you keep builds only upto particular number of days or only keep a particular number of builds. Like last 20 builds, 5 builds or so. Please check the documentation if that's what you were looking for. [Discard Old Build](https://plugins.jenkins.io/discard-old-build/). – vijay v Apr 01 '20 at 08:41
  • Yes but it was not in place earlier. Now I have to clean old builds – Jay2201 Apr 01 '20 at 12:42
  • I'll post an EDIT to the existing answer in sometime. You can check that. Btw, I'm not sure if there are groovy scripts available to do this. But it can be done in bash with it's utilities. – vijay v Apr 01 '20 at 13:40
  • Thanks Vijay Appreciated..!! Great help from you! – Jay2201 Apr 02 '20 at 05:50
  • Added the EDIT-1. @Jay2201 The script might look a bit clumsy but it does the job. – vijay v Apr 05 '20 at 03:41
0

You can try global-build-stats plugin provided by Jenkins. It provides graphical representation for the total number of builds executed in each pipeline with more details like execution time, status, executor and more. You can also filter by adding start and end date.

https://plugins.jenkins.io/global-build-stats/

Rajeswari
  • 71
  • 2
  • 11