-1

i have a python script which is called from jenkins job shell script. This python script could fail for multiple reasons. Based on the failure i need to catch the correct failure reason. I want this reason to be sent back to jenkins shell some how and i will post this error message through an email or some other means of notification. I tried setting a environment variable and access it in post build. But that doesn't seem to work. Any help appreciated. Below is the sample text code

#!/usr/bin/python
a=False
b=True
if(a):
    raise exception("a is true")
if(b):
    raise exception("b is true")

I need this "a is true" or "b is true" response sent back to jenkins shell and to fail the job.

Stoner
  • 846
  • 1
  • 10
  • 30
RK3
  • 1,221
  • 9
  • 26
  • 37
  • what have you tried so far? Because if your script throws an exception, it should result in failure on jenkins end – Waqas Sep 19 '19 at 07:50

1 Answers1

0

Do you need to exit with exception? it will mark Jenkins job failed.

Better to exit python script with some message or code except 1 and the handle the message or code at Jenkins side.

Now you modify the python script

#!/usr/bin/python
a=False
b=True
if(a):
    print ("a is true")
    exit(0)
if(b):
    print ("b is true")
    exit(0)

In the above, your job will not be marked failed and you will able to handle the job status on Jenkins instead from the python script.

In Jenkins bash script

script_response=$(python ab.py)

if [ "${script_response}" == "b is true" ]; then
   echo "B is true"
   exit 0
 else
   echo "B is flase"
   exit 1
fi
Adiii
  • 54,482
  • 7
  • 145
  • 148