arielma
arielma

Reputation: 1398

Is it possible to combine failure stage from groovy with try catch from pipeline?

I have a pipeline with some stages and logic. The only possible debugger is adding try-catch to each stage:

stage ("distribution"){
            steps{
                script{
                    try{
    amd_distribution_distribute_bundle
                    }
                   catch (Exception e) {
                    echo e.toString()
                    }
            }
        }
    }

however, if one stage fails, I need to stop the pipeline:

status = sh script: '''
                set +x
                python3.4 main.py --command create_bundle
                ''',  returnStatus:true
             }
        }
     }

    if (status == 1) {
    error("returning 1 from func")
    }

but the try-catch from the pipeline actually cancel it (it proceed to other stages. is there a way to combine them?

Upvotes: 0

Views: 263

Answers (1)

Ben Taylor
Ben Taylor

Reputation: 515

Instead of try/catch you can use a catchError step. You can use the buildResult or stageResult arguments to set what the build result should be set to if the step throws an error:

catchError (stageResult: "FAILURE") {
    sh script: # Your script here
}

See https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#-catcherror-catch-error-and-set-build-result-to-failure

Upvotes: 2

Related Questions