【问题标题】:Triggering Jenkins build failure when there is Python syntax error出现 Python 语法错误时触发 Jenkins 构建失败
【发布时间】:2021-06-09 22:38:25
【问题描述】:

我是 Jenkins 的新手,所以我想要一些关于如何在 Python 语法或编译错误时触发 Jenkins 构建失败的建议。我目前有一个管道脚本,可以签出指定的存储库,然后运行 ​​Python 文件“python pycompile.py”。 pycompile.py 的目的是使用一个名为 compileall (https://docs.python.org/3/library/compileall.html#module-compileall) 的模块来编译目录中的所有 Python 源文件,并在其中一个 Python 文件中遇到错误时发出警告。假设我有一个文件 file.py,它有语法错误,并且在运行“python pycompile.py”时收到警告。但是,Jenkins 构建仍然成功,因为 pycompile.py 没有问题,但 file.py 没有问题。当“python pycompile.py”在签出的 repo 目录中检测到 Python 文件错误时,如何触发 Jenkins 构建失败?

【问题讨论】:

    标签: python jenkins jenkins-pipeline


    【解决方案1】:

    如果 Jenkins 的返回状态为 0,它将始终成功。 如果您希望 jenkins 停止,则脚本将以other than 0 状态退出。例如:exit 1
    因此,在您的情况下,您有 2 个选项:

    1. 如果遇到语法错误,执行 python pycompile.py 应返回退出状态为 other than 0
    2. 如果上述模块在错误时无法返回除0 以外的退出状态,那么您可以编写自己的脚本并检查步骤 1 中是否有任何错误,如果有错误,那么您可以返回 0 以外的退出。在这种情况下,Jenkins 将失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      相关资源
      最近更新 更多