Search code examples
pythongitlab-ci

gitlab CI: powershell job doesnt fail on python exception


I have a simple gitlab config:

variables:
  P7_TESTING_INSTALLATION_PATH: D:\Logos_block_gitlab_runner\pSeven-v2024.05

stages:
  - cleanup
  - installation

cleanup_build:
  tags:
    - block_autotest
  variables:
    ErrorActionPreference: stop
  allow_failure: false
  stage: cleanup
  script:
    - Invoke-Expression -Command "$env:P7_TESTING_INSTALLATION_PATH\client\p7batch.exe --log-level=error --run $env:CI_PROJECT_DIR\autotest\jobs\_clear.py"
    - if(!$?) { Exit $LASTEXITCODE }

install_block:
  tags:
    - block_autotest
  variables:
    ErrorActionPreference: stop
  allow_failure: false
  stage: installation
  script:
    - Invoke-Expression -Command "$env:P7_TESTING_INSTALLATION_PATH\client\p7batch.exe --log-level=error --run $env:CI_PROJECT_DIR\autotest\jobs\_setup_block.py"

Both files _clear.py and _setup_block.py not exists, and I see the proper exception in log:

@ [Terminal]     raise IOError("No such file: '{}'".format(filepath))

@ [Terminal] IOError: No such file: 'D:\Logos_block_gitlab_runner\GitLab-Runner\builds\vDLVWCgmH\0\aero\logos_userblock\autotest\jobs\_clear.py'

[32;1m$ if(!$?) { Exit $LASTEXITCODE }[0;m
section_end:1719260050:step_script
[0K[32;1mJob succeeded[0;m

But I still receive 'job succeeded'!!!

I have added ErrorActionPreference: stop as well as allow_failure: false but still I receive success on python exception...


Solution

  • Invoke-Expression does not evaluate nor returns the status code of what it executes. It's a "fire-and-forget" operation. It does not even set $lastExitCode (or $? for that matter):

    pwsh> Invoke-Expression non-existing
    non-existing: The term 'non-existing' is not recognized as a name of a cmdlet, function, script file, or executable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    pwsh> echo $lastExitCode
    0
    pwsh> Invoke-Expression 1/0
    RuntimeException: Attempted to divide by zero.
    pwsh> echo $lastExitCode
    0
    pwsh> Invoke-Expression 1/0
    RuntimeException: Attempted to divide by zero.
    pwsh> echo $?
    True
    

    In order for the pipeline to fail you need to use a tool that checks the output of the executed command, such as Invoke-Command:

    pwsh> Invoke-Command non-existing
    Invoke-Command: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.
    pwsh> echo $?
    False