How/When does Execute Shell mark a build as failure in Jenkins?
Asked Answered
N

8

122

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?

Newby answered 2/4, 2014 at 14:25 Comment(0)
A
142

First things first, hover the mouse over the grey area below. Not part of the answer, but absolutely has to be said:

If you have a shell script that does "checkout, build, deploy" all by itself, then why are you using Jenkins? You are foregoing all the features of Jenkins that make it what it is. You might as well have a cron or an SVN post-commit hook call the script directly. Jenkins performing the SVN checkout itself is crucial. It allows the builds to be triggered only when there are changes (or on timer, or manual, if you prefer). It keeps track of changes between builds. It shows those changes, so you can see which build was for which set of changes. It emails committers when their changes caused successful or failed build (again, as configured as you prefer). It will email committers when their fixes fixed the failing build. And more and more. Jenkins archiving the artifacts also makes them available, per build, straight off Jenkins. While not as crucial as the SVN checkout, this is once again an integral part of what makes it Jenkins. Same with deploying. Unless you have a single environment, deployment usually happens to multiple environments. Jenkins can keep track of which environment a specific build (with specific set of SVN changes) is deployed it, through the use of Promotions. You are foregoing all of this. It sounds like you are told "you have to use Jenkins" but you don't really want to, and you are doing it just to get your bosses off your back, just to put a checkmark "yes, I've used Jenkins"

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.

Andalusite answered 2/4, 2014 at 15:23 Comment(7)
The reason I still use Jenkins is unclear to me... It seems that someone at the top did not quite understood that we already have this script and insisted we use Jenkins. I feel like I'm in a Dilbert strip. Thank you for the -e tip. It solved the problemNewby
There are still good reasons to use Jenkins: the audit trail, build status visibility, etc. If you already have a build script, moving it to Jenkins is a good first step before refactoring it to take advantage of Jenkins features.Tahr
Cross linking this answer serverfault.com/a/143576/186454 set +e and set -e can be specified anywhere in your script. Any code in between will not fail the build if returned value is not 0.Endotoxin
very well said on the shell script vs jenkins setPt
we use jenkins to provides authenticated access and a UI for job. A cron would not cut it. Jenkins does not just run cripts.Ouabain
I am new at this. And i was getting the same error when i was trying to build an Angular app. I tried everything... Then i thought that the reason could be because of lack of memory available in my server. So i upgraded and added more RAM. After that it worked well. :)Pastis
The more I use Jenkins the more I think no one should use Jenkins. That said, why can't we just get an answer to that actual question. Where does that message come from? Why is there any person on this planet who thinks it's OK to have that message without saying what it is from or why it is being marked as failure?Frasch
C
104

Simple and short answer to your question is

Please add following line into your "Execute shell" Build step.

#!/bin/sh

Now let me explain you the reason why we require this line for "Execute Shell" build job.

By default Jenkins take /bin/sh -xe and this means -x will print each and every command.And the other option -e, which causes shell to stop running a script immediately when any command exits with non-zero (when any command fails) exit code.

So by adding the #!/bin/sh will allow you to execute with no option.

Crigger answered 2/12, 2014 at 6:23 Comment(3)
Upvoted. Didnt know about the -xe default. When my grep comman was not finding a string my entire script failed because grep returned a non 0 return value :)Jokester
Worked great! Using it on my of the non-crucial steps, a cleanup step which just do something like find . -name 'bower_components' -exec rm {} \; and in some cases, it was failing. Thanks!Collagen
this cleared everything - 'And the other option -e, which causes shell to stop running a script immediately when any command exits with non-zero (when any command fails) exit code.'Bramwell
C
3

In my opinion, turning off the -e option to your shell is a really bad idea. Eventually one of the commands in your script will fail due to transient conditions like out of disk space or network errors. Without -e Jenkins won't notice and will continue along happily. If you've got Jenkins set up to do deployment, that may result in bad code getting pushed and bringing down your site.

If you have a line in your script where failure is expected, like a grep or a find, then just add || true to the end of that line. That ensures that line will always return success.

If you need to use that exit code, you can either hoist the command into your if statement:

grep foo bar; if [ $? == 0 ]; then ...    -->   if grep foo bar; then ...

Or you can capture the return code in your || clause:

grep foo bar || ret=$?
Crowell answered 17/3, 2017 at 11:55 Comment(1)
Thanks Bryan. You saved my day . Also, I think it is good idea to turn on both -x and -e . So that you see in your jenkins log.Marigolda
J
3

I 've tried all mentioned options (even changing sh to bash without -xe params), the only one option worked for me is:

<command-which-returns-not-zero> || exit 0
Jestude answered 14/1, 2022 at 13:5 Comment(0)
F
2

Plain and simple:

If Jenkins sees the build step (which is a script too) exits with non-zero code, the build is marked with a red ball (= failed).

Why exactly that happens depends on your build script.

I wrote something similar from another point-of-view but maybe it will help to read it anyway: Why does Jenkins think my build succeeded?

Forbidding answered 4/4, 2014 at 5:20 Comment(0)
G
2

So by adding the #!/bin/sh will allow you to execute with no option.

It also helped me in fixing an issue where I was executing bash script from Jenkins master on my Linux slave. By just adding #!/bin/bash above my actual script in "Execute Shell" block it fixed my issue as otherwise it was executing windows git provided version of bash shell that was giving an error.

Gout answered 17/10, 2017 at 9:27 Comment(0)
M
0

Try and always find where exactly its failing by adding the following line into your "Execute shell" Build step.

#!/bin/sh -xe

By adding the -x you will print each and every command that ran (including the lines from embedded scripts) and that will help in spotting the root cause.

Removing the -e option i.e. running #!/bin/sh will allow you to execute with no option, which is really a bad idea as Bryan explained well in one of the answers.

The problem is with no option Jenkins will ignore errors and continue execution of subsequent steps (if there are any) which will leave your process in an consistent state. If this is for a production build or deployment, that may have a bad impact.

Once you find the problem area, run the same failing command from the directory as jenkins-user manually, to get to the exact error/rootcause.

Mejia answered 5/8, 2022 at 23:37 Comment(0)
R
-1

In Jenkins ver. 1.635, it is impossible to show a native environment variable like this:

$BUILD_NUMBER or ${BUILD_NUMBER}

In this case, you have to set it in an other variable.

set BUILDNO = $BUILD_NUMBER
$BUILDNO
Residue answered 25/9, 2018 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.