The horror stories I found while searching for an answer for this one…
OK, I have a .sh script which pretty much does everything Jenkins supposed to do:
- checks out sources from SVN
- build the project
- deploys the project
- cleans after itself
So in Jenkins I only have to 'build' the project by running the script in an Execute Shell command.
The script is ran (the sources are downloaded, the project is build/deploy) but then it marks the build as a failure:
Build step 'Execute shell' marked build as failure
Even if the script was successfully ran! I tried closing the script with:
- exit 0 (still marks it as failure)
- exit 1 (marks it as failure, as expected)
- no exit command at all (marks it as failure)
When, how and why does Execute Shell mark my build as a failure?
Best Answer
First things first, hover the mouse over the grey area below. Not part of the answer, but absolutely has to be said:
The short answer is: the exit code of last command of the Jenkin's Execute Shell build step is what determines the success/failure of the Build Step.
0
- success,anything else
- failure. Note, this is determining the success/failure of the build step, not the whole job run. The success/failure of the whole job run can further be affected by multiple build steps, and post-build actions and plugins.You've mentioned
Build step 'Execute shell' marked build as failure
, so we will focus just on a single build step. If your Execute shell build step only has a single line that calls your shell script, then the exit code of your shell script will determine the success/failure of the build step. If you have more lines, after your shell script execution, then carefully review them, as they are the ones that could be causing failure.Finally, have a read here Jenkins Build Script exits after Google Test execution. It is not directly related to your question, but note that part about Jenkins launching the Execute Shell build step, as a shell script with
/bin/sh -xe
The
-e
means that the shell script will exit with failure, even if just 1 command fails, even if you do error checking for that command (because the script exits before it gets to your error checking). This is contrary to normal execution of shell scripts, which usually print the error message for the failed command (or redirect it to null and handle it by other means), and continue.To circumvent this, add
set +e
to the top of your shell script.Since you say your script does all it is supposed to do, chances are the failing command is somewhere at the end of the script. Maybe a final echo? Or copy of artifacts somewhere? Without seeing the full console output, we are just guessing.
Please post the job run's console output, and preferably the shell script itself too, and then we could tell you exactly which line is failing.