How to find last 5 jobs run on Jenkins Agent

Hello,

I’m trying to list out last 5 jobs run on each Jenkins agent, i used Build History option from agent details but it’s takes some time to load all details due to which i’m not able to export the metrics from Jenkins to Prometheus.

Do we have any alternative to display last 5 jobs run on agents ?

The build history of an agent that you access with the Build History link is a UI thing only. And it is limited as it is not able to list executions of pipeline jobs.
In order to get the history of an agent this will loop over all builds of all jobs and checks if the built was run on that agent so this is an expensive operation and the page will always list all jobs that have run. For pipeline jobs this information is not directly accessible as it is hidden deep in the flow information so this page is not able to find those builds.
The plugin Pipeline Agent Build History solves this by adding an additional link for agents that is able to extract that information from pipeline jobs (and it also shows freestyle jobs). It caches the builds and uses pagination which makes it pretty fast.
But the plugin has no rest api that will return e.g. some json with the last runs

Hello Markus,

Unfortunately, we have only freestyles jobs not a pipeline jobs. Still can i use Pipeline Agent Build History for freestyle jobs ? Mainly i was looking for rest api but seems that it’s not available to use.

Thanks a lot
-Pradip

yes the plugin also shows freestyle jobs.
There might be better solutions to keep track which jobs run on an agent for external monitoring systems, e.g. pushing this to elasticsearch, though it might require writing a new plugin (not sure if there is already a plugin for this)

if you want last 5 built jobs, try:

jenkins.model.Jenkins.instance.getAllItems( hudson.model.FreeStyleProject.class )
       .findAll { it.lastBuild }
       .collectEntries{[ (it.lastBuild.startTimeInMillis): it.lastBuild ]}
       .sort{ a, b -> a.key <=> b.key }
       .collect { it.value }.reverse().take(5)
       .collect { it.fullDisplayName }              // or it.absoluteUrl or whatever

Tips:

  • hudson.model.Job.class for all kinds of jobs
  • hudson.model.FreeStyleProject.class for freestyle jobs
  • org.jenkinsci.plugins.workflow.job.WorkflowJob.class for pipeline jobs

If you want to find the last 5 builds, you can try:

Integer count = 5
jenkins.model.Jenkins.instance.getAllItems( hudson.model.Job.class )
       .findAll { it.lastBuild }
       .collectEntries {
          def build = it.lastBuild
          (0..<count).inject([:]) { builds, x ->
            if ( build ) {
              builds << [ (build.startTimeInMillis): build ]
              build  = build.getPreviousBuild()
            }
            builds
          }
       }.sort{ a, b -> a.key <=> b.key }
		.collect { it.value }.reverse().take(count)
		.collect { it.fullDisplayName }             // or any function for hudson.model.Run or org.jenkinsci.plugins.workflow.job.WorkflowRun or hudson.model.FreeStyleBuild