Sub topic 13
Fixing the Workflow and Verifying the Result
This chapter covers the last stage of debugging a failing GitHub Actions run: making a targeted change and proving that it worked. It assumes you can already do the following:
- Find the failing step in a run.
- Separate the first real error from the errors that follow it.
- Place a failure in a category such as workflow syntax, environment, dependency, permission, secret, or test.
- Use
gh run viewandgh run rerun.
The fix loop
A fix is a hypothesis, and a new run tests it. Treat each edit as an experiment with an expected result that you can check.
Two outcomes are normal and do not mean you made things worse:
- The same error returns. Your hypothesis was wrong or the change did not apply.
- A different error appears. Steps that were skipped before can now run and fail for their own reasons.
Worked example: a project in a subdirectory
Suppose the repository keeps its Node project in app/. This workflow runs npm ci at the repository root:
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testThe run page shows a red npm ci step. The npm test step is grey, which means it was skipped, not that it passed. The log contains an error like this:
npm error code ENOENT
npm error syscall open
npm error path /home/runner/work/demo/demo/package.json
npm error errno -2
npm error enoent Could not read package.jsonThe failure category is environment. The command ran in the wrong directory. The signature is that the path points at the repository root, where no package.json exists. The error is not about a missing dependency or a failing test.
State the hypothesis before editing: npm ci runs in the workspace root, but package.json lives in app/. Setting the working directory should fix it.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: app
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: app/package-lock.json
- run: npm ci
- run: npm testThree details in this fix are easy to get wrong:
defaults.run.working-directoryapplies only torunsteps. Theusessteps (checkout and setup-node) are unaffected, which is what you want here because checkout must put files at the workspace root.- The
cacheoption onsetup-nodelooks for a lock file at the repository root by default. Withoutcache-dependency-path, the cache setup would fail because it cannot find a lock file, which is a second error hidden behind the first. cache-dependency-pathis relative to the workspace root, so it isapp/package-lock.json, notpackage-lock.json.
The cache change goes beyond the original error. It is included because the working-directory move would otherwise break caching. If you had not planned to use caching, you would leave it out. Keep each fix limited to the cause you identified.
Choosing the smallest change
Prefer the change that addresses the cause and nothing else. These habits reduce the risk of a fix that works by accident:
- Change one thing per run when the cause is uncertain. If you change the Node version and the working directory together, you will not know which one mattered.
- Fix the cause, not the symptom. Adding
continue-on-error: trueto a failing step turns the run green and leaves the problem in place. - Pin to a known version instead of floating. If a dependency update broke the build, a version pin or a lock file update is more reliable than retrying.
- Make the fix on a branch and open a pull request. The
pull_requesttrigger then runs your changed workflow before you merge.
Common fixes by category:
| Category | Typical fix | What to check in the new run |
|---|---|---|
| Workflow syntax | Correct indentation, key names, or expression syntax | The run starts and jobs appear at all |
| Environment | Set `working-directory`, runner OS, or tool version | The failing command's output shows the expected paths and versions |
| Dependency | Update the lock file, pin a version, fix the registry setting | The install step completes and the lock file is respected |
| Permission | Add a scoped entry to `permissions:` | The 403 is gone and the action it guarded happened |
| Secret | Create or rename the secret, or pass it through `env` | The step receives a non-empty value (masked as `***` in logs) |
| Test | Fix the code or the test, not the workflow | The test step's own summary shows the previously failing test passing |
For permission failures, grant only what the job needs. Here is an example that lets a job comment on a pull request:
permissions:
contents: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- run: gh pr comment "$PR_NUMBER" --body "Checks finished"
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}Setting permissions: replaces the defaults for any scope you do not list, so list every scope the job uses.
Re-run or push a new commit?
This decision causes frequent confusion. A re-run uses the same commit and the same workflow file version as the original run. Editing the workflow and clicking "Re-run" will not pick up your edit. You must push a new commit.
Re-running is useful when the cause is outside the commit:
- A transient network failure or runner problem.
- A flaky test that you want to confirm is flaky.
- A change in repository settings, such as a secret you just added.
The gh commands cover both cases:
# List recent failed runs
gh run list --status failure --limit 5
# Show only the logs of failed steps for a run
gh run view <run-id> --log-failed
# Re-run only the failed jobs
gh run rerun <run-id> --failed
# Re-run with debug logging enabled
gh run rerun <run-id> --debug--failed re-runs only the jobs that failed, and it keeps results from jobs that passed. Use --debug when a re-run fails the same way and the normal log does not explain why.
After you push a fix, watch the new run from the terminal:
gh run list --branch my-fix-branch --limit 3
gh run watch <run-id> --exit-status--exit-status makes gh run watch exit non-zero if the run fails. That is useful in scripts.
Verifying beyond a green check
A green check means every step exited with code 0. It does not prove the fix did what you intended. Verify these points against the new run:
- The previously failing step now passes. Open it and read its log, not only its icon.
- The previously skipped steps ran. In the example,
npm testmust show real test output. - The work actually happened. A test step that reports "0 tests" exits 0 and proves nothing.
- No new warnings or annotations. Check the run summary for annotations that appeared with your change.
- The fix holds on a second run. For caching changes, the first run saves the cache and the second should report a hit. Push a trivial commit or re-run to see it.
- The fix holds on every trigger. The example runs on
pushandpull_request. Confirm both runs are green.
Edge cases
- Fork pull requests. Runs triggered from forks receive a read-only token and no secrets by default. A permission or secret fix that works on a branch in your repository may still fail on a fork. This is expected behavior, not a bug in your fix.
- Flaky tests. If a failure disappears after
gh run rerun --failedand no code changed, record it as flaky. Do not report it as fixed. Run it several times before concluding anything. - Matrix jobs. A fix for one matrix cell can leave others red. Check every cell, and remember that
fail-fastmay cancel cells before they show their own error. - Debug output left behind. Debug logging set through the
--debugflag applies only to that re-run. Do not commitechostatements that print environment details, since the log may be visible to others.
Exercise
A teammate edits ci.yml to add working-directory: app and clicks "Re-run all jobs" on the failed run. The run fails with the same ENOENT error. Why, and what should they do?
Show answer
A re-run reuses the commit and workflow file from the original run, so the edit was not included. They should push the change in a new commit, which starts a new run with the updated file. After it finishes, they should confirm that npm ci and npm test both ran in app/, and that npm test printed real test results.
Exercise
After the fix, the run is green but npm test prints "No tests found" and exits 0. Which verification point fails, and what is a likely cause?
Show answer
The point that the work actually happened fails. A green check only means exit code 0. A likely cause is that the test runner is looking in the wrong directory or using the wrong file pattern. Check the working directory and the test configuration, then confirm the log shows the expected number of tests.