user2257530 user2257530 - 9 days ago 6
Groovy Question

How to list Jenkins projects/jobs in system groovy only on master node?

How to list projects/job names in system Groovy only on master node?
To be more specific:

I need only list of jobs which runs on master! I don't want output any job which runs on slave nodes!

Answer

How to list all jobs that has run on master, not that will be run on master... This script only checks the jobs that has actually run and isnt't removed/cleaned up.

http://javadoc.jenkins-ci.org/hudson/model/AbstractBuild.html#getBuiltOnStr()

import hudson.model.*

jenkins = Hudson.instance

items = jenkins.items

for (item in items){
  counter++
  def job = item.getLastBuild()
  if(job != null){
        if(job.getBuiltOnStr() == null || job.getBuiltOnStr() == "")
          println "Built on master: " + job.toString()
  }
} 
Comments