Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 294
dolphy Avatar asked Jul 10 '17 21:07

dolphy


People also ask

How do I stop Jenkins pipeline if stage fails?

Alternatively you can call error(String message) step to stop the pipeline and set its status to FAILED . For example, if your stage 1 calls error(msg) step like: stage("Stage 1") { steps { script { error "This pipeline stops here!" } } }

How do you get failed stage name in Jenkins pipeline?

STAGE_NAME in a post stage the result will be Declarative: Post Actions . Instead, you will need to store the stage name in a variable in all stages. So once a stage fails - Jenkins will not continue with the next stages and therefore you will have the "failed" stage name.

How do I skip a particular stage in Jenkins?

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


2 Answers

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:

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 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.

like image 90
Erik B Avatar answered Sep 27 '22 18:09

Erik B


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.

like image 33
mvk_il Avatar answered Sep 27 '22 18:09

mvk_il