【发布时间】: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'
}
}
}
}
}
}
}
我的第一个想法是创建一个数组来存储所有节点,然后检查它并通过values在axis中将变量分配给它。但是这个想法is not supported
有人可以帮忙吗? 提前致谢!
【问题讨论】:
标签: jenkins jenkins-pipeline jenkins-groovy