Post failure block in Jenkinsfile is not working

淺唱寂寞╮ 提交于 2019-12-01 22:47:49

I've found the issue, after several hours of searching. What you are missing (and I was missing too) is the catchError section.

pipeline {
    agent any
    stages {
        stage('Compile') {
           steps {
                catchError {
                    sh './gradlew compileJava --stacktrace'
                }
            }
            post {
                success {
                    echo 'Compile stage successful'
                }
                failure {
                    echo 'Compile stage failed'
                }
            }
        }
        /* ... other stages ... */
    }
    post {
        success {
            echo 'whole pipeline successful'
        }
        failure {
            echo 'pipeline failed, at least one step failed'
        }
    }

You should wrap every step that can potentially fail into a catchError function. What this does is:

  • If an error occurs...
  • ... set build.result to FAILURE...
  • ... and continue the build

The last point is important: your post{ } blocks did not get called because your entire pipeline was aborted before they even had a chance to execute.

Just in case someone else also made the same stupid mistake I did, don't forget the post block needs to be inside the pipeline block.

i.e. This is apparently valid, but (obviously) won't work:

pipeline {
  agent { ... }
  stages { ... }
}
// WRONG!
post {
  always { ... }
}

This is what's correct:

pipeline {
  agent { ... }
  stages { ... }
  post {
    always { ... }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!