简体   繁体   中英

Show a Jenkins pipeline build job stage as failed without failing the whole job

I have a Jenkins pipeline with some parallel stages that should not fail the job if they fail. Those stages start a build job.

I started from https://stackoverflow.com/a/56975220/1817610 .

The original sample works, but not if my stage builds another pipeline.

pipeline {
agent any
stages {
    stage('1') {
        steps {
            sh 'exit 0'
        }
    }
    stage('2') {
        parallel {
            stage('2.1') {
                steps {
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                        build job: 'Failing pipeline'
                    }
                }
            }
            stage('2.2') {
                steps {
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                        build job: 'Succesful pipeline'

                    }
                }
            }
        }
    }
    stage('3') {
        steps {
            sh 'exit 0'
        }
    }
}
}

See build 7 in screenshot

If I changed the stage to

stage('2.1') {
     steps {
          build job: 'Failing pipeline', propagate: false
     }
}

The job does not fail, but also the stage does not fail, see build 8.

I'd like to have the global state as successful but still showing that one of the builds failed.

截屏

you could make use of pure groovy try..catch block and control your SUCCESS and FAILURE with some condition.

Below is an example:

pipeline {
    agent any;
    stages {
        stage('01') {
            steps {
                sh "echo Hello"
            }
        }
        stage('02') {
            parallel {
                stage('02.1') {
                    steps {
                        script {
                            try {
                                def v = 10/0
                                println v
                            }catch(Exception e) {
                                println e
                            }
                        }
                    }
                }
                stage('02.2') {
                    steps {
                        script {
                            try {
                                def v = 10 % 2
                                println v
                            }catch(Exception e) {
                                println e
                            }
                        }
                    }
                }
            }
        }
    }
}

In my example, my parallel stage 02.1 will fail with java.lang.ArithmeticException: Division by zero but catch block will handle it by catching the exception. if you want to fail the build with some condition, you can put if..else condition inside catch {} and fail by throwing back the exception to Jenkins like

...
                 stage('02.1') {
                    steps {
                        script {
                            try {
                                def v = 10/0
                                println v
                            }catch(Exception e) {
                                if(someCondition) {
                                   println e
                                } else {
                                  throw e;
                                }
                            }
                        }
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM