Reproduce it in one line#
bash -c 'false | tee o.log; echo $?' # 0
bash -c 'set -o pipefail; false | tee o.log; echo $?' # 1
pipefail is not available in every shell. With dash, which is sh on Debian and Ubuntu, set -o pipefail printed Illegal option -o pipefail in dash 0.5.12.
GitHub Actions has two bash defaults#
The workflow syntax reference lists the command behind each shell value on Linux and macOS:
| Setting | Command run | pipefail |
|---|---|---|
No shell | bash -e {0} | Off |
shell: bash | bash --noprofile --norc -eo pipefail {0} | On |
The same run line can be green under the first and red under the second. Set it once for the job:
defaults:
run:
shell: bash
pipefail can also turn a working step red. grep exits 1 when nothing matches, and some_command | head -1 can end the first command with a broken pipe. Fix those lines individually instead of leaving the setting off.
Other ways to lose the status#
| Pattern | What is reported |
|---|---|
npm test; echo done | The status of echo, unless the shell runs with -e |
npm test || true | Always 0 |
result=$(npm test) followed by a check of $result | The text, and not the status, unless the script reads $? first |
local out=$(npm test) | The status of local, which is 0 |
| A container started in the background | The start, and not the process inside |
Hand over the status with the log#
Write it into the file, from the command itself:
set -o pipefail
npm test 2>&1 | tee test.log
status=$?
echo "exit $status at $(date -u +%FT%TZ) on $(git rev-parse HEAD)" | tee -a test.log
exit "$status"
A receiver who gets only a log has to infer the result from its text. Logs from tests often print errors on purpose, for example a test of the error path, so "there is an error in the log" and "the run failed" are different statements. The last line settles which one is true.
Under bash -e, the script stops at the failing pipeline before the status line. Use if npm test 2>&1 | tee test.log; then … or set +e around it when the record must be written either way.
Decide#
| Log | Step | Next |
|---|---|---|
| Failure summary from the runner | Green | Rerun without the pipe. Treat the result as failed until then |
| Error text, and the runner's summary says passed | Green | Probably an expected error. Find the test that prints it |
| No summary at all | Green | The runner may not have finished. Find the exit code or rerun |
Worked example#
This example is fictional. A release note links a green check and a log whose last lines say 3 failed, 415 passed. The step piped into tee with no shell set. Adding shell: bash turns the next run red on the same commit. Ben looks back at earlier runs' logs, and the same three tests had been failing for nine days. The decision is no longer about this release alone.
A pass can also come from running no tests. Return to handing over evidence.