4

I have a python script running in the build phase of Jenkins, in the execute shell area.

The problem is that if the script fail, I still see the build as successful. I did check and python use something similar to the return code for a shell command (the one you call with $?), although I can't figure out where to put the call to have "any" failure in the python script to trigger that return code

import sys

..... code here

....functions

sys.exit(-1)

I need to return sys.exit(-1), but where do you put it in the python code? So far I can only handle it using try blocks, and in the exception part I put the sys.exit(-1), but this add a lots of code, since I have a lots of functions in the script.

Is there a global location that I can use, to trigger the failure, so the Jenkins job fail?

  • 1
    I remember having a similar issue. Probably if something in your functions fail, it will never reach the `sys.exit()` part. For a starters you can use something like a `main() function` where all the logic is wrapped together. Then add the `if __name__ == "__main__":` part and there do something like `sys.exit(main())` Then this will propagate the errors. I think that is how I solved it back then – Rik Jan 18 '17 at 08:44
  • 1
    The build will fail if one of the shell blocks executes non-zero, so the problem lies in your python script not propagating the error. You _can_ check this by running the python script and directly echoing `$?` – Rik Jan 18 '17 at 08:47
  • Is there any reason for using `sys.exit(-1)`?can you use `sys.exit(1)`. – RejeeshChandran Jan 18 '17 at 11:23
  • This question is a repeat, please go through http://stackoverflow.com/questions/22814559/how-when-does-execute-shell-mark-a-build-as-failure-in-jenkins and http://stackoverflow.com/questions/13400445/jenkins-build-script-exits-after-google-test-execution/13404608#13404608 – RejeeshChandran Jan 18 '17 at 11:27
  • Not a repeat; I am not asking how to trigger a failure; I am asking how to implement sys.exit in my script; which is quite different. –  Jan 18 '17 at 23:20
  • @Rik, you were spot on; I can just ditch sys.exit and use a standard shell check for $? to get the result of the script. Feel free to add it as solution; although it was not what I was looking for; but it works :) –  Jan 18 '17 at 23:21

2 Answers2

5
def main():
    try:
        do_the_work()
        sys.exit(0) # success
    except:
        # insert code to log and debug the problem
        sys.exit(-1)

In words: if do_the_work returns, sys.exit(0). If do_the_work raises any exception which it does not itself handle, sys.exit(-1). Inside do_the_work, do not return except on success. raise an exception on any unrecoverable error state, for example

class DoSomethingError( exception)
...

ok = do_something()
if not ok:
    print ("do_something error return")
    raise DoSomethingError

Logging and debugging: search stack overflow for python answers concerning how to obtain and log an error traceback from a caught exception.

nigel222
  • 7,582
  • 1
  • 14
  • 22
  • I did conpensate for the -1; probably they voted based on the misconcept that I was asking how to trigger a failure in Jenkins; while I was looking for a way to use sys.exit so Jenkins knows that the python script did fail. Thanks for the reply. This works, although I just learned that I can still check for $? in the shell commands of the build step, for the output of the python script, without even need to change my source. –  Jan 18 '17 at 23:24
0

This question is a repeat, please see the below similar questions which are already answered.

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

Jenkins Build Script exits after Google Test execution

Community
  • 1
  • 1
RejeeshChandran
  • 4,168
  • 3
  • 21
  • 32