【问题标题】:Check if Agent Node is online检查代理节点是否在线
【发布时间】:2023-01-04 18:24:51
【问题描述】:

我正在尝试连接到我的几个 Jenkins 代理并在每个代理上运行一些命令。经过研究,follow the answer from this link,这个 Jenkinsfile 代码工作正常:

pipeline {
    agent none
    stages {
        stage('Check') {            
            matrix {
                agent {
                    label "${SLAVE}"
                }
   
                axes {
                    axis {
                        name 'SLAVE'
                        values "slv1", "slv2",
                        "slv3"
                    }
                }
                stages {
                    stage('do something') {
                        steps {
                            sh 'hostname'
                        }
                    }
                }
            }
        }
    }
}

但是我想在做任何事情之前检查每个节点是否在线。 我尝试过的东西没有任何运气。这是我最近的尝试:

Boolean finalResult = true

def checkStatus(String nodeName){
    Node cleanUpNode = Jenkins.instance.getNode(nodeName)
        Computer computer = cleanUpNode.toComputer()

        if (cleanUpNode == null) {
            println("ERROR: Node ${nodeName} doesn't exist")
            finalResult = false
            continue
        }

        if (computer.countBusy()) {
            println("WARNING: Ignore ${nodeName} as it is busy")
            continue
        }
        if (computer.isOffline())
        {
            println "Error! Node ${nodeName} is offline.";
            finalResult = false
            continue
        }
    return finalResult    
}

pipeline {
    agent none
    stages {
        stage('Check') {            
            matrix {
                agent {
                    label "${SLAVE}"
                }
                when {
                      expression { checkStatus(${SLAVE}) == true }
                }
                axes {
                    axis {
                        name 'SLAVE'
                        values "slv1", "slv2",
                        "slv3"
                    }
                }
                stages {
                    stage('do something') {
                        steps {
                            sh 'hostname'
                        }
                    }
                }
            }
        }
    }
}

我的第一个想法是创建一个数组来存储所有节点,然后检查它并通过valuesaxis中将变量分配给它。但是这个想法is not supported

有人可以帮忙吗? 提前致谢!

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-groovy


    【解决方案1】:

    专注于checkStatus方法,它有一些一般性的错误,不会起作用。对我有用的是以下内容。

    对于 #nodesByLabel 调用,您需要安装 Pipeline Utility Jenkins 插件。

    def checkStatus (String nodeLabel) {
    
        for (final String nodeName : nodesByLabel(nodeLabel)) {
            Node node = Jenkins.instance.getNode(nodeName)
        
            if (node == null) {
                println("ERROR: Node ${nodeName} doesn't exist")
                return false
            }
        
            Computer computer = node.toComputer()
     
            // TODO skip this; the node is always busy at this point
            if (computer.countBusy()) {
                println("WARNING: Ignore ${nodeName} as it is busy")
                return false
            }
    
            if (computer.isOffline()) {
               println "Error! Node ${nodeName} is offline."
               return false
            }
        }
        return true
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      相关资源
      最近更新 更多