Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab Job Passes Despite Non-Zero Exit Code

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.

like image 214
cvanbeek Avatar asked Dec 13 '25 19:12

cvanbeek


1 Answers

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:

  • How to stop a PowerShell script on the first error?
  • Why are my PowerShell exit codes always "0"?
like image 58
sytech Avatar answered Dec 16 '25 22:12

sytech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!