Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a Jenkins pipeline stage as failed without failing the whole job

Here's the code I'm playing with

node {     stage 'build'     echo 'build'      stage 'tests'     echo 'tests'      stage 'end-to-end-tests'     def e2e = build job:'end-to-end-tests', propagate: false     result = e2e.result     if (result.equals("SUCCESS")) {         stage 'deploy'         build 'deploy'     } else {         ?????? I want to just fail this stage     } } 

Is there any way for me to mark the 'end-to-end-tests' stage as failed without failing the whole job? Propagate false just always marks the stage as true, which is not what I want, but Propagate true marks the job as failed which I also don't want.

like image 465
techgnosis Avatar asked Apr 25 '16 22:04

techgnosis


People also ask

How do you make Jenkins pipeline fail?

You can use the error step from the pipeline DSL to fail the current build. error("Build failed because of this and that..")

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)


2 Answers

This is now possible, even with declarative pipelines:

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.

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

Erik B


Stage takes a block now, so wrap the stage in try-catch. Try-catch inside the stage makes it succeed.

The new feature mentioned earlier will be more powerful. In the meantime:

try {    stage('end-to-end-tests') {      node {              def e2e = build job:'end-to-end-tests', propagate: false        result = e2e.result        if (result.equals("SUCCESS")) {        } else {           sh "exit 1" // this fails the stage        }      }    } } catch (e) {    result = "FAIL" // make sure other exceptions are recorded as failure too }  stage('deploy') {    if (result.equals("SUCCESS")) {       build 'deploy'    } else {       echo "Cannot deploy without successful build" // it is important to have a deploy stage even here for the current visualization    } } 
like image 45
vaza Avatar answered Sep 22 '22 22:09

vaza