How to get a list of all Jenkins nodes assigned with label including master node?

萝らか妹 提交于 2019-12-04 01:55:46

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 contains the master-node and all the slaves.

Update to @patrick-b answer : contains can be buggy if you have labels containing same string, I've added a split step do check every label separated with spaces @NonCPS def hostNames(label) { def nodes = [] jenkins.model.Jenkins.instance.computers.each { c -> c.node.labelString.split(' ').each { l -> if (l != null && l.equals(label)) { nodes.add(c.node.selfLabel.name) } } } return nodes }

Updated answer: in a pipeline use nodesByLabel to get all nodes assigned to a label.

Try using for (aSlave in hudson.model.Hudson.instance.slaves) {} and aSlave.getLabelString()); to get all the labels for all of your nodes. You can construct a list of nodes per label this way.

I think that you can do this with:

def nodes = Jenkins.instance.getLabel('my-label').getNodes()
for (int i = 0; i < nodes.size(); i++) {
    node(nodes[i].getNodeName()) {
        // on node
    }
}

I don't know for sure whether this works with cloud nodes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!