Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Get Current Stage Status After Using catchError

This is a follow-up to my earlier question:

Set a stage status in Jenkins Pipelines

It turns out I can keep a pipeline as SUCCESS but can mark an individual stage as UNSTABLE if I want via catchError like this:

node()
{
    stage("Stage 1")
    {
        catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE')
        {
            sh 'exit 1'
        }
    }
}

If I want to get the current status of the pipeline itself, I can use currentBuild.getCurrentResult() but I don't see a currentStage analog to this.

I'm interested in trying out a pattern that might look something like this in my stages:

stage("1") {
    catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
        // do stuff
    }
    // perhaps more catchError() blocks
    if(currentStage.getCurrentResult() == "UNSTABLE") {
        // do something special if we're unstable
    }
}

but that would fail because there's no currentStage available.

So basically, catchError() is nice but I'd like to know how I can catch the status change to my stage if it gets changed... Does anyone know how you access the status of the current stage you're in from a pipeline?

like image 700
TxAG98 Avatar asked Nov 06 '19 18:11

TxAG98


3 Answers

Though there is no direct method for accessing the result of a stage in a pipeline as of now, you can work around it. This is considering you are only interested in either SUCCESS or UNSTABLE stage results as per the question and not in FAILURE.

The workaround is to initialize an empty map at the top of your pipeline to store the result of each stage. Now, instead of the catchError() method, use the unstable() method in combination with a try-catch block. This is because the latter not only lets you set the result as unstable but also perform other operations such as add the result to the map in the except block. Then you can read this stored result from the map in your if statement.

Example

stageResults = [:]
...
stage("1") {
    try {
        // do stuff
        // Add to map as SUCCESS on successful execution 
        stageResults."{STAGE_NAME}" = "SUCCESS"
    } catch (Exception e) {
        // Set the result and add to map as UNSTABLE on failure
        unstable("[ERROR]: ${STAGE_NAME} failed!")
        currentBuild.result = "SUCCESS"
        stageResult."{STAGE_NAME}" = "UNSTABLE"
    }
    if(stageResults.find{ it.key == "{STAGE_NAME}" }?.value == "UNSTABLE") {
        // do something special if we're unstable
    }
}
like image 131
Dibakar Aditya Avatar answered Oct 10 '22 09:10

Dibakar Aditya


I did it like this (to keep the catchError):

def boolean test_results = false

pipeline {
    ...
    stage( 'x' ) {
        steps{
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                
                <Do some dangerous stuff here>
                
                //
                // If we reached here the above step hasn't failed
                //
                script { test_results = true }
            }
        }
    }
    stage( 'y' ) {
        steps{
            script{
                if( test_results == true ) {
                } else {
                }
            }
        }
    }
}
like image 5
Alexander Dultsev Avatar answered Oct 10 '22 08:10

Alexander Dultsev


As for me the most elegant way to do it, it's a post section. In your example, you mark the stage as UNSTABLE so after that you can catch it using post->unstable.

stage("1") {
    steps {
        catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
            error 'Something goes wrong'
        }
    }
    post {
        always { echo 'Executed on every build'}
        unstable { echo 'Executed only if build is unstable (marked by catchError)'}
    }
}
like image 3
Oleksandr Dashkov Avatar answered Oct 10 '22 09:10

Oleksandr Dashkov