jenkins管道带有标签的节点数组

时间:2017-08-23 05:21:51

标签: jenkins jenkins-pipeline

我正在尝试在具有指定标签

的节点中并行执行jenkins管道中的步骤

我有一个带有节点名称的硬编码定义“def deviceLabels = ['Node 1','Node 2']”,这两个都标有“Device”,并且工作正常

“return jenkins.model.Jenkins.instance.nodes.collect {node - > node.name}”返回所有节点名称,但是如何获取包含标签“Device”的节点数组?

2 个答案:

答案 0 :(得分:4)

这似乎返回包含特定标签的节点名称列表:

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

你可以在jenkins控制台(your.jenkins.url / script)中搞乱这类事情。

答案 1 :(得分:0)

您可以使用它来获取所有具有通用标签的节点,此示例将返回包含所有标签为“ Device1”和“ 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