Skip Jenkins Pipeline Steps If Node Is Offline

I simply did this:

 pipeline {
     agent none
     environment { AGENT_NODE = "somenode" }
     stages {
         stage('Offline Node') {
             when {
                 beforeAgent true
                 expression {
                     return nodesByLabel(env.AGENT_NODE).size() > 0
                 }

             }
             agent {
                  label "${env.AGENT_NODE}"
             }
             steps {
                  ...
             }
         }
     }
}

There is a pipeline call for this.

nodesByLabel 'my-node'

Returns [] if no node is online; returns arraylist with online instances otherwise.


This should work:

Jenkins.instance.getNode('my-node').toComputer().isOnline()

see http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html