cyberbemon
cyberbemon

Reputation: 3190

Making a job fail in jenkins

This question might sound weird, but how do I make a job fail?

I have a python script that compiles few files using scons, and which is running as a jenkins job. The script tests if the compiler can build x64 or x86 binaries, I want the job to fail if it fails to do one of these.

For instance: if I'm running my script on a 64-bit system and it fails to compile a 64-bit. Is there something I can do in the script that might cause to fail?

Upvotes: 9

Views: 19788

Answers (6)

Casterial
Casterial

Reputation: 31

Rather than making Jenkins run the bash, have your Python script do it with

Try{
  subprocess.check_call(cmd, args, shell=True)
}
catch{
  raise
}

subprocess.check_call using stderr output which also better communicates with Jenkins directly and allows other plugins like Failure Cause Management to read and fail builds based on regex expressions. Any time this exception is triggered it will raise the exit code and error which will fail the job thanks to Jenkins failing on any non-zero return code.

You can read more about check_call here

Exiting with -1 or 1 doesn't work because you won't see the true error.

Upvotes: 0

P_M
P_M

Reputation: 51

In the same use case, but in a different approach to handle.

In my case, I used to build steps of shell script because I needed to execute multiple steps. Like git pull, change branch, active virtual environment, execute python script, deactivate the virtual environment, and so on.

I tried to raise an Exception for an error or use sys.exit code in code file. And it works only for a python script. Not for the Jenkins job. Because there was some step executed after python script, will execute the rest of the code. And the end of the Job Jenkins status Success.

So, I update the shell script step that can fail the Jenkins job. by the following steps.

  • Add raise an Exception for an error or use sys.exit code in Python code file.
  • Next check the exit code of the script if exit code is None Zero then exit 1 the shell script and job failed.
.
.
.

python3 python_code.py

EXIT_STATUS=$?
echo "Exit status : $EXIT_STATUS"
if [ "$EXIT_STATUS" -ne "0" ]
then
  exit 1
fi

.
.
.

Upvotes: 0

Jaber
Jaber

Reputation: 423

I came across this as a noob and found the accepted answer is missing something if you're running python scripts through a Windows batch shell in Jenkins.

In this case, Jenkins will only fail if the very last command in the shell fails. So your python command may fail but if there is another line after it which changes directory or something then Jenkins will believe the shell was successful.

The solution is to check the error level after the python line:

if %ERRORLEVEL% NEQ 0 (exit)

This will cause the shell to exit immediately if the python line fails, causing Jenkins to be marked as a fail because the last line on the shell failed.

Upvotes: 1

Aesthete
Aesthete

Reputation: 18848

You can raise an exception at any point. If it goes unhandled, the application will stop. You don't even need to specify which exception you're raising.

if not yourTestHere:
    raise

If you want to specify a message, you can just raise a standard exception.

from exceptions import Exception
if not yourTestHere:
    raise Exception("Script failed because of bla bla bla")

Upvotes: 3

Wouter Devolder
Wouter Devolder

Reputation: 71

The important thing is that the run of your python script is the last step of your jenkins job when it throws an exception or a non-zero exception code. If you run anything else after that, even echo "my job is done" will change the error code.

For me the following fails my jenkins job

echo "exit(1)" >> test.py
python test.py

When I do the following my jenkins job shows up as successful

echo "exit(1)" >> test.py
python test.py
echo "This changes my exit code back to 0, which is successful"

Upvotes: 7

Y__
Y__

Reputation: 1777

If your script exit with a non-zero status the build should fail.

import sys
sys.exit(-1)

Upvotes: 7

Related Questions