Sub topic 4
Why a Step Fails: Exit Codes and Shell Defaults
This chapter assumes you can read a workflow YAML file and run commands in a Bash shell. It also assumes you know the workflow, job, and step hierarchy and can open a run page. Here you will learn the rule that decides whether a step is red or green, which is an exit code. You will also learn how the default shell can hide failures or cause them.
The rule: a step passes when its process exits with 0
A run step starts a shell and gives it your script. When the shell exits, the runner reads the exit code. An exit code is an integer that a process returns to its parent when it ends.
0means success.- Any other value means failure.
The runner does not parse your output or look for the word "error". It only checks the code. This has two consequences:
- A step can print many errors and still pass if the shell exits with 0.
- A step can print nothing and fail.
When a step fails because of a nonzero code, the log ends with a line like this:
Error: Process completed with exit code 1.That line names the code, but it does not explain the failure. The cause is in the output above it.
What happens after a step fails
Once a step fails, the runner updates the status of the rest of the job.
- The failing step is marked failed.
- Later steps are skipped, unless their
if:condition allows them to run. - The job is marked failed.
- Other jobs that list this job in
needsare skipped. Independent jobs keep running.
Take the baseline workflow from this tutorial. If package.json is in app/ and the workflow runs npm ci from the repository root, npm ci exits with 1. The npm test step shows as skipped. The skipped step did not fail. It never ran. Beginners often look for an error in the skipped step, and there is none.
Two settings change this flow:
continue-on-error: trueon a step lets the job continue. The step is still shown as failed in the log, but the job does not fail because of it.timeout-minutescancels a step or job that runs too long. The result iscancelled, notfailed, and the log usually has no exit-code line.
Reading common exit codes
Many tools use their own codes, so check the tool's documentation. A few codes come from the shell and the operating system, and they are worth recognizing.
| Code | Usual meaning | Typical cause in a workflow |
|---|---|---|
| 1 | General error | A test failed, a build failed, or `npm ci` could not find `package.json` |
| 2 | Misuse of a command | Wrong arguments or a syntax error in a script |
| 126 | Found but not executable | A script missing its executable bit (`chmod +x`) |
| 127 | Command not found | A tool is not installed on the runner, or a typo in the command name |
| 137 | Killed by signal 9 (128 + 9) | The process ran out of memory and was killed |
| 143 | Terminated by signal 15 (128 + 15) | The job was cancelled or timed out |
A code above 128 means the process was ended by a signal. Subtract 128 to get the signal number.
Shell defaults that change behavior
The shell option decides how your script runs. If you do not set it, the default depends on the runner's operating system.
| `shell` setting | Command the runner uses | Stops at first error? |
|---|---|---|
| Not set, Linux or macOS | `bash -e {0}` (falls back to `sh` if Bash is missing) | Yes, but pipelines are not checked |
| `bash` set explicitly | `bash --noprofile --norc -eo pipefail {0}` | Yes, and pipelines are checked |
| Not set, Windows | `pwsh` | Yes, for PowerShell errors; native commands need care |
| `sh` | `sh -e {0}` | Yes |
{0} is replaced by the path of a temporary file that holds your script.
The -e flag means "exit immediately if a command fails". It makes multi-line scripts safe by default. Without it, a script would carry on after a failure, and the step's code would come only from the last command.
The pipeline trap
With the default Bash command, -e is set but pipefail is not. A pipeline's exit code is then the code of its last command. A failure earlier in the pipeline is lost.
name: Pipe demo
on: workflow_dispatch
jobs:
demo:
runs-on: ubuntu-latest
steps:
- name: Hidden failure (default shell)
run: |
false | tee output.txt
echo "this still runs"
- name: Visible failure (explicit bash)
shell: bash
run: |
false | tee output.txt
echo "this never runs"In the first step, false exits with 1, but tee exits with 0. The pipeline reports 0, and the step passes. In the second step, shell: bash adds pipefail, so the pipeline reports 1 and the step fails before the echo.
This is a common reason a "green" run hides a broken test. The usual pattern is npm test | tee test.log. If you want the output saved and the failure kept, set shell: bash or add set -o pipefail at the top of the script.
Other ways scripts hide or create failures
- Command substitution in assignments. In
x=$(failing_command),-estops the script. Inexport x=$(failing_command), theexportsucceeds and the failure is lost. Assign first, then export. - Conditions and
||. Bash ignores-efor commands insideif,while, or a||or&&list. A failing command there does not stop the script. - Unset variables. Bash expands an unset variable to an empty string. A path like
rm -rf "$DIR/"becomes dangerous whenDIRis empty. Addingset -umakes this an error. - PowerShell and native programs. In PowerShell, a native program that fails does not always stop the script. Check
$LASTEXITCODEafter calls that matter, or set$ErrorActionPreference = 'Stop'and test the code yourself.
Actions can fail a step without a shell
A step with uses: runs an action, not a shell script. The action decides how it fails. JavaScript actions call a function such as core.setFailed(message), which logs an error and sets the step's result to failed. You will often see an Error: line with a clear message instead of a numeric exit code. Treat that message as the starting point for diagnosis.
Setting the shell once
If many steps depend on strict Bash behavior, set it as a default for the whole job or workflow:
name: Strict shell
on: [push, pull_request]
defaults:
run:
shell: bash
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test and keep a log
run: npm test 2>&1 | tee test.logdefaults.run.shell applies to every run step in scope. Now a failing npm test fails the step even though tee follows it. The defaults.run block also accepts working-directory, which is the fix for the npm ci problem described earlier.
Checklist for a failed step
- Find the
Process completed with exit code Nline, or theError:line from an action. - Read the output above it, not only the line itself.
- Look up the code: 127 means a missing command, 137 means memory, and 1 needs the tool's own message.
- Check whether the shell's defaults could be hiding or causing the result, such as a pipeline without
pipefailor a command inside an||list. - Check which steps were skipped. They are consequences, not causes.
The next chapter uses these facts to find the first real error in a log full of follow-on messages.
Exercise
A step runs npm test | tee results.txt with no shell: setting on an Ubuntu runner. The tests fail and print errors, but the step is green. Why, and what are two ways to fix it?
Show answer
With no shell: setting, the runner uses bash -e {0}, which has no pipefail. The pipeline's exit code is that of the last command, tee, which succeeds, so the failure of npm test is lost. Fix it by setting shell: bash on the step (or in defaults.run.shell), which uses -eo pipefail, or by adding set -o pipefail as the first line of the script. Either way the pipeline now returns npm test's nonzero code and the step fails.
Exercise
A job has three steps in order: npm ci, npm test, and a cleanup step with if: always(). npm ci exits with code 1. Which steps fail, which are skipped, and which run?
Show answer
npm ci fails. npm test is skipped, because it has no if condition that allows it to run after a failure. The cleanup step runs because always() is true regardless of earlier results. The job is marked failed because of npm ci.
Exercise
A log ends with "Error: Process completed with exit code 127." What does this code usually mean, and what do you check first?
Show answer
Code 127 means "command not found". Check the command name for typos and check whether the tool is installed on the runner, or whether an earlier setup step that should have installed it was skipped or used the wrong version.