Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit from the Jenkins pipeline if a stage sets build fail/unstable status?

I have a declarative Jenkins pipeline with stage1, stage2, stage3 and so on. I want to stop stage2 from running if stage1 sets the build unstable/fail.

I know I can stop the steps in stage1 from running using return when the build is not success but couldn't find a way where I can just exit the pipeline without running the stages below stage1

Here is what I have:

    stage('stage1') {             steps {                 script{                     //somesteps                     if ("${stdout}" == "1"){                     currentBuild.result = 'UNSTABLE'                     return                     } //if                     //somesteps             } //script         } //steps     } //stage      // run only when stage1 is success     stage('stage2'){         when {             expression {               params.name ==~ /x|y/             }         }         steps {             script{                     //stage2 steps             }         }     } 

If params.name ==~ /z/ stage 3 will be executed skippping stage2

Note: I cannot include the steps in stage2/3/.. in stage1. It should be that way. Based on the build paramters stage2/3/4... will be called after stage1

like image 823
user6348718 Avatar asked Aug 01 '18 16:08

user6348718


People also ask

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)

How do I abort Jenkins pipeline build?

Pipeline jobs can be stopped by sending an HTTP POST request to URL endpoints of a build. BUILD ID URL/stop - aborts a Pipeline. BUILD ID URL/term - forcibly terminates a build (should only be used if stop does not work).

How do I stop Jenkins pipeline execution?

You can mark the build as ABORTED, and then use the error step to cause the build to stop: if (! continueBuild) { currentBuild. result = 'ABORTED' error('Stopping early…') }


1 Answers

The easiest way to skip remaining pipeline stages is to set up a variable which will control if following stages should be skipped or not. Something like this:

def skipRemainingStages = false  pipeline {     agent any      stages {         stage("Stage 1") {             steps {                 script {                     skipRemainingStages = true                      println "skipRemainingStages = ${skipRemainingStages}"                 }             }         }          stage("Stage 2") {             when {                 expression {                     !skipRemainingStages                 }             }              steps {                 script {                     println "This text wont show up...."                 }             }         }          stage("Stage 3") {             when {                 expression {                     !skipRemainingStages                 }             }              steps {                 script {                     println "This text wont show up...."                 }             }         }     } } 

This is very simple example that sets skipRemainingStages to true at Stage 1 and Stage 2 and Stage 3 get skipped because expression in the when block does not evaluates to true.

enter image description here

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!"         }     } } 

In this case pipeline stops whenever error(msg) step is found and all remaining stages are ignored (when blocks are not even checked).

enter image description here

Of course you can call error(msg) depending on some condition to make it FAILED only if specific conditions are met.

like image 177
Szymon Stepniak Avatar answered Oct 09 '22 00:10

Szymon Stepniak