post Conditions

The post Block and Build Conditions

post runs after the pipeline or a stage and picks blocks by result, in a fixed order: always, changed, fixed, regression, aborted, failure, success, unstable, unsuccessful, and last cleanup. This job fails on odd build numbers, so three builds walk through the cases:

decl-post: every common conditionGroovy
pipeline {
  agent { label 'linux' }
  stages {
    stage('Test') {
      steps { sh 'test $((BUILD_NUMBER % 2)) -eq 0' }   // odd builds fail
      post { failure { echo 'stage post: the Test stage failed' } }
    }
  }
  post {
    always     { echo "always: ${currentBuild.currentResult}" }
    changed    { echo 'changed: result differs from the previous build' }
    fixed      { echo 'fixed: SUCCESS after a failed or unstable build' }
    regression { echo 'regression: failed after a success' }
    failure    { echo 'failure: notify the team' }
    success    { echo 'success' }
    cleanup    { echo 'cleanup: runs last'; deleteDir() }
  }
}

Build 1 (FAILURE) ran always, changed, failure and cleanup; build 2 (SUCCESS) always, changed, fixed, success and cleanup; build 3 (FAILURE) always, changed, regression, failure and cleanup. In each failed build the stage-level post ran first, inside the stage. changed fired even on build 1, which had nothing to differ from. Notify on fixed and regression (Build Notifications), so people hear when something breaks or recovers rather than on every build, and put deleteDir() or cleanWs() in cleanup.