In a Jenkins job I am running a shell script, on success of which
triggering a downstream project in Postbuild.
I have a problem because my shell scripts gets successful in two
scenarios. For Ex let us consider scenario-A and scenario-B.
If script is successful with scenario-A then it should trigger the
downstream project, but if script is successful with scenario-B then
the job should end and should not trigger downstream script.
tried to match Text and use Text Finder plugin, but it makes build
unstable. I don't want the job status to be Unstable or Failed when
scenario-B passes. I am able to successfully match the strings using
few scripting in Execute shell script plugin, but what should I give
to finish the Jenkins job with success status and avoiding the
downstream project when the string matches.
Execute Shell Plugin Contains
cd dir
./myscript
string_name=`cat aaa.log | grep foo`
if [ string_name == "foo" ] then;
\\Command to aviod downstream project
fi
Sounds to me that you'd be better off trying to implement this logic via Build Flows https://wiki.jenkins.io/display/JENKINS/Build+Flow+Plugin?focusedCommentId=60917290 or Pipeline 2.0 https://jenkins.io/doc/book/pipeline/
Build flows is probably closer to what you already have right now (and in itself is kind of a bridge in between traditional jobs and Pipeline 2.0).
Your logic would be sth like (groovy code inside a Build Flow or a Pipeline 2.0 Jenkinsfile):
if (build('scenario-B-Job'))
return
else if build('scenario-A-Job') {
build('downstream-Job')
}
Not sure I get your logic exactly right (you don't mention whether A and B are mutually exclusive or if they can/must run in parallel) but I think you get the idea.
I have a makefile for compiling a static library. This makefile has a rule for compiling a unit test suite associated with the static library. Once the test suite is compiled, a python script is invoked to run the tests and log the results. It looks like this:
unit:
$(MAKE) -C cXbase/unit
python $(TESTS_RUNNER) $(UNIT_TESTS_EXEC) $(UNIT_TESTS_LOG)
I use Python to make the tests invocation portable. Right now, everything works fine: the tests compile and the test runner is called and logs everything properly in Linux and Windows. Only, when a test fail, I would like to stop the whole make process and return an error. More precisely, I would like not to be able to make all or to make unit when a unit test fails (or many).
If a unit test fails, the Python script returns a specific exit code. I would like to be able to capture it in a portable way and have make fail if that exit code is captured.
Would you have any recommendations on how to do this? I have found nothing convincing or portable elsewhere.
Thanks
It seems the solution was way simpler than I imagined. The python exit code reflects directly in make it's exit code. In other words, if the script fails (exit code other than 0), make sees this as a command error and stops.
I had an error in my Python script exit code handling upon tests failure which hid this from me. Now it is solved and it works perfectly.
I found out about this here: Handling exit code returned by python in shell script
I am trying to execute multiple windows batch commands in jenkins one after the other. The problem is that if any the of the project/build fails, it never gonna execute next windows batch commands.
My question is How to execute next windows batch command if previous execution fails?
Help me with this regard.
When you say "multiple windows batch commands", do you mean:
Multiple Execute Windows Batch Command build steps
Or multiple lines of commands within a single build step?
If configuring multiple build steps, you just need to make sure that the last command of the build step does not return anything other than 0. You can do this by adding either of the following as the very last statement in your build step:
either exit /b 0
or echo "All done"
As for multiple lines within the same build step, default implementation of Execute Windows Batch Command does not break if one line/statement fails (which is different from default Execute Shell implementation). As long as the very last statement returns 0, the build step will not fail, and any lines failing in between does not matter.
Once again, you can reference to the above list to make sure that the last line always returns 0
Re "if any the of the project/build fails"
Do you mean "if any of the batch commands fails"?
See Conditional BuildStep Plugin.
In your job config scroll down to:
Build
[ For each command that can fail Add build step at the bottom of this Build section ]
Conditional step (single)
Run? | Not
! | Execute Windows batch commands
Commands | ... your commands ...
[ Click Advanced... ]
On evaluation failure | Fail the build
Builder | Set the build result
Result | Success
Or add just one Conditional step (single) section and write all of your commands into:
Commands | ... your commands ...
Or maybe Conditional step (multiple) is the way to go for you. I haven't used this so far, so I'm not much of a help there.
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?
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.
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.
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=$?
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
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?
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.
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.
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
I compile an NSIS script to a .exe install file. I launch the .exe with command line \S silent option.
Installation performs silently as wanted, but there is exit code 1. Exit code 1 corresponds to case with user choosing cancel on the wizard. However, install is successful and mode is silent (no user interaction). Also, where does this exit code comes from, and how to manually enforce an exit code 0?
I have an idea i could do something in .onInstSuccess function, to enforce an exit code 1 if installation is successful.
Also, ExecWait is capturing the exit code into a variable, but has got no 'set' option.
What would you recommend?
Thanks and regards
Without any sample code it is a bit hard to guess what the problem could be!
You can set a specific exit code with SetErrorLevel.
As far as ExecWait goes, setting anything makes no sense, when it returns the child process has ended. If you want to use the exit code of a child process all you need is to get it:
ExecWait '"c:\foo.exe"' $0
SetErrorLevel $0