Sub topic 4
Why a step fails: exit codes and shell defaults
This chapter assumes you can read YAML, run commands in a shell, and have a repository with a workflow. It explains the rule that decides whether a step is red or green, and how the default shell changes that rule.
The rule: exit codes
A run step passes when its shell exits with code 0. Any other code fails the step. The runner then marks the job as failed and skips the remaining steps, unless a later step has a status condition such as if: failure() or if: always().
The log shows this as a final line like Error: Process completed with exit code 1. That line reports the symptom only. The cause is printed above it.
Shell defaults decide which errors count
The shell exit code is the exit code of the last command that ran, unless the shell stops earlier. How the shell treats errors in the middle of a script depends on the shell.
| Shell | When it applies | Behavior on a failing command |
|---|---|---|
| `bash` (explicit) | `shell: bash` | Runs with `-e -o pipefail`. The first failing command stops the script. |
| `bash` (default) | Linux and macOS runners when no `shell` is set | Runs with `-e`, but without `pipefail`. |
| `pwsh` | Windows runners by default | A failing native command does not stop the script by default. |
| `sh` | `shell: sh` | Runs with `-e`. |
Check the GitHub documentation for the exact flags on your runner, because they can differ between shells and over time. What matters is the pattern: a script keeps going or stops depending on the shell options.
Pipes hide failures
Without pipefail, a pipeline returns the exit code of its last command only.
steps:
- name: Hides the failure
run: npm test | tee test.log
- name: Reports the failure
shell: bash
run: npm test | tee test.logIn the first step, tee succeeds even if npm test fails, so the step passes with default bash. In the second step, explicit shell: bash adds pipefail, so the step fails as expected. If a green step hides a broken test run, check for a pipe.
Multi-line scripts
With -e, the first failing command ends the script and the step. Without it, as in pwsh with native commands, later lines still run. The log then contains output from commands that ran after the real failure, and the final exit code may come from the last line only.
steps:
- name: Stop on first error
shell: bash
run: |
npm ci
npm test
echo "Done"If npm ci fails here, npm test never runs and nothing prints "Done".
Common sources of non-zero codes
- A test runner or linter exits 1 when checks fail. This is the intended signal.
command not foundgives exit code 127.- A script file that is not executable gives exit code 126.
- A process killed for running out of memory or by a timeout may give 137 or 143, or the job may be cancelled.
exit 1in your own script fails the step on purpose, as in theRun checksstep in the annotation example.
Steps that fail without stopping the job
Two settings change the effect of a failure, not the exit code itself:
continue-on-error: trueon a step lets the job continue and counts the step as passed for the job result. The step still shows a warning-style annotation.if: failure()andif: always()let a step run after an earlier failure, which is useful for diagnostics.
steps:
- run: npm test
- name: Collect diagnostics
if: failure()
run: ls -laReading this in a real log
In the baseline workflow, if package.json lives in app/, npm ci fails with an error about a missing package.json. Then the runner prints Error: Process completed with exit code 1. and npm test is skipped. The exit code tells you which step failed. The message above it tells you why.
Exercise
A step runs npm test | tee results.log with no shell key on a Linux runner. Tests fail, but the step is green. Why, and what is the smallest change that fixes it?
Show answer
With the default bash invocation there is no pipefail, so the pipeline returns the exit code of tee, which is 0. Adding shell: bash to the step enables -o pipefail, so the failure of npm test becomes the pipeline's exit code and the step fails.
Exercise
A job has steps npm ci, npm test, and a final step with if: failure(). npm ci fails. Which steps run?
Show answer
npm test is skipped because an earlier step failed and it has no status condition. The final step runs because failure() is true.