0

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.

Screenshot

carl verbiest
  • 1,113
  • 1
  • 15
  • 30

1 Answers1

0

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;
                                }
                            }
                        }
                    }
Samit Kumar Patel
  • 1,932
  • 1
  • 13
  • 20