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.
[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.
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 becomes 0
; otherwise, it is 1
.[1]
1
even if the last statement is an external-program call that reported a different nonzero exit code, as reflected in automatic $LASTEXITCODE
variableTherefore:
$LASTEXITCODE
, to relay the specific exit code reported by the most recently executed external program, use exit $LASTEXITCODE
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:
exit
calls specifying an exit code are honored.0
is reported.1
is reported.
throw
statement or via the -ErrorAction Stop
common parameter or the $ErrorActionPrefererence = 'Stop'
preference variable.For background information, see this answer.
[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.