Juha-Matti Heikkala
Juha-Matti Heikkala

Reputation: 31

jenkins pipeline array of nodes with label

I'm trying to run steps in jenkins pipeline parallel in nodes with specified label

I have a hardcoded define with node names "def deviceLabels = ['Node 1', 'Node 2']" and both of these have also label "Device" and that works fine

"return jenkins.model.Jenkins.instance.nodes.collect { node -> node.name }" returns all node names but how to get array of nodes containing label "Device" ?

Upvotes: 3

Views: 4064

Answers (2)

AsafD
AsafD

Reputation: 31

You can use this to get all nodes with common labels, this example will return a list with all nodes that have the label 'Device1' and 'JAVA'

//define what labels to select
labelToSelect = ['Device1','JAVA']
listOfNodeNames = []
//iterate over each label
labelToSelect.each  { label ->
   listOfLabelNodeNames = jenkins.model.Jenkins.instance.nodes.collect {
             //for each label get the list of nodes that contain the label
           node -> node.getLabelString().contains(label) ? node.name : null
     }
     // if it's the first list then just keep the first list
      if(listOfNodeNames.size() == 0)
          listOfNodeNames = listOfLabelNodeNames
      else // do intersection with the next list of nodes that contain the next label
         listOfNodeNames = listOfNodeNames.intersect(listOfLabelNodeNames)
}
//remove all instances of null in our collection
listOfNodeNames.removeAll(Collections.singleton(null))
listOfNodeNames

Upvotes: 0

burnettk
burnettk

Reputation: 14047

This seems to return a list of the node names that contain a certain label:

labelToSelect = 'Device'
listOfNodeNames = jenkins.model.Jenkins.instance.nodes.collect {
  node -> node.getLabelString().contains(labelToSelect) ? node.name : null
}
listOfNodeNames.removeAll(Collections.singleton(null))
listOfNodeNames

You can mess around with this sort of thing in your jenkins console (your.jenkins.url/script).

Upvotes: 5

Related Questions