grayaii grayaii - 3 years ago 364
Groovy Question

How to get a list of running jenkins builds via groovy script?

Is there a way to get a list of RUNNING builds in Jenkins via a System Groovy Script?
I tried looping through the busy executors, but from an executor object, I cannot get the build object:

def busyExecutors = Jenkins.instance.computers
.collect {
c -> c.executors.findAll { it.isBusy() }
.flatten() // reminder: transforms list(list(executor)) into list(executor)

busyExecutors.each { e ->
println('=====print out methods of executor object=======');
println e.metaClass.methods*.name.sort().unique();


I can also target the JOB that I'm interested in like so:

def item = hudson.model.Hudson.instance.getItem("my_job");
println item.metaClass.methods*.name.sort().unique();

But then I will have to loop through 100s (if not more) builds and ask each build if they are running.

There has to be an easier/better way of getting a list of running builds.

There is a lot of information on how to do various things via System Groovy Scripts (some of which I wrote), but I cannot figure out how to get a list of running builds:

How to get currently running job's node name in jenkins using groovy

How to make a Jenkins/Hudson job surveil some others jobs and decide whether to build or not?

Answer Source

I found a way to do this without using the REST API or parsing XML:

Jenkins.instance.getItems().each { job->
  job.builds.each { build->
    if (build.getResult().equals(null)) {
      // do stuff here...

Note that this won't descend into folders or Multibranch Pipelines or anything like that. You'll need to manually descend into folders or concoct some way of doing it automatically. For instance, here's a version that works for a Multibranch Pipeline:

Jenkins.instance.getItemByFullName(multibranchPipelineProjectName).getItems().each { repository->
  repository.getItems().each { branch->
    branch.builds.each { build->
      if (build.getResult().equals(null)) {
        // do stuff here ...

I think there may be a more accurate method to use than build.getResult().equals(null) to determine if a build is running or not, but I'm having trouble finding good API docs, so I'm not sure. This was just the first method that I found using object introspection that worked.

Again due to the lack of API docs, I'm not sure if there's a significant difference between Jenkins.instance.getItems() which I used here and Jenkins.instance.getAllItems() which was used in this answer.

Finally, note that this is a relatively inefficient method. It iterates over every build of every job, so if you save a long history of builds (the default setting is to save a history of only 10 builds per job) or have thousands of jobs, this may take a while to run. See How do I Efficiently list **All** currently running jobs on Jenkins using Groovy for a question that asks how to do this task more efficiently.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download