Jenkins管道基于执行的批处理文件退出代码失败

huangapple go评论67阅读模式
英文:

Jenkins pipeline failing based on executed batch file exit code

问题

我有一个Jenkins阶段,将根据pycodestyle标准检查存储库中的所有.py文件。

为此,我使用一个.bat文件来执行此命令。但是,如果批处理文件的退出代码不是0,Jenkins流水线也会停止。有没有办法,我可以继续执行Jenkins流水线,无论批处理文件的退出代码是什么?

现在我的阶段看起来是这样的。

```pycodestyle: {
    powershell"""
    .\\venv\\Scripts\\Activate.ps1
    ${WORKSPACE}\\code_analyzer\\check_pycodestyle.bat 2>&1 | tee pycodestyle.log
    """

批处理文件的输出看起来是这样的。

[2023-04-03T09:20:23.748Z] 

[2023-04-03T09:20:23.748Z] _PR-27>pycodestyle --exclude venv,mfile,resource_rc.py --config=_PR-27\code_analyzer\\pep8.config _PR-27\code_analyzer\\.. 

[2023-04-03T09:20:23.748Z] _PR-27\code_analyzer\\..\release\whl_release.py:47:1: E402 module level import not at top of file

[2023-04-03T09:20:24.009Z] _PR-27\code_analyzer\\..\tests\system\test_utils_build_and_install.py:31:1: E402 module level import not at top of file

[2023-04-03T09:20:24.269Z] _PR-27\code_analyzer\\..\utils\helper.py:45:1: W391 blank line at end of file

script returned exit code 1 

我希望即使退出代码为1,也可以继续执行Jenkins流水线。


<details>
<summary>英文:</summary>

I have a jenkins stage that will check all the .py files in the repo according to the pycodestyle standard. 

For this purpose I am using a .bat file to execute this command. But, if the exit code for the batch file is not 0, the jenkins pipeline will also stop. Is there any way, I can continue the jenkins pipeline execution irrespective of what the exit code is for my batch file?

right now my stage looks something like this.

pycodestyle: {
powershell"""
.\venv\Scripts\Activate.ps1
${WORKSPACE}\code_analyzer\check_pycodestyle.bat 2>&1 | tee pycodestyle.log
"""


The output of the batch file looks something like this.

```none
[2023-04-03T09:20:23.748Z] 

[2023-04-03T09:20:23.748Z] _PR-27&gt;pycodestyle --exclude venv,mfile,resource_rc.py --config=_PR-27\code_analyzer\\pep8.config _PR-27\code_analyzer\\.. 

[2023-04-03T09:20:23.748Z] _PR-27\code_analyzer\\..\release\whl_release.py:47:1: E402 module level import not at top of file

[2023-04-03T09:20:24.009Z] _PR-27\code_analyzer\\..\tests\system\test_utils_build_and_install.py:31:1: E402 module level import not at top of file

[2023-04-03T09:20:24.269Z] _PR-27\code_analyzer\\..\utils\helper.py:45:1: W391 blank line at end of file

script returned exit code 1 

I want to continue the execution of the jenkins pipeline even after the exit code is 1.

答案1

得分: 1

你可以将这个放在catchError步骤中,或者使用基本的try-catch块(如果使用脚本语法)。

关于此的更多信息,请查看文档链接。

英文:

You can put this in a catchError step or use a basic try-catch block, (if using scripted syntax).

More info on this documentation url.

答案2

得分: 0

KiiroiSenko的有用答案 指向了一个_Jenkins_解决方案。

然而,问题在_PowerShell_方面可以更简单地解决 - 只需在最后加上 exit 0 作为最后一个语句:

.\\venv\\Scripts\\Activate.ps1
${WORKSPACE}\\code_analyzer\\check_pycodestyle.bat 2&gt;&amp;1 | tee pycodestyle.log
exit 0

