Search code examples
jenkinsjenkins-pluginsjenkins-clijenkins-scriptler

How do I get a list of jobs with longest build time in Jenkins


I need to generate a weekly report on our Jenkins build cluster. One of the reports is to display a list of jobs that have the longest build time.

The solution I can come up with is to parse the "Build history" page on each slave (also master) and for each build of a job, parse the build page and look for "Took x min x sec on slave-xx".

This feels quite cumbersome, does anyone know a better solution using Jenkins API or Groovy script console?

Thanks


Solution

  • You can get the build data for your report through the Jenkins API. For a given job, you can retrieve the list of builds with duration information using something like:

    http://jenkins:8080/job/my-job/api/json?tree=builds[id,number,duration,timestamp,builtOn]

    To see a list of all the API-available build data for a given job:

    http://jenkins:8080/job/my-job/api/json?tree=builds[*]

    Once you have a query that retrieves the job information that you need for your report, it should be straightforward to loop over the jobs.

    Most Jenkins pages have a link at the bottom to the REST API that describes a bit about accessing the API for that page, e.g. http://jenkins:8080/job/my-job/api.