Igor Zilberman Igor Zilberman - 1 month ago 6
Groovy Question

How to list jenkins slaves of selected projects

For example, I want to list slaves that are defined in "Foo" jobs: Project_Foo1, Project_Foo2 etc...

def matchedJobs = Jenkins.instance.items.findAll { job ->
job.name =~ /Foo/
}


I can list projects name, but how to get the slave names?

Answer

some groovy way

def matchedJobs = Jenkins.instance.items.findAll{ job -> 
  job.name =~ /test/ && job.getAssignedLabel() != null 
}.collect { job ->
      "${job.name}: ${job.getAssignedLabel().getNodes().collect({it.getNodeName()==""?"master":it.getNodeName()}).join(',')}\n" 
}.each { slaves ->
    println slaves
}
Comments