Have Jenkins Fail Fast When Node Is Offline
I ended up creating this solution which has worked well. The first build step of SubJob01 is an Execute system Groovy script, and this is the script:
import java.util.regex.Matcherimport java.util.regex.Patternint exitcode = 0println("Looking for Offline Slaves:");for (slave in hudson.model.Hudson.instance.slaves) { if (slave.getComputer().isOffline().toString() == "true"){ println(' * Slave ' + slave.name + " is offline!"); if (slave.name == "Slave01") { println(' !!!! This is Slave01 !!!!'); exitcode++; } // if slave.name } // if slave offline} // for slave in slavesprintln("\n\n");println "Slave01 is offline: " + hudson.model.Hudson.instance.getNode("Slave01").getComputer().isOffline().toString();println("\n\n");if (exitcode > 0){ println("The Slave01 slave is offline - we can not possibly continue...."); println("Please contact IT to resolve the slave down issue before retrying the build."); return 1;} // ifprintln("\n\n");
The jenkins pipeline statement 'beforeAgent true' can be used in evaluating the when condition previous to entering the agent.
stage('Windows') { when { beforeAgent true expression { return ("${TARGET_NODES}".contains("windows")) } } agent { label 'win10' } steps { cleanWs() ... }
Ref: