0

I have a Jenkins declarative pipeline in which some stages have a post action:

stage('1 unit tests') { ..... }
stage('2 other tests') {
   steps { ..... }
   post {
      success { ...... }
   }
}

It seems that if a unit test fails (build becomes unstable in stage 1), then the conditional post action of stage 2 is not performed.

How can I make sure the post action is only skipped if the build status changes during the current stage?

dwjbosman
  • 906
  • 9
  • 33

1 Answers1

0

There are some "options", I don't know what you might like or find acceptable. If a stage is skipped; it also skips all of its internals.

1: It's not exactly what you want but you can manipulate/mark a stage differently than other stages and continue the execution using something like the skipStagesAfterUnstable option or catchError. For more info see this post. But it may also be to heavy-handed and forcing you into a limited set of results or lead to unwanted execution.

catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')

2: You can move the stage to a separate pipeline/job, and trigger it via or after the run of this pipeline

3: Another option might be something like the following pseudo-code, but this feels more like a hack and adding ('global') 'state flags' adds clutter:

failedOne = false
failedTwo = false

pipeline {
    agent any
    stages {
        stage('Test One') {
            steps {...}
            post {
                failure {
                    failedOne=true
                    postFailureOne()
                }
            }
        }
        stage('Test Two') {
            steps {...}
            post {
                failure {
                    failedTwo=true
                    postFailureTwo()
                }
            }
        }
    }
    post {
        success { .... }
        failure {
            if (!failedTwo) postFailureTwo()
        }
    }
}

void postFailureOne() { echo 'Oeps 1' }
void postFailureTwo() { echo 'Oeps 2' }
Sir.Chefcoq
  • 330
  • 1
  • 7