Sub topic 6
How exit codes fail a step and a job
This chapter assumes you can read a basic GitHub Actions workflow, know what npm run lint does, and have a lint script and a format check script like the ones from the earlier chapters. You will learn how a process exit code becomes a red or green check on a pull request.
The exit code is the signal
Every command ends with an exit code, a small integer returned to the shell. 0 means success. Any other value means failure. ESLint exits with 1 when it finds lint errors. prettier --check exits with 1 when any file is not formatted. Ruff behaves the same way.
GitHub Actions does not read the tool's output to decide whether a check passed. It only looks at the exit code of the step's shell.
From step to job to workflow run
A run: step starts a shell. On Linux runners, GitHub invokes bash with -e (exit on first error) and -o pipefail. If the last command in the step exits non-zero, the step fails.
The failure then spreads upward:
- A failed step marks the job as failed.
- By default, the remaining steps in that job are skipped. A step with
if: always()orif: failure()is the exception. - A failed job marks the workflow run as failed.
- The failed job's name appears as a failing status check on the pull request.
Seeing it in a workflow
Consider this job. The step order matters, because the first failure stops the rest.
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run format:checkIf npm run lint exits 1, npm run format:check never runs. You fix the lint errors, push again, and only then learn about formatting problems. This is a cost of putting both checks in one job. The parallel jobs chapter shows how to remove it.
Multi-line scripts and hidden failures
With a multi-line run: block, the -e flag stops the script at the first failing command. A pipeline is different. Without pipefail, a pipeline returns the exit code of its last command only, so a failing linter piped into another tool can look like a success. The default shell on Linux sets pipefail for you. If you set shell: sh yourself, you lose that safeguard.
- name: Lint and keep a log
run: npm run lint 2>&1 | tee lint.logWith the default bash shell, this step fails when npm run lint fails, even though tee succeeds.
Deliberately ignoring a failure
Sometimes you want a step to report but not block. You can use continue-on-error: true on a step.
- run: npm run lint
continue-on-error: trueThe step shows as failed in its logs, but the job continues and can still pass. Use this sparingly. A lint check that cannot fail no longer protects the main branch, and reviewers tend to ignore it.
Common mistakes
- Using a command that always exits
0, such asprettier --write .. It rewrites files on the runner and passes. Use--check. - Adding
|| trueto silence a noisy tool. This hides every failure. - Running a linter with a warnings-only configuration. ESLint exits
0for warnings unless you pass--max-warnings 0.
Exercise
A step runs npx eslint . | tee out.txt with shell: sh. ESLint finds errors and exits 1, but the step is green. Why, and what is the simplest fix?
Show answer
In plain sh without pipefail, the pipeline's exit code is the exit code of the last command, tee, which is 0. Remove the custom shell: sh so the default bash with -e -o pipefail is used, or set shell: bash, so the ESLint failure propagates.