2

I have a GitLab CI/CD Job with the following definition:

compile:
  stage: compile
  tags:
    - windows
    - powershell
    - bl653_8dc0_1053
  artifacts:
    paths:
      - main.linenumbers.uwc
  script:
    - XComp_BL653_8DC0_1053.exe .\main.linenumbers.sb
    - Test-Path -Path .\main.linenumbers.uwc

When the job executes, the XComp_BL653_8DC0_1053.exe application fails and returns exit code 7. However, the build still succeeds even though there was a non-zero exit code and no artifacts.

Executing "step_script" stage of the job script
00:02
$ XComp_BL653_8DC0_1053.exe .\main.linenumbers.sb
OnEvent  EVTMR2              call HandlerTimer2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compile Error: (0x0453) TOK_UNKNOWN_EVENTFUNC
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
File   : main.linenumbers.sb
Line   : 110
Source : OnEvent  EVTMR2              call HandlerTimer2
       : ----------------------------------^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uploading artifacts for successful job
00:01
Version:      14.3.2
Git revision: e0218c92
Git branch:   14-3-stable
GO version:   go1.13.8
Built:        2021-09-30T16:11:30+0000
OS/Arch:      windows/amd64
Uploading artifacts...
Runtime platform                                    arch=amd64 os=windows pid=7216 revision=e0218c92 version=14.3.2
WARNING: main.linenumbers.uwc: no matching files   
ERROR: No files to upload                          
Job succeeded

I can see that it never runs the Test-Path line, so it is correctly exiting when the non-zero exit code happens, but why is it saying the build passes?

I'm using GitLab EE version 14.9. My runner is a PowerShell Executer on Windows 10.

cvanbeek
  • 1,796
  • 21
  • 30

1 Answers1

1

This behavior is an artifact of how powershell works. The script will continue even if a command fails and the overall exit code for the script (and job) will be the exit code of the last command.

To ensure a command failure in XComp_BL653_8DC0_1053.exe causes the job to stop and exit, you would want to do something like:

script:
  - | 
    XComp_BL653_8DC0_1053.exe .\main.linenumbers.sb
    if(!$?) { Exit $LASTEXITCODE }

You can see this pattern repeated a lot in the internal powershell scripts used by the runner.

You can also set the $ErrorActionPreference = "Stop" to change this behavior for powershell cmdlets (not necessarily .exes). This can be done in an environment variable:

  variables:
    ErrorActionPreference: STOP

For additional context, see:

sytech
  • 29,298
  • 3
  • 45
  • 86
  • 1
    Gave this a try and unfortunately, it didn't work. One thing to note from my question is that the CI/CD job does stop after the XComp.exe exits with a non-zero exit code. So I don't think it's an issue of powershell not exiting, but of GitLab not recognizing that it exited with a non-zero code. Even if XComp.exe is my last command, the job still "succeeds" – cvanbeek Apr 14 '22 at 21:42