Jenkins post actions on stage level are influenced by buildResult

Viewed 150

In my jenkins pipeline i have several post-success actions in different stages that define environment variables depending on the stage results (in success case). I am also using parallel and sequential stages, but i do not believe this is affecting the result. Below is a trimmed excerpt:

stage('stage1') {
    agent { 
        label 'agent1'
    }
    when { 
        expression { 
            return Jenkins.instance.getItem("${'job1'}").isBuildable()
        }
        environment name: 'job0', value: 'success'
    }
    steps { 
        catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
            build job: 'job1'
        }
    }
    post {
        success {
            script {
                env.job1 = "success"
                echo "job1: ${env.job1} !"
            }
        }
    }
}

Now whenever the build result is set to FAILURE within a stage by catchError, all post actions in other stages do not evaluate the related stage result, but the build result instead, which i do not want (and believe they should not do), referring to https://www.jenkins.io/doc/book/pipeline/syntax/#post :

The post section defines one or more additional steps that are run upon the completion of a Pipeline’s or stage’s run (depending on the location of the post section within the Pipeline)

But in my case, after the first error no more stage level post-success actions are executed.. My workaround so far is to modify catcherror, add a post-failure action that sets another variable which is evaluated in the very last stage to set the build result:

stage('stage1') {
    steps {
        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { 
            build job: 'job1' 
        }
    }
    post {
        success { .. }
        failure {
            script {
                env.testFail = "true"
                echo "testFail: ${testFail} !"
            }
        }
    }
}

..

stage('set buildResult') {
    when {
        environment name: 'testFail', value: 'true'
    }
    steps {
        script {
             bat '''
             echo "testFail variable found. Changing buildresult to FAILURE."
             exit 1
             '''
        }   
    }
}

Is there anything I missed in the docs, or can anybody confirm this behavior? I would appreciate to remove my workaround and let the catchError do all the work for me. I have already searched through official docs and mailing list, including jenkins issues, but have not found information about this issue anywhere.

This is my first time contributing to stackoverflow, so please forgive me if I made mistakes or did not express myself clearly (leaving reader-only mode).

Jenkins - v2.204.2 Pipeline - v2.6 Pipeline Declarative - v1.8.4

0 Answers
Related