How to continue past a failing stage in Jenkins declarative pipeline syntax

jenkins declarative pipeline continue on failure
jenkins pipeline fail stage
jenkins continue on failure
jenkins scripted pipeline continue on failure
jenkins pipeline disable stage
jenkins pipeline always execute stage
jenkins run build step after failure
jenkins declarative pipeline build job

I want to define multiple stages in Jenkins declarative pipeline syntax which can continue past any one of them failing. I cannot find any existing questions which are true duplicates, because they all assume or allow scripted syntax.

pipeline {
    agent any
    stages {
        stage('stage 1') {
            steps {
                echo "I need to run every time"
            }
        }
        stage('stage 2') {
            steps {
                echo "I need to run every time, even if stage 1 fails"
            }
        }
        stage('stage 3') {
            steps {
                echo "Bonus points if the solution is robust enough to allow me to continue *or* be halted based on previous stage status"
            }
        }
    }
}

To clarify, I'm not looking for how to do accomplish this in scripted syntax. I'm trying to understand if this kind of flow control is actually supported and formalized in declarative syntax. To that end, I'll try to define exactly what I'm looking for:

Required
  • No try/catch. I don't want to drop down into scripted mode, or "wrap" my declarative pipeline in another shared library or scripted block.
  • No post step shenanigans. I want true multiple stages, not one stage with a post always step that contains all my other logic
Optional
  • The failing stage should be recognized as failed; I don't want a failed stage showing up as green because it was "skipped" or "continued".
  • A build with any failed stage should be marked as red (or yellow, or anything that is not green).
Related but Not Sufficient
  • How to continue a stage in jenkins pipeline even if the build fails
  • Determine Failed Stage in Jenkins Declaritive Pipeline
  • Continue Jenkins pipeline past failed stage
  • Show a Jenkins pipeline stage as failed without failing the whole job
  • Jenkins continue pipeline on failed stage

This is now possible:

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:

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 is a fairly new feature.

EDIT: This is the question that this answer was originally written for. It is also the correct answer for a few other questions, which is why I posted this answer there as well. This is the right solution for multiple similar problems. I've tailored my other answers to their specific questions to make that clear. I only copied the answer to save myself some time. That doesn't mean it's not a good correct answer.

