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 likedocker. The task only checks$LASTEXITCODEonce, at the end.
To make a step fail fast, opt in at the top of any step that runs more than one command:
# pwsh 7.4+ (current hosted agents)$PSNativeCommandUseErrorActionPreference = $truedocker build -t "$image" -f Dockerfile .docker builder prune --all --force | Out-Nulldf -h /set -euo pipefaildocker build -t "$image" -f Dockerfile .docker builder prune --all --force > /dev/nulldf -h /If a command is meant to be best effort, check its exit code yourself instead of relying on the default:
docker builder prune --all --force | Out-Nullif ($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.