Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get failure reason from Jenkins pipeline

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

like image 987
MikeB Avatar asked Dec 16 '17 06:12

MikeB


1 Answers

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).

like image 162
VonC Avatar answered Sep 23 '22 07:09

VonC