Continue Jenkins pipeline past failed stage, This is now possible. Below is an example of a declarative pipeline, but catchError works for scripted pipelines as well. pipeline { agent any  The accepted answer wouldn't fail the stage or even mark it as unstable. It is now possible to fail a stage, continue the execution of the pipeline and choose the result of the build: In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:


I may be missing something, but the idea of declarative, opinionated pipeline is to provide coverage of most simple use cases. The moment you need something the opinionated hasn't covered, you HAVE to resort to scripted pipeline, this is only referring to the "requirement" of "declarative pipeline": not going to happen now.

As to your other "requirements", they make very little sense, since the whole idea is to wrap low-level ugliness into shared libraries providing users with constructs like:

    mylib.failable_stages({
      stages {
        stage('stage 1') {
          steps {
            echo "I need to run every time"
          }
        }
        stage('stage 2') {
          steps {
            echo "I need to run every time, even if stage 1 fails"
          }
        }
        stage('stage 3') {
          steps {
            echo "Bonus points if the solution is robust enough to allow me to continue *or* be halted based on previous stage status"
          }
        }
      }
    })

Naturally, you would have to find or implement such mylib class and the failable_stages would get a closure, and wrap it in various plumbing/boilerplate code pieces.

Hope this is helpful.

Running multiple steps, When a step fails to execute correctly the Pipeline will fail. When all the On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline. Jenkinsfile (Declarative Pipeline) With the basics of defining multiple steps finished, let's continue to "Defining execution environments"  How to send “back to normal” notifications in Jenkins Declarative Pipeline? the new declarative syntax for this. continue past a failing stage in Jenkins


Pipeline Syntax, Declarative Pipeline is a relatively recent addition to Jenkins Pipeline which presents a more Only run the steps in post if the current Pipeline's or stage's run has a "failed" status, Do not allow the pipeline to resume if the master restarts. once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing  Problem There is no way to catch a failing block, mark it as "failed" with a user defined description and continue execution of the Pipeline. In the example below, the Optional Tests stage would always be successful.


I achieved it using post. My requirement was to send out slack notification irrespective of the build status.

@Library('instanceGroups')
import x.z.y.jenkins.libraries.SlackNotifier

def slackHelper = new x.z.y.jenkins.libraries.SlackNotifier(env)
final String projectName = "pomeranian"
final String featureBranchPattern = "f_"

pipeline {
    agent any
    options { disableConcurrentBuilds() }

    stages {
        stage('clean') {
            steps {
                script {
                    try {
                        echo 'Current Branch...' + env.BRANCH_NAME
                        sh 'rm -rf /var/lib/jenkins/.gradle/caches'
                        sh './gradlew clean'
                    } catch (e) {
                        currentBuild.result = 'FAILURE'
                        slackHelper.buildGenericJobFailureNotificationMessage()
                        throw e
                    }
                }
            }
        }

        stage('compile') {
            steps {
                script {
                    try {
                        sh "./gradlew compileJava"
                    } catch (e) {
                        currentBuild.result = 'FAILURE'
                        slackHelper.getCompilationFailureSlackNotificationMessage()
                        throw e
                    }
                }
            }
        }

        stage('test') {
            steps {
                script {
                    try {
                        sh "./gradlew test"
                    } finally {
                        junit 'build/test-results/test/*.xml'

                        slackHelper.getTestStatuses(currentBuild)
                        slackHelper.buildUnitTestSlackNotificationMessage()
                    }
                }
            }
        }

        stage('publish 2 nexus') {
            steps {
                script {
                  // some code
                }
            }
        }

        stage('git tagging') {
            steps {
                script {
                    // some more code...
            }
        }
    }


    post {
        always {
            script {
                slackHelper.finalBuildStatusMessage(currentBuild)
                slackSend(channel: '#ci-cd', attachments: slackHelper.getFinalSlackMessage())
            }
        }
    }
}

Jenkins Pipeline - fail a stage but continue the build, I would like to fail a stage in a pipeline job without aborting the build (pipeline). There's no *easy* way to do this - in Declarative + Blue Ocean, we now have this Can you please link to the syntax for the Blue Ocean metadata to use? /​40600621/continue-jenkins-pipeline-past-failed-stage/45815702. In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed: 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.


jenkins: Continue Jenkins pipeline past failed stage, Continue Jenkins pipeline past failed stage It depends whether you are using declarative pipeline syntax or scripted pipeline syntax. Browse other questions tagged jenkins groovy jenkins-pipeline jenkins-groovy jenkins-declarative-pipeline or ask your own question. The Overflow Blog Podcast 231: Make it So


Jenkins Pipelines and their dirty secrets 3., usr/bin/env groovy // HEADS UP: This is Declarative Pipeline syntax execute this step, and continue to the next step even if this step failed. Restart from a Stage. You can restart any completed Declarative Pipeline from any top-level stage which ran in that Pipeline. This allows you to rerun a Pipeline from a stage which failed due to transient or environmental considerations, for example. All inputs to the Pipeline will be the same.


Allow Jenkins stage to continue on failed builds · Issue #685 , There is a nice option in the Check Precondition stage to enable/disable "Fail Pipeline". See screenshot: It would be nice to have this same  Only run the steps in post if the current Pipeline’s or stage’s run’s status is failure, unstable, or aborted and the previous run was successful. aborted. Only run the steps in post if the current Pipeline’s or stage’s run has an "aborted" status, usually due to the Pipeline being manually aborted. This is typically denoted by gray in the web UI.