本文介绍了如何获取分配有标签的所有 Jenkins 节点的列表,包括主节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 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.get.computers.each { c ->
    if (c.node.labelString.contains(label)) {
      nodes.add(c.node.selfLabel.name)
    }
  }
  return nodes
}

jenkins.model.Jenkins.get.computers 包含主节点和所有从节点.

jenkins.model.Jenkins.get.computers contains the master-node and all the slaves.

这篇关于如何获取分配有标签的所有 Jenkins 节点的列表,包括主节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:28