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
https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console
https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05
How to make a Jenkins/Hudson job surveil some others jobs and decide whether to build or not?
Go to Script Console under Manage Jenkins, this script will print the name of all jobs including jobs inside of a folder and the folders themselves: Jenkins. instance. getAllItems(AbstractItem.
Visit "Manage Jenkins" > "Manage Nodes". Select any node to view the status page. In the menu on the left, a menu item is available to open a "Script Console" on that specific agent.
Open your jenkins instance script console http://yourJenkins:port/script following is an example for how to get information about a sepcific job. copy the code to the console, change the jobName to the required job and click "Run".
I found a couple ways to do this without using the REST API or parsing XML:
runningBuilds = Jenkins.instance.getView('All').getBuilds().findAll() { it.getResult().equals(null) }
This assumes that you have not deleted or modified the default "All" view in Jenkins. Of course you can substitute a different view name if you know exactly which view your builds are going to be in. Or you can try this approach:
runningBuilds = Jenkins.instance.getItems().collect { job->
job.builds.findAll { it.getResult().equals(null) }
}.flatten()
Although this approach doesn't require a view name, it also has limitations. It 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 these are all relatively inefficient methods. They iterate 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.
This is not particularly efficient (but MUCH more efficient than using the API). It will print out all currently running builds with an HTML link. Can be run in script console or via scriptler.
def now = new Date() // Get the current time
// Get a list of all running jobs
def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll {
it.isBuilding() }
buildingJobs.each { job->
// Enumerate all runs
allRuns = job._getRuns()
allRuns.each { item ->
// If NOT currently building... check the next build for this job
if (!item.isBuilding()) return
// Access and calculate time information for this build.
def startedAt = new Date(item.getStartTimeInMillis())
def duration_mins = ((now.getTime() - item.getStartTimeInMillis()) / 60000).intValue()
estDurationMins = (item.getEstimatedDuration() / 60000).intValue()
String jobname = item.getUrl()
jobname = jobname.replaceAll('job/', '') // Strip redundant folder info.
println "${duration_mins.toString().padLeft(5)}/" +
"${estDurationMins.toString().padLeft(4)} - " +
"<a href=${baseURL}${item.getUrl()}>${jobname}</a>"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With