I see on Declarative Pipeline that it's done via "post" section really easy with conditions like 'always', 'failure', ...:
https://jenkins.io/doc/book/pipeline/syntax/#post
But with Scripted Pipeline there are no examples on how it's done:
This link provides an example but only for "always" condition
https://jenkins.io/doc/book/pipeline/jenkinsfile/#handling-failures
I see this docs on how to set that result but I don't understand because with Declarative Pipeline you don't have to set it manually, plugins provided commands handle that for you.
https://support.cloudbees.com/hc/en-us/articles/218554077-How-to-set-current-build-result-in-Pipeline
Can anyone help me with this?
For example if I do this:
node { try { error 'Test error' } catch (ex) { echo 'Error handled' } }
It doesn't trigger the "FAILURE" build status automatically and I don't see the echo. Why?
Your piece of code works as expected :
node { try { error 'Test error' } catch (ex) { echo 'Error handled' } }
gives :
[Pipeline] node
Running on maître in /var/lib/jenkins/workspace/test-pipeline2
[Pipeline] {
[Pipeline] error
[Pipeline] echo
Error handled
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Explanations :
If you want to mark your build as failed, you have to do this explicitly in the catch block. You can also use the catchError block to handle this for you. See : https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-catcherror-code-catch-error-and-set-build-result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With