1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775

2 Answers2

1

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • just using try and catch and changing the status of the ```currentBuild.status = 'SUCCESS'``` worked. Thank you for the link and suggestion @KiiroiSenko – programmer_04_03 Apr 04 '23 at 08:32
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 becomes 0; otherwise, it is 1.[1]

      • 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.
        • 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.

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I tried with ```exit 0``` the problem with this is, I believe that it will set the exit code to 0 but since it is exiting the powershell completely, the following stages in the jenkinsfile will never be executed. Please correct me if I am wrong here. – programmer_04_03 Apr 04 '23 at 13:35
  • @programmer_04_03, a `powershell.exe` CLI call with either (possibly implied) `-Command` or `-File` _always_ exits - whether you use `exit` or not: the specified statements / script is executed, and the process terminates (the only exception is if you also pass `-NoExit`, which you obviously shouldn't do in a CI/CD environment). In other words: `exit 0` should do what you want, without unwanted side effects. – mklement0 Apr 04 '23 at 13:44