How to mark build success when one of the stages is aborted? How to mark build success when one of the stages is aborted? jenkins jenkins

How to mark build success when one of the stages is aborted?


I would suggest one improvement to Michael's answer (which is correct btw). You can use catchError to mark stage ABORTED (or UNSTABLE) and mark the build SUCCESS, but you need to wrap the code that may timeout with try-catch block to control the error. Consider the following example:

pipeline {    agent any    stages {        stage('First') {            options {                timeout(time: 3, unit: 'SECONDS')            }            steps {                script {                    catchError(buildResult: 'SUCCESS', stageResult: 'ABORTED') {                        try {                            echo "Executing stage I"                            sleep 4                        } catch(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {                            error "Stage interrupted with ${e.toString()}"                        }                    }                }            }        }        stage('Second') {            steps {                script {                    echo "Executing stage II"                }            }        }    }}

When you run this pipeline, the stage that timed out is marked as ABORTED, but the pipeline continues and if there is no failure in the remaining stages, it is marked as SUCCESS.

enter image description here

And here is what the UNSTABLE stage status looks like.

enter image description here

Michael's solution works as well, but it produces a slightly different result - the stage that times out is marked as SUCCESS, and this might be less intuitive. You need to click on the stage to check if it timed out or not.

pipeline {    agent any    stages {        stage('First') {            options {                timeout(time: 3, unit: 'SECONDS')            }            steps {                script {                    try {                        echo "Executing stage I"                        sleep 4                    } catch(Exception e) {                        currentBuild.result = "SUCCESS"                    }                }            }        }        stage('Second') {            steps {                script {                    echo "Executing stage II"                }            }        }    }}

enter image description here


Your catchError() won't work in your case. The documantation (Source) tells the following:

buildResult (optional)

If an error is caught, the overall build result will be set to this value. Note that the build result can only get worse, so you cannot change the result to SUCCESS if the current result is UNSTABLE or worse. Use SUCCESS or null to keep the build result from being set when an error is caught.

The build status is set with currentBuild.currentResult which can have three values: SUCCESS, UNSTABLE, or FAILURE.

If you want to mark the build as SUCCESS on abortion the post-option (Source) aborted can be used:

pipeline {    agent any    stages {        stage('Example') {            steps {                echo 'Hello World'            }        }    }    post {         aborted {             // Executed only if stage is aborted            currentBuild.result = 'SUCCESS'        }    }}