0

I got strange behavior of Jenkins post build task script. its purpose is showing build error in slack like following.

EDIT: our Jenkins running on Mac OSX Yosemite (10.10.4) and using Unity3d as build tool.

SLACK_BOT_PATH=$WORKSPACE/tools/bot.rb
SLACK_BOT_NAME="cortana"
SLACK_BOT_TOKEN=`cat $WORKSPACE/../../sendchat_token`
ERRORS=`tail -5000 ~/Library/Logs/Unity/Editor${BUILD_NUMBER}.log | grep ": error"`

ruby $SLACK_BOT_PATH $SLACK_BOT_NAME $SLACK_BOT_TOKEN "build fails : $ERRORS"

and strange behavior is, it aborted on the ERRORS= line when ERRORS has no contents (empty string). Jenkins console output is like following.

[workspace] $ /bin/sh -xe /var/folders/j3/8x825bdn2l9dm497yjs2144c0000gn/T/hudson7348609981772923445.sh
+ SLACK_BOT_PATH=*snip*
+ SLACK_BOT_NAME=cortana
++ cat *snip*/../../sendchat_token
+ SLACK_BOT_TOKEN=*snip*
++ tail -5000 ~/Library/Logs/Unity/Editor1710.log
++ grep ': error'
+ ERRORS=
POST BUILD TASK : FAILURE

after I change grep filter so that ERRORS has some contents, the post build script runs correctly again.

I want to report some general error message (eg. build fails) when no actual ERRORS found in logs. but also report detail error message when its available.

of course it is easy that inserting some line to send general message before grep error log so that every time such a general message sent to slack, but I want know the reason why empty ERRORS terminate entire script.

does anyone encounter same issue? if so, finally you know the cause of problem? thanks.

takehiro iyatomi
  • 753
  • 1
  • 7
  • 20

2 Answers2

0

To be precise Jenkins will terminate your build when ERRORS is empty in you code because when there is no output from the grep command, your shell errorlevel will be set to 1 automatically , therefore it terminates your build with error ERRORS= , you can debug the same by printing errorlevel value. echo $ERRORLEVEL [linux] or echo %ERRORLEVEL% [windows]. Error 0 stand for success and other error codes stands for failure. if you want your post build script to be executed ignoring even if grep output is empty , then set errorlevel value in post build step of jenkins build.

ERRORLEVEL=0

Try the above and see if that helps

prudviraj
  • 3,634
  • 2
  • 16
  • 22
  • You are assuming he is running on Windows environment (`echo %ERRORLEVEL%`). He is clearly running in *nix environment, most likely some MacOS. The test should be `echo $?`. – Slav Jun 03 '15 at 14:38
  • @slav , i got your point i have mentioned command according to OS in [OS type] , thanks for highlighting – prudviraj Jun 03 '15 at 16:51
  • @prudviraj thank you for your explanation even through I forget to mention what OS I've used. I'm never aware that grep exits with 1 when no result filtered... I'm ashamed my ignorance : – takehiro iyatomi Jun 05 '15 at 03:28
0

As @prudviraj explained, the issue is that your grep command returns without finding anything, and therefore returns with exit code 1.

Read detailed answer here: Jenkins Build Script exits after Google Test execution

In short: your script is launched with /bin/sh -xe, the -e means "fail immediately on any error", and your grep "errors out" when nothing is found.

The quick fix is to put set +e before the command. But the better way is proper error handling, as explained in the other answer I've linked.

Community
  • 1
  • 1
Slav
  • 27,057
  • 11
  • 80
  • 104
  • thank you! finally I use set +e solution and after that, check return value is empty or not. because we cannot know current grep filter can detect the error beforehand. – takehiro iyatomi Jun 05 '15 at 03:41