Context

A pipeline step built an image, pruned the build cache and printed disk usage, one command per line. The build failed, but the step stayed green. The error only showed up at a later step that needed the image.

Insight

Both shell tasks pass or fail on the last command only by default.

  • Bash@3 (inline): the script runs without set -e, so a failing command in the middle doesn’t stop it. The task’s result is the last command’s exit code.
  • PowerShell@2 (pwsh): the default $ErrorActionPreference = 'Stop' covers cmdlet errors, not the exit codes of native commands like docker. The task only checks $LASTEXITCODE once, at the end.

To make a step fail fast, opt in at the top of any step that runs more than one command:

Terminal window
# pwsh 7.4+ (current hosted agents)
$PSNativeCommandUseErrorActionPreference = $true
docker build -t "$image" -f Dockerfile .
docker builder prune --all --force | Out-Null
df -h /
Terminal window
set -euo pipefail
docker build -t "$image" -f Dockerfile .
docker builder prune --all --force > /dev/null
df -h /

If a command is meant to be best effort, check its exit code yourself instead of relying on the default:

Terminal window
docker builder prune --all --force | Out-Null
if ($LASTEXITCODE -ne 0) { Write-Host "##vso[task.logissue type=warning]Prune failed" }

A single-command step is fine without this, because the last command is the only command.