Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abort a build immediately with success status in Jenkins pipeline

Is there any pipeline step available to abort a build on certain cases with success status?

There is an error step available to abort a build with failure status. But I don't know about the success status.

like image 259
Yahwe Raj Avatar asked Oct 24 '25 12:10

Yahwe Raj


1 Answers

As was said in other replies, there isn't a step to abort in this way. As Christopher suggested you can use try-catch around the aborting code and use error(). I think you will need to track the abort status of your build - you could define an abort method globally in the pipeline to set this status and raise an error so it will abort other steps in your stage.

If you used the declarative pipeline you can use a 'when' declaration with an expression in later stages so they don't execute when the abort status is set.

I am interested in this problem myself so I worked out an example of a pipeline that does this here:

/**
 * Tracking if the build was aborted
 */
Boolean buildAborted = false

/**
 * Abort the build with a message
 */
def abortBuild = { String abortMessage ->
    buildAborted = true
    error(abortMessage)
}

pipeline {

    agent any

    parameters {
        string(name: 'FailOrAbort', defaultValue: 'ok', description: "Enter 'fail','abort' or 'ok'")
    }

    stages {

        stage('One') {

            steps {

                echo "FailOrAbort = ${params.FailOrAbort}"

                script {

                    try {

                        echo 'Doing stage 1'

                        if(params.FailOrAbort == 'fail') {

                            echo "This build will fail"
                            error("Build has failed")

                        }
                        else if(params.FailOrAbort == 'abort') {

                            echo "This build will abort with SUCCESS status"
                            abortBuild("This build was aborted")

                        }
                        else {

                            echo "This build is a success"

                        }

                        echo "Stage one steps..."

                    }
                    catch(e) {

                        echo "Error in Stage 1: ${e.getMessage()}"

                        if(buildAborted) {
                            echo "It was aborted, ignoring error status"
                        }
                        else {
                            error(e.getMessage())
                        }
                    }
                }
            }

            post {
                failure {
                    echo "Stage 1 failed"
                }
            }
        }

        stage('Two') {

            when {
                expression {
                    return !buildAborted
                }
            }

            steps {
                echo "Doing stage 2"
            }
        }

        stage('Three') {

            when {
                expression {
                    return !buildAborted
                }
            }

            steps {
                echo "Doing stage 3"
            }
        }
    }

    post {
        always  {
            echo "Build completed. currentBuild.result = ${currentBuild.result}"
        }
        failure {
            echo "Build failed"
        }
        success {
            script {
                if(buildAborted) {
                    echo "Build was aborted"
                } else {
                    echo 'Build was a complete success'
                }
            }
        }
        unstable {
            echo 'Build has gone unstable'
        }
    }
}

As a side note there is a property 'currentBuild.result' you can adjust in the pipeline but once set to 'FAILURE' it cannot be cleared back to 'SUCCESS' - the Jenkins model doesn't allow it AFAIK.

like image 195
macg33zr Avatar answered Oct 26 '25 23:10

macg33zr