Jenkins 与 pylint 导致构建失败

发布于 2024-12-03 16:38:27 字数 182 浏览 2 评论 0原文

我添加了一个构建步骤来执行 Python 脚本。
在此脚本中,使用 lint.Run(..args) 调用 pylint 来检查代码。
该脚本有效,但最终构建失败,并显示唯一的错误消息:

构建步骤“执行 Python 脚本”将构建标记为失败

有人知道为什么会发生这种情况吗?

I added a build step to execute a Python script.
In this script pylint is called with the lint.Run(..args) to check the code.
The script works but in the end, the build fails with the only error message:

Build step 'Execute Python script' marked build as failure

Someone has an idea why this happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

与君绝 2024-12-10 16:38:28

今天遇到了这个(虽然没有使用詹金斯)。

就我而言,这是因为 Pylint 如何在其退出代码中编码 fatal-error-warning-refactor-convention-usage 信息:https://docs.pylint.org/en/1.6.0/run.html#exit-codes

我的修复:(

#!/usr/bin/env sh

# Wraps Pylint invocation to produce shell-friendly exit codes
# Because Pylint exit codes are weird:
# https://docs.pylint.org/en/1.6.0/run.html#exit-codes

PYTHON_EXECUTABLE=python
if [ ! -z $PYTHON_ENV ]; then
    PYTHON_EXECUTABLE="$PYTHON_ENV/bin/python"
fi

${PYTHON_EXECUTABLE} -m pylint $@

PYLINT_EXIT_CODE=$?

exit $(($PYLINT_EXIT_CODE % 4))

要点:https://gist.github.com/nkashy1/ae59d06d4bf81fb72047fcd390d08903)

Ran into this today (although not using Jenkins).

In my case it was because of how Pylint encodes fatal-error-warning-refactor-convention-usage info in its exit code: https://docs.pylint.org/en/1.6.0/run.html#exit-codes

My fix:

#!/usr/bin/env sh

# Wraps Pylint invocation to produce shell-friendly exit codes
# Because Pylint exit codes are weird:
# https://docs.pylint.org/en/1.6.0/run.html#exit-codes

PYTHON_EXECUTABLE=python
if [ ! -z $PYTHON_ENV ]; then
    PYTHON_EXECUTABLE="$PYTHON_ENV/bin/python"
fi

${PYTHON_EXECUTABLE} -m pylint $@

PYLINT_EXIT_CODE=$?

exit $(($PYLINT_EXIT_CODE % 4))

(Gist: https://gist.github.com/nkashy1/ae59d06d4bf81fb72047fcd390d08903)

感情洁癖 2024-12-10 16:38:28

Pylint 期望被分析的代码 100% 完美。
即使代码警告也可能导致以非零状态代码退出。
尝试按照 Pylint 建议修复您的代码并评分 10/10。

希望这有帮助。

Pylint expect the code being analyzed to be 100% perfect.
Even code warning may cause exit with non zero status code.
Try to fix your code as Pylint suggest and rate 10/10.

Hope this helps.

凯凯我们等你回来 2024-12-10 16:38:28

我同意@dmeister,但对于管道代码(Jenkinsfile),我建议尝试/捕获,然后解析错误。通过这种方式,您可以确定是否只是从 pylint 获取状态位(请参阅 Pylint 文档), pylint 是否报告使用错误,或者是否发生灾难性失败:

try {       
    sh 'pylint --output-format=parseable my_module'
} catch ( pylint_rc ) {
    // pylint_rc will be of the form
    // "hudson.AbortException: script returned exit code NN"
    // where NN is 1-63 and represents bit field;
    // bits 0-4 indicate lint-ish issues in analyzed code,
    // bit 5 indicates pylint usage error
    echo "pylint_rc= \'$pylint_rc\'"
    String rc = "$pylint_rc"
    String code = rc.split()[5]
    echo "Isolated return code string value $code"
    int value = code.toInteger()

    // catastrophic/crash error returns a 1; else there is a pylint return code
    int error_bits_code = value & 0x20
    int lint_bits_code = value & 0x1f
    echo "pylint error_bits_code=$error_bits_code ; lint_bits_code=$lint_bits_code"
    if ( (value == 1) || (error_bits_code != 0) ) {
        currentBuild.result = "FAILURE"
        throw pylint_rc
    }
}

向 groovy 纯粹主义者致歉 - groovy 不是我的事,所以我确信这可以改进 - 请告诉我。有一个已知的漏洞:如果 pylint 仅检测到“致命”类型的错误(位 0)并且没有任何其他类型的问题(位 1-4 未设置),则此代码将错误地引发异常。但我的代码标记了大量问题,所以这对我来说不是问题。对于具有高超技术的人来说,修复(?解析错误消息?)可能是微不足道的。

I agree with @dmeister, but with pipeline code (Jenkinsfile) I suggest a try/catch and then parsing the error. This way you can determine if you're just getting status bits from pylint (see the Pylint docs), whether pylint reports a usage error, or whether there was a catastrophic fail:

try {       
    sh 'pylint --output-format=parseable my_module'
} catch ( pylint_rc ) {
    // pylint_rc will be of the form
    // "hudson.AbortException: script returned exit code NN"
    // where NN is 1-63 and represents bit field;
    // bits 0-4 indicate lint-ish issues in analyzed code,
    // bit 5 indicates pylint usage error
    echo "pylint_rc= \'$pylint_rc\'"
    String rc = "$pylint_rc"
    String code = rc.split()[5]
    echo "Isolated return code string value $code"
    int value = code.toInteger()

    // catastrophic/crash error returns a 1; else there is a pylint return code
    int error_bits_code = value & 0x20
    int lint_bits_code = value & 0x1f
    echo "pylint error_bits_code=$error_bits_code ; lint_bits_code=$lint_bits_code"
    if ( (value == 1) || (error_bits_code != 0) ) {
        currentBuild.result = "FAILURE"
        throw pylint_rc
    }
}

Apologies to groovy purists - groovy isn't my thing, so I'm sure this can be improved on - let me know. There is one known hole: if pylint detects only "fatal"-type errors (bit 0) and no other issues of any kind (bits 1-4 are not set) then this code will incorrectly throw an exception. But my code flags tons of issues, so that's not a problem for me. The fix (?parse error msg?) might be trivial for someone with groovy chops.

伊面 2024-12-10 16:38:28

pylint 给出的不是零退出,但没有明显的错误消息。您可能需要跟踪以查看哪个文件 pylint 失败。并查看文件。

对我来说,我添加一个带有 init.py 文件的空目录,并且 pylint 给出相同的错误

The pylint give not zero exit, but without obviously error message. you may need to trace to see which file pylint got failed. and take a look in the file.

For me, I add an empty directory with init.py file, and pylint give same error

泪眸﹌ 2024-12-10 16:38:27

您也可以简单地放置一个

pylint ||退出0

shell 命令行中的 。通过检查 pylint 的结果,Pylint 插件无论如何都会使构建失败。

You can also simply put a

pylint || exit 0

in the shell cmdline. The Pylint plugin will fail the build anyway by checking the result of pyllint.

紫南 2024-12-10 16:38:27

在 Pylint 1.9.3 中,有一个 --exit-zero 标志。

https://github.com/PyCQA/pylint/blob/1.9/ChangeLog#L47

In Pylint 1.9.3, there is a --exit-zero flag.

https://github.com/PyCQA/pylint/blob/1.9/ChangeLog#L47

孤城病女 2024-12-10 16:38:27

即使仅发现一个小的警告问题,Pylint 也有返回非零退出代码的令人不愉快的行为。仅当一切正常时,才会返回 0(请参阅手册页)。

由于非零代码通常表示错误,因此 Jenkins 构建失败。

我看到有两种方法可以克服这个问题:

  • 在 pylint 周围使用一个始终返回 0 的小脚本。然后 jenkins 就不会因为 pylint 而失败。我使用一个小的 python 脚本,在之后使用 os.system() 和 sys.exit(0) 调用 pylint 。您可以将其视为覆盖 pylint 的错误代码。
  • 修补木柱。例如,在我的 Linux 系统上,sys.exit() 调用位于文件 /usr/lib/pymodules/python2.6/pylint/lint.py 中

Pylint has the unpleasant behavior to return a non-zero exit code even only if a small warning issue was found. Only when everything was fine, 0 is returned (see man page).

As usually a non-zero code denotes an error, Jenkins fails the build.

I see two ways to overcome this:

  • Use a small script around pylint that always returns 0. Then jenkins will not fail because of pylint. I use a small python script calling pylint with os.system() and sys.exit(0) after than. You can see it as overriding the error code of pylint.
  • Patch pylint. For example, on my Linux system the sys.exit() call is in the file /usr/lib/pymodules/python2.6/pylint/lint.py
嘿咻 2024-12-10 16:38:27

看来您的 pylint 执行退出时状态为非零(缺少脚本、错误选项...),也许您退出脚本时引发异常或 sys.exit(something_else_than_zero)

it seems that your pylint execution exit with a non-zero status (missing script, bad options...), maybe you exit the script with an exception raised or a sys.exit(something_else_than_zero)

梦在深巷 2024-12-10 16:38:27

最近的 rylint 有不调用 sys exit 的选项

lint.Run(args, exit=False, **kwargs)

Recent rylint have option for not calling sys exit

lint.Run(args, exit=False, **kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文