如何获取所有分配了标签的Jenkins节点(包括主节点)的列表? [英] How to get a list of all Jenkins nodes assigned with label including master node?
问题描述
我正在创建Jenkins管道作业,并且需要在所有带有特定标签的节点上运行该作业.
I'm creating a Jenkins pipeline job and I need to run a job on all nodes labelled with a certain label.
因此,我试图获取分配有特定标签的节点名称的列表. (有了一个节点,我可以用getAssignedLabels()
获得标签)
Therefore I'm trying to get a list of node names assigned with a certain label. (With a node I can get the labels with getAssignedLabels()
)
jenkins.model.Jenkins.instance.nodes
中的nodes
-列表似乎不包含我需要包含在搜索中的主节点.
The nodes
-list in jenkins.model.Jenkins.instance.nodes
seems not contain the master-node which I need to include in my search.
我当前的解决方案是遍历jenkins.model.Jenkins.instance.computers
并使用getNode()
方法获取节点.这行得通,但是在Jenkins的javadoc中,我正在阅读此列表,可能不是最新的.
My current solution is to iterate over the jenkins.model.Jenkins.instance.computers
and use the getNode()
-method to get the node. This works, but in the javadoc of Jenkins I'm reading the this list might not be up-to-date.
从长远来看,我将添加(动态)云节点,否则我恐怕将无法使用computers
.
In the long-run I will add (dynamically) cloud-nodes and I'm afraid that I won't be able to use computers
then.
什么是正确的获取所有当前节点的列表?
What is the right get the list of all current nodes?
这就是我现在正在做的事情:
This is what I'm doing right now:
@NonCPS
def nodeNames(label) {
def nodes = []
jenkins.model.Jenkins.instance.computers.each { c ->
if (c.node.labelString.contains(label)) {
nodes.add(c.node.selfLabel.name)
}
}
return nodes
}
推荐答案
这就是我现在正在做的事情.我还没有发现其他东西:
This is the way I'm doing right now. I haven't found something else:
@NonCPS
def hostNames(label) {
def nodes = []
jenkins.model.Jenkins.instance.computers.each { c ->
if (c.node.labelString.contains(label)) {
nodes.add(c.node.selfLabel.name)
}
}
return nodes
}
jenkins.model.Jenkins.instance.computers
包含主节点和所有从节点.
jenkins.model.Jenkins.instance.computers
contains the master-node and all the slaves.
这篇关于如何获取所有分配了标签的Jenkins节点(包括主节点)的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!