gpt4 book ai didi

python - 当 python 脚本中的任何内容失败时,在 Jenkins 中触发失败

转载 作者:太空宇宙 更新时间:2023-11-04 00:41:35 24 4
gpt4 key购买 nike

我有一个 python 脚本在 Jenkins 的构建阶段运行,在执行 shell 区域。

问题是如果脚本失败,我仍然认为构建成功。我确实检查过并且 python 使用了类似于 shell 命令(你用 $? 调用的那个)的返回码的东西,尽管我无法弄清楚将调用放在哪里以触发 python 脚本中的“任何”失败那个返回码

import sys

..... code here

....functions

sys.exit(-1)

我需要返回 sys.exit(-1),但是你把它放在 python 代码的什么地方?到目前为止,我只能使用 try block 来处理它,在异常部分我放置了 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)

换句话说:如果 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

记录和调试:搜索堆栈溢出以获取有关如何从捕获的异常中获取和记录错误回溯的 python 答案。

关于python - 当 python 脚本中的任何内容失败时,在 Jenkins 中触发失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com