英文:
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>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>&1 | tee pycodestyle.log
exit 0
也就是说:
-
你正在调用
powershell.exe
,Windows PowerShell CLI,使用隐含的-Command
参数,它接受一个或多个要执行的PowerShell语句,并根据以下规则确定要报告给其调用者的退出代码: -
除非使用
exit
语句_显式_控制进程退出代码,否则它是_隐式_由执行的最后一个语句的_成功状态_来确定退出代码的,根据自动的$?
变量的值:-
如果
$?
是$true
,表示成功的语句结果,则进程退出代码变为0
;否则,它是1
。1- 请注意,即使最后一个语句是调用报告了_不同的_非零退出代码的外部程序调用,也会反映在自动的
$LASTEXITCODE
变量中
- 请注意,即使最后一个语句是调用报告了_不同的_非零退出代码的外部程序调用,也会反映在自动的
-
因此:
- 如果你想报告
$LASTEXITCODE
的值,以中继由最近执行的外部程序报告的具体退出代码,使用**exit $LASTEXITCODE
** - 相反 - 如在你的情况下 - 如果你想通过将
0
设置为退出代码_无条件地_向调用者发出成功信号,使用**exit 0
**
- 如果你想报告
-
-
请注意,如果你使用**
-File
CLI参数**而不是-Command
- 以便调用一个脚本_文件_(*.ps1
),退出代码逻辑将发生变化:- 仅会尊重显式的指定退出代码的
exit
调用。 - 否则:
- 无论脚本执行期间是否发生错误,都会报告退出代码
0
。 - 除非_发生_致命(终止脚本)错误,在这种情况下报告
1
。- 当脚本调用在根本上失败时(由于语法错误或有效的执行策略阻止脚本执行,例如),或者当脚本通过
throw
语句或通过-ErrorAction Stop
通用参数或$ErrorActionPrefererence = 'Stop'
偏好变量显式触发时。
- 当脚本调用在根本上失败时(由于语法错误或有效的执行策略阻止脚本执行,例如),或者当脚本通过
- 无论脚本执行期间是否发生错误,都会报告退出代码
- 仅会尊重显式的指定退出代码的
要获取背景信息,请参阅此答案。
1 如果语句是对_外部程序_的调用或涉及到其作为管道的一部分,并且管道中没有其他命令发出错误条件,那么其进程退出代码,如$LASTEXITCODE
中反映的那样,决定了$?
的值:0
将$?
设置为$true
,任何非零值都将其设置为$false
。
在_Windows PowerShell_中(但不再在PowerShell(Core)7+中),即使存在一个2>&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"""
.\\venv\\Scripts\\Activate.ps1
${WORKSPACE}\\code_analyzer\\check_pycodestyle.bat 2>&1 | tee pycodestyle.log
exit 0
"""
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 becomes0
; otherwise, it is1
.<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
- Note that it is
-
Therefore:
- If you want to report the value of
$LASTEXITCODE
, to relay the specific exit code reported by the most recently executed external program, useexit $LASTEXITCODE
- Conversely - as in your case - if you want to unconditionally signal success to the caller by setting
0
as the exit code, useexit 0
- If you want to report the value of
-
-
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.- A fatal error occurs when script invocation fails fundamentally (due to syntax errors or the effective execution policy preventing script execution, for instance) or when one is triggered explicitly by the script, via a
throw
statement or via the-ErrorAction Stop
common parameter or the$ErrorActionPrefererence = 'Stop'
preference variable.
- A fatal error occurs when script invocation fails fundamentally (due to syntax errors or the effective execution policy preventing script execution, for instance) or when one is triggered explicitly by the script, via a
- Irrespective of whether errors occurred during the script's execution, exit code
- Only explicit
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>&1
redirection is present and there is actual stderr output.</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论