Post failure block in Jenkinsfile is not working

前端 未结 2 1462
不知归路
不知归路 2021-01-12 10:56

I\'m trying a post failure action with a parallel step but it never works.

This is my Jenkinsfile:

pipeline {
  agent any
  stages {
    stage(\"tes         


        
2条回答
  •  长情又很酷
    2021-01-12 11:37

    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.

提交回复
热议问题