# How to find last 5 jobs run on Jenkins Agent

**URL:** https://community.jenkins.io/t/how-to-find-last-5-jobs-run-on-jenkins-agent/21339
**Category:** Using Jenkins
**Tags:** question
**Created:** [October 4, 2024, 8:26am UTC](https://community.jenkins.io/t/how-to-find-last-5-jobs-run-on-jenkins-agent/21339 "2024-10-04T08:26:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pradip10](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@pradip10](https://community.jenkins.io/u/pradip10)
#### Post date: [October 4, 2024, 8:26am UTC](https://community.jenkins.io/t/how-to-find-last-5-jobs-run-on-jenkins-agent/21339/1 "2024-10-04T08:26:21Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)
#### Post date: [October 4, 2024, 8:40am UTC](https://community.jenkins.io/t/how-to-find-last-5-jobs-run-on-jenkins-agent/21339/2 "2024-10-04T08:40:48Z")

</div>

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](https://plugins.jenkins.io/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

---

<div class="post-metadata">

### Author: ![pradip10](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@pradip10](https://community.jenkins.io/u/pradip10)
#### Post date: [October 4, 2024, 8:45am UTC](https://community.jenkins.io/t/how-to-find-last-5-jobs-run-on-jenkins-agent/21339/3 "2024-10-04T08:45:56Z")

</div>

Hello Markus,

Unfortunately, we have only freestyles jobs not a pipeline jobs. Still can i use [Pipeline Agent Build History](https://plugins.jenkins.io/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

---

<div class="post-metadata">

### Author: ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)
#### Post date: [October 4, 2024, 8:52am UTC](https://community.jenkins.io/t/how-to-find-last-5-jobs-run-on-jenkins-agent/21339/4 "2024-10-04T08:52:12Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![marslo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/marslo/32/10018_2.png) [@marslo](https://community.jenkins.io/u/marslo)
#### Post date: [October 15, 2024, 1:59am UTC](https://community.jenkins.io/t/how-to-find-last-5-jobs-run-on-jenkins-agent/21339/5 "2024-10-15T01:59:09Z")

</div>

if you want last 5 built jobs, try:

```auto
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:

```auto
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

```
