Jenkins: Ignore failure in pipeline build step

JenkinsJenkins Pipeline

Jenkins Problem Overview


With jenkins build flow plugin this was possible:

ignore(FAILURE){
    build( "system-check-flow" )
}

How to do this with Declarative Pipeline syntax?

Jenkins Solutions


Solution 1 - Jenkins

To ignore a failed step in declarative pipeline you basically have two options:

  1. Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)

> stage('someStage') { > steps { > script { > try { > build job: 'system-check-flow' > } catch (err) { > echo err.getMessage() > } > } > echo currentBuild.result > } > }

  1. Use catchError

> stage('someStage') { > steps { > catchError { > build job: 'system-check-flow' > } > echo currentBuild.result > } > }

In both cases the build won't be aborted upon exception in build job: 'system-check-flow'. In both cases the echo step (and any other following) will be executed.

But there's one important difference between these two options. In first case if the try section raises an exception the overall build status won't be changed (so echo currentBuild.result => SUCCESS). In the second case you overall build will fail (so echo currentBuild.result => FAILURE).

This is important, because you can always fail the overall build in first case (by setting currentBuild.result = 'FAILURE') but you can't repair build in second option (currentBuild.result = 'SUCCESS' won't work).

Solution 2 - Jenkins

In addition to simply making the stage pass, it is now also possible to fail the stage, but continue the pipeline and pass the build:

pipeline {
    agent any
    stages {
        stage('1') {
            steps {
                sh 'exit 0'
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh "exit 1"
                }
            }
        }
        stage('3') {
            steps {
                sh 'exit 0'
            }
        }
    }
}

In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:

Pipeline Example

As you might have guessed, you can freely choose the buildResult and stageResult, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.

Just make sure your Jenkins is up to date, since this feature is only available since "Pipeline: Basic Steps" 2.16 (May 14, 2019). Before that, catchError is still available but without parameters:

        steps {
            catchError {
                sh "exit 1"
            }
        }

Solution 3 - Jenkins

I was looking for an answer for a long time and I found a hack for it! I put the try/catch block on the whole stage:

 try {
   stage('some-stage') {
         //do something
   }
 } catch (Exception e) {
    echo "Stage failed, but we continue"  
 }
 try {
   stage("some-other-stage") {  // do something }
 } catch (Exception e) {
    echo "Stage failed, but we still continue"
 }

As result you will get something like this: enter image description here

This is still not ideal, but it gives the necessary results.

Solution 4 - Jenkins

In recent versions it's possible to pass propogate=false option to build step.

link: https://jenkins.io/doc/pipeline/steps/pipeline-build-step/

example: build job:"jobName", propagate:false

Solution 5 - Jenkins

Try this example:

stage('StageName1')
{
    steps
    {
        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')
        {
            SomeCodeThatCanBeErrored
        }
    }
}
stage('StageName2')
{
    steps
    {
        ContinueOtherCode
    }
}

Solution 6 - Jenkins

For my decalartive pipeline I have found another solution:

stage('Deploy test')
 {
  steps
   {      
    bat returnStatus: true, script: 'sc stop Tomcat9'
    // The return value of the step will be the status code!
    // evaluate return status yourself, or ignore it
   }
 }

The same works for the sh command to execute scripts on Unix platforms.

The example ignores the return status, because the tomcat might be already stopped, because of a previously failed pipeline run.

Solution 7 - Jenkins

In the new pipeline, you can use try-catch to achieve this.

node{
   try{
      build job: 'system-check-flow'
   }    
   catch (err){
      echo "system-check-flow failed"
   }
   try{
      build job: 'job2'
   }    
   catch (err){
      echo "job2 failed"
   }
}

Here it will build the 'system-check-flow' job. If it fails it will catch the error, ignore, and then move on to build 'job2'

Solution 8 - Jenkins

See this post for a full discussion.

Solution 9 - Jenkins

pipeline {
    agent any

    stages {
                stage('Stage') {
                    steps{
                        script{
                            jobresult = build(job: './failing-job',propagate:false).result
                            if(jobresult != 'SUCCESS'){
                                catchError(stageResult: jobresult, buildResult: 'UNSTABLE'){
                                    error("Downstream job failing-job failed.")
                                }
                            }
                        }
                    }
                }
    }
}

For all those that are wondering about how to set the result of a downstream job to the stage/build) Not the most graceful solution, but it gets the job done. Funny thing is that if this stageResult variable was available as a global variable or as a variable outside the catchError block these kinds of solutions would not be needed. Sadly it isn't, and the only way to set the stage result in a pipeline that I thought of is this way. The error() block is needed, otherwise catchError will not set the stageResult/buildResult(the catchError block requires an error, ofcourse).

Solution 10 - Jenkins

you could put the step script inside "post" step, if if it's a teardown like step

code as below:

    post {
    always {
        script {
            try{
                echo 'put your alway need run scripts here....if it's a teardown like step'
            }catch (err) {
                echo 'here failed'
        }
        script{
            emailext (
                xxxx
            )
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlenkoviView Question on Stackoverflow
Solution 1 - Jenkinsuser1053510View Answer on Stackoverflow
Solution 2 - JenkinsErik BView Answer on Stackoverflow
Solution 3 - JenkinsStacyView Answer on Stackoverflow
Solution 4 - JenkinsVanoView Answer on Stackoverflow
Solution 5 - Jenkins3Dark UnicornView Answer on Stackoverflow
Solution 6 - JenkinsPowerStatView Answer on Stackoverflow
Solution 7 - JenkinsR_KView Answer on Stackoverflow
Solution 8 - JenkinsJesse GlickView Answer on Stackoverflow
Solution 9 - JenkinsДавидко ГеоргиеввView Answer on Stackoverflow
Solution 10 - Jenkinslizisong1988View Answer on Stackoverflow