Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip Jenkins Pipeline Steps If Node Is Offline

I have a Jenkins Pipeline job that, for part of the build, uses a node with a lot of downtime. I'd like this step performed if the node is online and skipped without failing the build if the node is offline.

This is related, but different from the problem of skipping parts of a Matrix Project.

I tried to programmatically check if a node is online like so.

jenkins.model.Nodes.getNode('my-node').toComputer().isOnline()

This runs up against the Jenkins security sandbox:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified method java.lang.Class getNode java.lang.String

I tried setting a timeout that will be tripped if the node is offline.

try {
    timeout(time: 10, unit: 'MINUTES') {
        node('my-node') {
            // Do optional step
        }
    }
} catch (e) {
    echo 'Time out on optional step. Node down?'
}

This has a major downside. I have to know what the longest time the step would take, then wait even longer when the node is down. I tried working around that with a "canary" step:

try {
    timeout(time: 1, unit: 'SECONDS') {
        node('my-node') {
            echo 'Node is up. Performing optional step.'
        }
    }
    node('my-node') {
        echo 'This is an optional step.'
    }
} catch (e) {
    echo 'Time out on optional step. Node down?'
}

This skips the step if the node is up, but busy with another job. This is the best solution I have come up with so far. Is there just a way to check if the node is online without using a timeout?

like image 314
James Marble Avatar asked Jul 06 '16 19:07

James Marble


People also ask

How do I skip steps in Jenkins pipeline?

You can skip stages in declarative pipelines using when , so the following should work. stages { stage('Deploy') { when { equals expected: true, actual: Deploy } steps { // ... } } }

How do I skip a build in Jenkins?

First add a "Conditional steps (multiple)" build step. Choose "Regular expression match" for the "Run?" field. This will allow you to check if the changeset owner/author is the "jenkins" user. The "Expression" field is "^jenkins$", which is the name of the Plastic SCM user from whom we want to skip the builds.

How do I skip jobs in Jenkins?

Enable on: freestyle job On a freestyle job, the plugin can be enabled under the Environment section. Among other options, there is also a checkbox for "SCM Skip". After enabling plugin there are also options to override matching regex and enable deletion of skipped build.

What happens when a shell step fails in pipeline?

If the shell step fails, the Pipeline build’s status will be set to failed, so that the subsequent mail step will see that this build is failed. In the case of the mail sender, this means that it will send mail. (It may also send mail if this build succeeded but previous ones failed, and so on.)

Does Jenkins getInstance have a security vulnerability?

Note of warning: I just updated to Jenkins ver. 2.32.3 and was notified that Jenkins.getInstance (required for this answer to work) "may have introduced a security vulnerability". The tool "recommends clearing" the signature.

Can I use a stash from one pipeline run in another?

Stashes from one Pipeline run are not available in other runs, other Pipelines, or other jobs. If you want to persist artifacts for use outside of a single run, consider using archiveArtifacts instead. Note that the stash and unstash steps are designed for use with small files.

What happens if the body throws an exception in a pipeline?

If the body throws an exception, mark the build as a failure, but nonetheless continue to execute the Pipeline from the statement following the catchError step.


Video Answer


2 Answers

There is a pipeline call for this.

nodesByLabel 'my-node'

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

like image 73
Kayla Sell Avatar answered Sep 20 '22 14:09

Kayla Sell


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 {
                  ...
             }
         }
     }
}
like image 21
user3470202 Avatar answered Sep 18 '22 14:09

user3470202