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 0as the last statement:That is:
You're invoking
powershell.exe, the Windows PowerShell CLI, with the implied-Commandparameter, 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
exitstatement 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.[1]1even if the last statement is an external-program call that reported a different nonzero exit code, as reflected in automatic$LASTEXITCODEvariableTherefore:
$LASTEXITCODE, to relay the specific exit code reported by the most recently executed external program, useexit $LASTEXITCODE0as the exit code, useexit 0Note that if you were to use the
-FileCLI parameter instead of-Command- so as to invoke a script file (*.ps1), the exit-code logic changes:exitcalls specifying an exit code are honored.0is reported.1is reported.throwstatement or via the-ErrorAction Stopcommon 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$?:0sets$?to$true, and any nonzero value sets it to$false.In Windows PowerShell (but no longer in PowerShell (Core) 7+),
$falsecan (inappropriately) also result even with an exit code of0, namely if a2>&1redirection is present and there is actual stderr output.