Enable pipefail in the Bash process running your test pipeline so a successful tee cannot hide a failed test. If the log is required and you also need to preserve the test’s exact exit code, capture Bash’s PIPESTATUS array immediately after the pipeline. Report both codes and return the test code if it failed, otherwise return the tee code. A green check should mean the test command and the log copy both succeeded.
Last reviewed: 2026-11-24 (Asia/Shanghai; 2026-11-23 UTC).
Reproduce the false green before changing the wrapper
A coding agent adds 2>&1 | tee test.log so that a failing test is visible on the console and saved for later diagnosis. The next run prints a failure and saves the right line, yet the step passes. That combination is possible because the shell normally takes the pipeline status from its last command. tee can successfully copy the output of a test that failed. The file’s existence is not evidence that the test passed.
Run this in a throwaway directory with Bash available. The small child process writes a fixed line to standard error and returns 7; the fixture does not need an application or a test framework.
(
set +e
set +o pipefail
bash -c 'printf "%s\n" "fixture failed" >&2; exit 7' 2>&1 | tee baseline.log
printf 'pipeline=%d test=%d tee=%d\n' "$?" "${PIPESTATUS[@]}"
)
Expect pipeline=0 test=7 tee=0 and a baseline.log containing fixture failed. The printf expands both status values before executing; putting a separate echo or assignment before reading PIPESTATUS would measure that newer command instead. The Bash manual
explains both the aggregate pipeline status and the array of individual statuses. The POSIX definition of tee
explains why its status answers a different question: whether the copy succeeded.
If all you need is a nonzero result when either stage fails, set -o pipefail is sufficient for this foreground pipeline, provided the calling script does not then consume or overwrite the failure. With it enabled Bash returns the rightmost nonzero stage status, or zero if all stages succeed. That is enough for the live log in a randomized test-order check
when you do not need to distinguish the two failure types. It does not guarantee that the aggregate is the test’s code if the logger fails too.
Capture both statuses before printing anything
Save this wrapper as ci/run-logged.sh and invoke it with Bash. Pass the log path first, then the actual test command and its arguments. The test command is invoked using "$@", so each input stays a separate argument. Pass a normal runner or an executable test script that already returns the right result when invoked on its own.
#!/usr/bin/env bash
set -euo pipefail
if (( $# < 2 )); then
printf 'Usage: %s LOG COMMAND [ARG ...]\n' "$0" >&2
exit 2
fi
log_path=$1
shift
if "$@" 2>&1 | tee -- "$log_path"; then
stages=("${PIPESTATUS[@]}")
else
stages=("${PIPESTATUS[@]}")
fi
printf 'test_status=%d tee_status=%d\n' "${stages[0]}" "${stages[1]}" >&2
if (( stages[0] != 0 )); then
exit "${stages[0]}"
fi
exit "${stages[1]}"
The pipeline is an if condition so Bash’s -e does not exit the wrapper before it can capture the outcome. The first command in each branch is deliberately identical: the wrapper needs the two statuses whether the aggregate succeeded or failed. They refer to the test and the logger, in that order. Keep the 2>&1 on the test command, before the pipe, to feed both test streams into the copy. The wrapper’s status line is outside that pipe and belongs to the enclosing step’s diagnostics, not the test log.
Treat the two fields as observed process exits, not as a root-cause analysis. If both are nonzero, this wrapper favors the test code for compatibility with callers and still reports that the log copy failed. If a logger stops reading early, an upstream write can fail too. A nonzero test status in that case does not on its own prove that an assertion failed. Also avoid adding a formatter or head to the pipeline without revisiting the design: this wrapper maps exactly two positions and expects the logger to consume the full stream.
Prove the check rejects a bad log destination
Run the following from the repository root after saving the wrapper. Use a directory as the log destination to force a file-open error without relying on permissions, which can behave differently under different runner identities. Each if in this probe lets the next case run even when the preceding wrapper exits nonzero. Do not put that tolerance around the real required test step.
fixture_dir=$(mktemp -d)
mkdir "$fixture_dir/not-a-log"
probe() {
local result
if bash ci/run-logged.sh "$@"; then
result=0
else
result=$?
fi
printf 'wrapper_status=%d\n' "$result"
}
probe "$fixture_dir/pass.log" bash -c 'printf "%s\n" "fixture passed"'
probe "$fixture_dir/fail.log" bash -c 'printf "%s\n" "fixture failed" >&2; exit 7'
probe "$fixture_dir/not-a-log" bash -c 'printf "%s\n" "fixture passed"'
probe "$fixture_dir/not-a-log" bash -c 'printf "%s\n" "fixture failed" >&2; exit 7'
printf 'Inspect fixture logs in %s\n' "$fixture_dir"
| Fixture | Test status | tee status | Wrapper status |
|---|---|---|---|
| Test passes, log writable | 0 | 0 | 0 |
| Test fails, log writable | 7 | 0 | 7 |
| Test passes, log destination is a directory | 0 | Nonzero | Same as tee |
| Test fails, log destination is a directory | 7 | Nonzero | 7, with both failures reported |
The local fixture produced those results on Bash 5.2.21 and GNU tee 9.4. In the last two cases tee returned 1 and the fixture text still reached the console. Use a nonzero assertion for tee rather than depending on that particular number: the standard does not require that exact error code. For the writable cases, check the expected line in each file as well as the status. The test is about both successful delivery and honest failure reporting, not about whether a pathname happens to exist.
Keep the test result authoritative in the CI step
In GitHub Actions, the documented unspecified Linux/macOS shell uses Bash with -e when Bash is available; explicitly setting shell: bash also supplies pipefail to the generated step script. That setting does not rewrite a separate script or choose the interpreter for it. Invoke this wrapper with Bash explicitly and make its return value the step’s result. For example, after checkout, a repository with an executable ci/test.sh can use this step fragment:
steps:
- name: Test and save the log
shell: bash
run: |
mkdir -p artifacts
bash ci/run-logged.sh artifacts/test.log ./ci/test.sh
The GitHub Actions workflow reference
also documents continue-on-error. Leave it off for this required step. If you need an artifact or summary after a failure, give collection its own step and an explicit condition that runs it on failure. Do not append || true to the wrapper to get at the file, and do not replace the check with a successful upload. The upload answers whether a file was retained; it cannot change the test outcome.
Before approving the patch, run one known-failing fixture through the real CI step and inspect the step result, both exit fields, and the saved log for that same attempt. Then run a passing fixture and confirm the check turns green without changing the shell or the test selection. The local probes above verify Bash behavior, not the hosted workflow. If the hosted step is still green after a nonzero wrapper result, inspect the calling shell and the workflow’s failure policy before asking an agent to fix the application. Use a bounded CI repair loop once you know which command actually failed.
Sources checked
- GNU Bash manual page, hosted by Bash maintainer Chet Ramey
— Pipelines,
PIPESTATUS, and theset -eexceptions. This is an evergreen language reference for the Bash behavior exercised here, not a release announcement. - The Open Group and IEEE: POSIX tee
— Description, Exit Status, and Consequences of Errors in the Issue 8 specification. The standard supports the nonzero failure expectation, not the exact GNU
teeerror code observed locally. - GitHub Docs: Workflow syntax for GitHub Actions
— Step shell defaults, exit-code handling, and step-level
continue-on-error, checked against the live official reference.