Home > database >  Display Jenkins pipeline stage as failed without failing the whole job
Display Jenkins pipeline stage as failed without failing the whole job

Time:12-31

Could you please tell me what I am doing wrong. I would like to failure a Stage1. Stage2 should be green and that case, and I would like to have whole build Success. What I am doing wrong?

pipeline{
    agent none
    stages{
        stage ("Stage1") {    
            steps {
                script{
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                        stage ("stege1") {
                            def seekAndDestroy = build job: 'SeekAndDestroy' // it is going FAILURE
                        } 
                    }
                    stage ("Stege2") {
                        sh "exit 0"
                    }
                }
            }                  
        }
    }
}

CodePudding user response:

catchError is something similar to try/catch block, so you need to catch error of the code you are executing. For details - see documentation: Result

CodePudding user response:

I changed a pipeline based on Karolina Ochlik suggestion but still have a problem

pipeline{
    agent { label "master" }
    stages {
        stage ("stage 1") {
            steps {
                script {
                    catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
                        seekAndDestroyEnv = build job: 'Change_env_state'
                    }
                }
            }
        }
        stage ("stage 2") {
            steps {
                sh "exit 0"
            }
        }
    }
}

if the error is in the job 'Change_env_state' then my whole pipeline is going to finish with failure and catchError doesn't work, if the error is in the pipeline e.g. i declare a wrong build job name

                   catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
                        seekAndDestroyEnv = build job: 'wrong job name'

build is going to change a status to unstable which is OK.

so:

  • how to invoke that called job return failure in current pipeline? then display stage as failure and whole job as unstable or failed?
  • Related