allow_failure:exit_codes unusable with pwsh (on docker runner)
Summary
The exit code is always 1 when using pwsh shell with stdin, this is because the calls to pwsh have to be nested for encoding reasons (https://gitlab.com/gitlab-org/gitlab-runner/-/blob/7f824785bf65b4c3b42b56e970dd830a72a67805/shells/powershell.go#L107-144).
For executors other than shell and custom, stdin is always used (https://gitlab.com/gitlab-org/gitlab-runner/-/blob/7f824785bf65b4c3b42b56e970dd830a72a67805/shells/powershell.go#L610-623).
Steps to reproduce
I can reproduce this locally by running powershell commands:
$LaunchScript = @"
`$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
pwsh.exe -NoProfile -Command -
"@
$MyEncodedScript = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($LaunchScript))
Get-Content .\CI-Script.ps1 | pwsh.exe -NoProfile -NoLogo -InputFormat text -OutputFormat text -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $MyEncodedScript
Where CI-Script.ps1 contains:
echo "Running..."
exit 123
This always produces $LASTEXITCODE
as 1
. Whereas running Get-Content .\CI-Script.ps1 | pwsh.exe -NoProfile -NoLogo -InputFormat text -OutputFormat text -NonInteractive -ExecutionPolicy Bypass -Command -
gives the correct 123
.
.gitlab-ci.yml
variables:
FF_DISABLE_POWERSHELL_STDIN: 1
minimal-repo:
image: mcr.microsoft.com/powershell:lts-windowsserver-ltsc2022
script:
- echo "Running..."
- exit 123
allow_failure:
exit_codes: 123
Actual behavior
Whatever the exit code from the CI script the final exit code reported to GitLab is 1.
Expected behavior
The exit code should be propogated.
Relevant logs and/or screenshots
job log
$ echo "Running..."
Running...
$ exit 123
Cleaning up project directory and file based variables
00:02
ERROR: Job failed: exit code 1
Environment description
config.toml contents
...
[[runners]]
executor = "docker-windows"
shell = "pwsh"
...
Used GitLab Runner version
Running with gitlab-runner 16.8.0 (c72a09b6)
on docker-windows on win-builder08 - node 1 hXmfhKDM, system ID: s_f0ad5ee113eb
feature flags: FF_DISABLE_POWERSHELL_STDIN:true
Possible fixes
Include if(!
$?) { Exit &{if($LASTEXITCODE) {
$LASTEXITCODE} else {1}} }` in the launch script.
This is similar to #28658 (closed) and #28244 (closed) and #29127 (closed), but none of the mentioned workarounds/fixes work in this case.