Sub topic 14
Reproducing a failure outside Actions
This chapter assumes you can already find the failing step in a run and tell the first real error from the errors that follow it. It also assumes you can read the failure categories from the earlier chapters: environment, dependency, permission, secret, and test problems. You need a shell, Git, and ideally Docker.
Pushing a commit and waiting for a run is a slow loop. Each attempt takes minutes and fills the history with "try again" commits. If you can make the same failure happen on your own machine, you can iterate in seconds. You then push one commit that you already know works.
Not every failure can be reproduced locally. Some depend on secrets, token permissions, or the runner itself. Knowing which failures reproduce is part of the skill.
Decide whether local reproduction is worth it
Use the failure category to decide.
| Category | Reproduces locally? | Reason |
|---|---|---|
| Test failure | Usually | Same code and same command give the same result, if versions match |
| Dependency failure | Usually | Lock file and package manager versions decide the outcome |
| Environment failure (wrong version, missing tool) | Yes, if you match the runner | The cause is the difference between your machine and the runner |
| Workflow syntax error | Not needed | The file is rejected before any step runs. Fix the YAML |
| Permission error (403) | No | It depends on the `GITHUB_TOKEN` permissions granted at run time |
| Secret error | Partly | You can supply fake values, but not the real ones |
For permission and secret failures, go back to the log, the permissions: block, and the repository settings. A local run will not show the problem.
Collect what the log tells you
Before you type anything, write down these facts from the failed run. The logs hold all of them.
- The commit. The run page shows the commit SHA. For
pull_requestevents, the workflow checks out a temporary merge commit, not your branch tip. - The runner image. Expand "Set up job" and look under "Runner Image". It names the operating system image and its version.
- Tool versions. Look at the output of the setup steps, such as the Node version that
actions/setup-nodeinstalled. - The exact command. Copy it from the step header. Note any
working-directory,shell, orenvsettings on that step or indefaults. - The environment. Note
env:blocks at the workflow, job, and step levels.
A step that prints versions makes comparison easier. You can add one near the top of the job:
- name: Print environment
run: |
node --version
npm --version
echo "OS=$RUNNER_OS ARCH=$RUNNER_ARCH"The reproduction loop
The loop is the same for every failure. Start from a clean state and run the same command. If the failure appears, you can fix it. If it does not, the difference between the two environments is the cause, so you compare them.
Since I cannot nest fences, the diagram follows in the required format.
Start from a clean checkout
The runner begins every job with a fresh checkout of committed files. Your working directory has extras: uncommitted edits, ignored files such as node_modules, and a .env file the repository does not track. Any of these can hide a failure.
This script clones the committed state of one SHA and runs the commands from the fixed workflow earlier in this tutorial:
#!/usr/bin/env bash
set -euo pipefail
sha="${1:?usage: repro.sh <commit-sha>}"
work="$(mktemp -d)"
git clone --quiet . "$work/repo"
git -C "$work/repo" checkout --quiet "$sha"
cd "$work/repo/app"
export CI=true
node --version
npm ci
npm testRun it from the repository root, for example bash repro.sh 3f9c2ab. Three details matter here:
git clonecopies only what is committed, so uncommitted files cannot influence the result.cd .../appmirrorsdefaults.run.working-directory: appin the workflow.CI=trueis set because many tools change behavior in CI. Test runners often switch from watch mode to a single run, and some tools turn warnings into errors.
If the run came from a pull request, the commit is the merge commit. Fetch it first with git fetch origin refs/pull/<number>/merge, then pass FETCH_HEAD's SHA to the script. The failure may only exist in the merged result.
Match the runner more closely
Your laptop probably differs from ubuntu-latest in operating system, installed tools, and default shell. Docker closes most of that gap.
docker run --rm -e CI=true -v "$work/repo":/work -w /work/app node:20 \
bash -c 'npm ci && npm test'The node:20 image is not the runner image. It gives you a clean Linux environment with the Node version the workflow requested. That is enough for most dependency and test failures. It will not contain every tool preinstalled on a hosted runner, such as the full set of language runtimes. If the failure involves a missing system package, install it in the container the same way the workflow does.
Mounting the clone rather than your working directory keeps your local node_modules out of the container. Native modules built on macOS or Windows break on Linux, which causes confusing errors of its own.
Match the shell behavior
How the shell runs your command can change whether a step fails. As covered earlier, the default shell on Linux runners behaves differently depending on whether the step names a shell:
| Step setting | Command used | Effect |
|---|---|---|
| No `shell:` | `bash -e {0}` | Stops at the first failing command, but a failure inside a pipe can be missed |
| `shell: bash` | `bash --noprofile --norc -eo pipefail {0}` | Stops at the first failure, including failures inside a pipe |
Run multi-line scripts locally under the same flags. A pipe such as npm test | tee out.log passes locally in a plain interactive shell because the exit status of tee wins. With pipefail, a failing npm test fails the whole line.
bash --noprofile --norc -eo pipefail -c 'npm test | tee out.log'
echo "exit status: $?"Also remember that your interactive shell loads profile files, aliases, and functions that the runner does not.
Supply the variables the workflow expects
Workflows read variables and secrets that do not exist on your machine. Find them in the log, in the step's env section, or in the YAML.
GITHUB_*andRUNNER_*variables. Scripts that readGITHUB_SHAorGITHUB_REF_NAMEneed these set. Export plausible values yourself.- Secrets. Do not paste real secrets into your shell history. Use a harmless stand-in and see whether the failure changes. A step that fails with an authentication error will fail locally with a fake value too, which at least confirms the step uses the value as you expect.
- Missing values. If a secret is not available, for example in a pull request from a fork, the expression evaluates to an empty string. Reproduce that by exporting the variable as empty:
export API_KEY="".
Running the workflow itself with act
The open-source tool act runs workflow files locally inside Docker containers. It is useful when the failure depends on how steps are wired together, not just on one command.
# List the jobs act can see
act -l
# Run one job for a push event
act push -j test
# Pass a stand-in secret
act push -j test -s API_KEY=dummy-valueTreat it as an approximation. Its default images are smaller than the hosted runner images, and some actions, caching behavior, and token features do not work the same way. A pass in act does not prove a pass in Actions, and a failure in act may come from act itself. Use it to speed up your loop, then confirm in a real run.
When it will not reproduce
If the exact command passes locally on a clean checkout, the cause is somewhere in this list. Check in order of likelihood.
- Tool version mismatch. Compare
node --versionandnpm --versionagainst the log. An unpinnedlatestcan change between runs. - Files that exist locally but not in Git. A missing file is excluded by
.gitignore, or a filename differs by letter case. macOS and Windows file systems are usually case-insensitive, and Linux is not, soimport "./Utils"can work locally and fail in CI. - Stale caches. A restored cache can hold old dependencies. Check the cache step's output and try a run without the cache.
- Different event. The
pull_requestmerge commit differs from your branch tip. - Timing and resources. Tests that depend on speed, ordering, or time zones can fail on a slower runner. The runner clock uses UTC.
- Permissions and secrets. These cannot be reproduced locally, as covered above.
When you find the difference, fix the cause, not just the symptom. If the Node version differed, pin it in the workflow with node-version so CI and your machine stay aligned.
Exercise
A test passes on your laptop but fails in Actions with "Cannot find module './Utils'". The file in the repository is named utils.js. Your laptop runs macOS. What is the likely cause, and how would you reproduce it locally?
Show answer
macOS file systems are usually case-insensitive, so the import "./Utils" finds utils.js locally. The Linux runner has a case-sensitive file system, so the import fails. Reproduce it by running the tests in a Linux container against a clean clone, for example docker run --rm -e CI=true -v "$work/repo":/work -w /work node:20 bash -c 'npm ci && npm test'. Fix it by changing the import to "./utils" so the case matches the real filename.
Exercise
Your step runs "npm test | tee out.log" without a shell setting. It reports success in Actions even though tests failed. Locally, running with "bash --noprofile --norc -eo pipefail" makes it fail. What is the difference, and what change would make the CI step reliable?
Show answer
Without a shell setting, the runner uses "bash -e {0}", which has no pipefail, so the exit status of the pipe is the status of tee, which succeeds. With shell: bash, the runner uses -eo pipefail, so the failing npm test fails the pipe. Add "shell: bash" to the step, or set "set -o pipefail" in the script, so a failure in npm test fails the step.