我有一个 python 脚本在 Jenkins 的构建阶段运行,位于执行 shell 区域。
问题是,如果脚本失败,我仍然认为构建成功。我确实检查过,python 使用了类似于 shell 命令返回代码的东西(用 $ 调用的那个?),尽管我不知道在哪里放置调用以在 python 脚本中触发“任何”失败返回代码
import sys
..... code here
....functions
sys.exit(-1)
Run Code Online (Sandbox Code Playgroud)
我需要返回 sys.exit(-1),但是你把它放在 python 代码中的哪里?到目前为止,我只能使用 try 块来处理它,并且在异常部分我放置了 sys.exit(-1),但这添加了大量代码,因为我在脚本中有很多函数。
是否有一个我可以使用的全局位置来触发故障,以便 Jenkins 作业失败?
def main():
try:
do_the_work()
sys.exit(0) # success
except:
# insert code to log and debug the problem
sys.exit(-1)
Run Code Online (Sandbox Code Playgroud)
换句话说:如果do_the_work返回,sys.exit(0). 如果do_the_work引发任何它本身不处理的异常,sys.exit(-1). 在里面do_the_work,除非成功否则不要返回。例如,在任何不可恢复的错误状态上引发异常
class DoSomethingError( exception)
...
ok = do_something()
if not ok:
print ("do_something error return")
raise DoSomethingError
Run Code Online (Sandbox Code Playgroud)
日志记录和调试:搜索堆栈溢出以获取有关如何从捕获的异常中获取和记录错误回溯的 python 答案。
| 归档时间: |
|
| 查看次数: |
10414 次 |
| 最近记录: |