我想在Jenkins管道中并行运行具有指定标签的节点。
我有一个硬编码定义,其中包含节点名称 "def deviceLabels = ['Node 1', 'Node 2']",这两个节点也都有标签 "Device",这个可以正常工作。
"return jenkins.model.Jenkins.instance.nodes.collect { node -> node.name }" 返回所有节点名称,但如何获取包含标签 "Device" 的节点数组?
我想在Jenkins管道中并行运行具有指定标签的节点。
我有一个硬编码定义,其中包含节点名称 "def deviceLabels = ['Node 1', 'Node 2']",这两个节点也都有标签 "Device",这个可以正常工作。
"return jenkins.model.Jenkins.instance.nodes.collect { node -> node.name }" 返回所有节点名称,但如何获取包含标签 "Device" 的节点数组?
这似乎返回包含特定标签的节点名称列表:
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)中调试这种类型的事情。
//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