Learning center

The log says failed and the step is green

Look for a pipe. In a shell pipeline the exit status is the last command's, so npm test | tee test.log reports success when tee succeeds, whatever the tests did.

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:

SettingCommand runpipefail
No shellbash -e {0}Off
shell: bashbash --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#

PatternWhat is reported
npm test; echo doneThe status of echo, unless the shell runs with -e
npm test || trueAlways 0
result=$(npm test) followed by a check of $resultThe 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 backgroundThe 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#

LogStepNext
Failure summary from the runnerGreenRerun without the pipe. Treat the result as failed until then
Error text, and the runner's summary says passedGreenProbably an expected error. Find the test that prints it
No summary at allGreenThe 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.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading