powershellbatch-filejenkins-pipelinepycodestyle

Jenkins pipeline failing based on executed batch file exit code


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.


Solution

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

    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.