也就是说:

  • 你正在调用powershell.exe,Windows PowerShell CLI,使用隐含的-Command参数,它接受一个或多个要执行的PowerShell语句,并根据以下规则确定要报告给其调用者的退出代码:

  • 除非使用exit语句_显式_控制进程退出代码,否则它是_隐式_由执行的最后一个语句的_成功状态_来确定退出代码的,根据自动的$?变量的值:

    • 如果$?$true,表示成功的语句结果,则进程退出代码变为0;否则,它是11

      • 请注意,即使最后一个语句是调用报告了_不同的_非零退出代码的外部程序调用,也会反映在自动的$LASTEXITCODE变量
    • 因此:

      • 如果你想报告$LASTEXITCODE的值,以中继由最近执行的外部程序报告的具体退出代码,使用**exit $LASTEXITCODE**
      • 相反 - 如在你的情况下 - 如果你想通过将0设置为退出代码_无条件地_向调用者发出成功信号,使用**exit 0**
  • 请注意,如果你使用**-File CLI参数**而不是-Command - 以便调用一个脚本_文件_(*.ps1),退出代码逻辑将发生变化:

    • 仅会尊重显式的指定退出代码的exit调用。
    • 否则:
      • 无论脚本执行期间是否发生错误,都会报告退出代码0
      • 除非_发生_致命(终止脚本)错误,在这种情况下报告1

要获取背景信息,请参阅此答案

1 如果语句是对_外部程序_的调用或涉及到其作为管道的一部分,并且管道中没有其他命令发出错误条件,那么其进程退出代码,如$LASTEXITCODE中反映的那样,决定了$?的值:0$?设置为$true,任何非零值都将其设置为$false
在_Windows PowerShell_中(但不再在PowerShell(Core)7+中),即使存在一个2&gt;&amp;1重定向并且存在实际的stderr输出,也可能会(不适当地)导致$false,即使退出代码为0

英文:

KiiroiSenko's helpful answer points to a Jenkins solution.

The problem can more simply be solved on the PowerShell side, however - just add exit 0 as the last statement:

    powershell&quot;&quot;&quot;
    .\\venv\\Scripts\\Activate.ps1
    ${WORKSPACE}\\code_analyzer\\check_pycodestyle.bat 2&gt;&amp;1 | tee pycodestyle.log
    exit 0
    &quot;&quot;&quot;

That is:

  • You're invoking powershell.exe, the Windows PowerShell CLI, with the implied -Command parameter, which accepts one or more PowerShell statements to execute, and determines the exit code to report to its caller as follows:

  • Unless you use an exit statement to control the process exit code explicitly, it is the success status of the last statement executed that implicitly determines the exit code, based on the value of the automatic $? variable:

    • If $? is $true, indicating a successful statement outcome, the process exit code becomes 0; otherwise, it is 1.<sup>1</sup>

      • Note that it is 1 even if the last statement is an external-program call that reported a different nonzero exit code, as reflected in automatic $LASTEXITCODE variable
    • Therefore:

      • If you want to report the value of $LASTEXITCODE, to relay the specific exit code reported by the most recently executed external program, use exit $LASTEXITCODE
      • Conversely - as in your case - if you want to unconditionally signal success to the caller by setting 0 as the exit code, use exit 0
  • Note that if you were to use the -File CLI parameter instead of -Command - so as to invoke a script file (*.ps1), the exit-code logic changes:

    • Only explicit exit calls specifying an exit code are honored.
    • Otherwise:
      • Irrespective of whether errors occurred during the script's execution, exit code 0 is reported.
      • Except if a fatal (script-terminating) error occurs, in which case 1 is reported.

For background information, see this answer.


<sup>1 If a statement is a call to an external program or involves one as part of the pipeline and no other commands in the pipeline signal an error condition, its process exit code, as reflected in $LASTEXITCODE, determines the value of $?: 0 sets $? to $true, and any nonzero value sets it to $false.
In Windows PowerShell (but no longer in PowerShell (Core) 7+), $false can (inappropriately) also result even with an exit code of 0, namely if a 2&gt;&amp;1 redirection is present and there is actual stderr output.</sup>

huangapple
  • 本文由 发表于 2023年4月4日 15:50:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定