Sub topic 10
Running lint and format as separate jobs
This chapter assumes you have a working lint and format workflow like the one from the basic workflow chapter. It also assumes you have lint and format:check scripts in package.json. Here you split the single job into two jobs that run side by side.
Why split the job
In a single job, steps run in order. If npm run lint fails, the job stops and npm run format:check never runs. You fix the lint errors, push, and only then learn that formatting also failed. That costs you an extra round trip.
Separate jobs avoid this. Each job gets its own runner, so both checks always run. Each also appears as its own status check on the pull request, so a failure tells you at once whether the problem is lint or formatting.
The two-job workflow
Jobs in the same workflow run in parallel unless you add needs. You only need to define two jobs with no dependency between them.
name: Lint
on:
pull_request:
push:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run format:checkThe job IDs, lint and format, become the names of the status checks. Pick them with care, because branch protection rules refer to these names.
Trade-offs
Each job repeats checkout, setup, and npm ci. That doubles the runner time you consume, even though the wall-clock time usually drops. The cache from setup-node reduces the cost of the repeated install, since both jobs restore the same cache.
| Approach | Wall-clock time | Runner minutes | Failure feedback |
|---|---|---|---|
| One job, two steps | Sum of both checks | Lower | Stops at the first failure |
| Two jobs | Longer of the two checks | Higher | Reports both failures |
If your checks take a few seconds each, setup dominates and the split gains little. It pays off when the linter is slow or when you want clear, separate statuses.
Common errors
- Renaming a job after requiring it. Branch protection matches on the check name. If you rename
formattoprettier, the old required check never reports, and the pull request waits forever. Update the protection rule at the same time. - Adding
needsby accident.needs: linton theformatjob makes it run only afterlintsucceeds. That brings back the first-failure-hides-the-second problem. - Duplicate job names across workflows. Two workflows that both define a job called
lintproduce confusingly similar checks. Give workflows and jobs distinct names.
Exercise
You split lint and format into two jobs, and a pull request has both lint errors and unformatted files. How many failing checks do you see, and why?
Show answer
Two. The jobs have no needs relationship, so they run in parallel on separate runners. Each one fails on its own exit code, and each reports its own status check.
Exercise
Your team requires the format check before merging. A teammate renames the job ID from format to prettier. What happens to open pull requests?
Show answer
They stay blocked. Branch protection still waits for a check named format, which no longer reports. You must update the required checks list to prettier.