16

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

MikeB
  • 2,402
  • 1
  • 15
  • 24
  • did you take a look at https://stackoverflow.com/questions/43736594/get-error-reason-in-jenkinsfile-failure – saw303 Dec 16 '17 at 07:00
  • That's a decent start, but only provides the failure stage not reason/exception – MikeB Dec 16 '17 at 07:20

2 Answers2

3

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Thanks for the info! Since I'm using a declarative pipeline, do I need to wrap every step of every stage in a `script { try { ... } catch(e) { ... } } `? – MikeB Dec 16 '17 at 07:10
  • @MikeB Yes, although declarative might not be compatible with try/catch: see https://stackoverflow.com/q/44003788/6309 – VonC Dec 16 '17 at 07:50
  • If you can catch the error within a step, you can use script to assign a variable: https://github.com/jenkinsci/pipeline-examples/blob/094fa9f02f95dfc0c7d798c7e83b8d38caae0a65/declarative-examples/simple-examples/postUnstable.groovy – VonC Dec 16 '17 at 07:52
  • Ok yeah you are right. Unfortunately, the exception I got wasn't very helpful: `hudson.AbortException: script returned exit code 1` – MikeB Dec 16 '17 at 08:04
  • @MikeB Not helpful indeed! I am looking at https://github.com/jenkinsci/pipeline-model-definition-plugin/find/master, looking for "Post" and see how this is implemented. – VonC Dec 16 '17 at 08:06
  • @MikeB Just to be sure, did that catched error have a cause (a chained error which might say more about its origin)? – VonC Dec 16 '17 at 08:07
  • `e.cause` is null – MikeB Dec 16 '17 at 08:17
  • 1
    @MikeB, @VonC: Has it been figured out how to get a meaningful error message out of a stage's thrown Exception? We are also only ever getting `hudson.AbortException: script returned exit code 1`... – Going Bananas Jun 12 '18 at 13:42
  • @GoingBananas I have not for now. – VonC Jun 12 '18 at 20:16
0

If you have a way of invoking the curl command, this is what i used in my bash script recently.

full_error_msg=$(curl -s -k -X GET $url/job/$job_name/lastBuild/consoleText 2> /dev/null | tac | grep Error | head -n 2 | tr -d '\n')

Since the error message usually comes at the end of the build, i use tac command to flip the output, grep for the line containing 'Error', and head the next two lines removing the carriage return between these two Error lines.