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 python code file. And it works only for a python script. Not for the Jenkins job. Because there was some step executed after python script, jenkins 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 python script if exit code is None Zero then exit 1 the shell script and jenkins job failed.
.
.
.
python3 python_code.py
EXIT_STATUS=$?
echo "Exit status : $EXIT_STATUS"
if [ "$EXIT_STATUS" -ne "0" ]
then
exit 1
fi
.
.
.
pythonpython3jenkinsshellscript