Sub topic 13
Fixing the Workflow and Verifying the Result
This chapter assumes you can already find a failing step and read its log. You should know how exit codes make a step fail, and you should be able to tell a root-cause error from the errors that follow it. Here you apply a fix and prove it worked.
Fix one cause at a time
Change only what the first real error points to. If you change several things at once, a green run won't tell you which change fixed it. A red run won't tell you which change broke something new.
Take the baseline workflow below. The repository keeps package.json in app/, so npm ci fails at the repository root. The later steps are skipped.
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 log for npm ci reports that it cannot find a package.json or lock file in the working directory. That is the first real error. The skipped npm test step is a consequence, not a second bug.
Apply a targeted fix
Set working-directory for every run step with defaults.run. Point the cache at the lock file in app/, because setup-node looks for it at the root by default.
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 testThe defaults.run setting affects only run steps. It does not change uses steps, which is why the cache path needs its own entry.
Verify with a new run
A fix is confirmed only by a new run of the changed workflow. Push the change to a branch, or re-run the failed jobs, and then check the result.
# Re-run only the failed jobs
gh run rerun <run-id> --failed
# Watch the newest run and show its result
gh run list --limit 1
gh run view <run-id> --log-failedRe-running an old run uses the workflow file from the original commit. If you changed the workflow file, push a new commit so that the new run uses it.
Check what the new run shows
Confirm these points before you call the fix finished:
- The run used your new commit. Check the commit SHA at the top of the run page.
- The step that failed before now passes. Open it and read its log, don't rely only on the green check.
- Steps that were skipped before now ran.
- Any new failure is a different error. If so, you fixed the first problem and uncovered the next one. Repeat the loop.
Common mistakes when fixing
| Mistake | Result |
|---|---|
| Re-running a failed job after editing the workflow | The old workflow file is used, so the failure repeats |
| Fixing the last error in the log | The root cause remains and the failure returns |
| Adding `continue-on-error: true` to hide a failure | The run turns green while the problem remains |
| Changing several things in one commit | You can't tell which change worked |
Exercise
A run fails at npm ci because package.json is in app/. You edit the workflow file on the same branch and click "Re-run failed jobs" on the old run. The run fails the same way. Why, and what should you do?
Show answer
Re-running an existing run reuses the workflow file from its original commit, so your edit was not used. Push a new commit with the change so that a new run starts with the updated workflow, then check that npm ci and npm test pass in that run.