I'm trying to connect to couple of my Jenkins agents and run some commands on each of them. After researching, follow the answer from this link, this Jenkinsfile code works fine:
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'
}
}
}
}
}
}
}
But I want to check each of nodes is online or not before doing anything. I haven't had any luck with what I've tried. This is my latest trying:
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'
}
}
}
}
}
}
}
My first idea is create an array to store all the nodes, then check it and assign variable into it through values in axis. But this idea is not supported
Can anyone help? Thanks in advance!