I'm setting up a declarative pipeline for Jenkins. In my post
section, I am using slackSend
to notify my team that the build is broken. I'd like to include the failure reason. Is this available in env
or currentBuild
or something else? I haven't seen anything in the documentation, but seems like a common use case
I've seen some posts about using currentBuild.rawBuild.getLog(10)
and that works, but it is just filled with way too much information. I need to zero in on the actual exception
Another approach is to use a catchError
or at least a try/catch
.
Then, as in this answer, you can get the error message: String error = "${e}";
Regarding catchError, you would wrap every step that can potentially fail into a catchError
function. If an error occurs, it will set build.result
to FAILURE, and continue the build.
See catchError
, which points out that only the try/catch
approach might be useful to catch the actual error e
(and its string).
You might then add that error string to a global variable, that your post
step could then access.
That would be less verbose and/or more precise than currentBuild.rawBuild.getLog(10)
.
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