Sub topic 15
Enforcing Lint and Format Checks in CI
This chapter assumes you have the lint and format:check npm scripts from earlier chapters, a working ESLint flat config with eslint-config-prettier, and a project hosted on GitHub. You should also know basic Git branching and pull requests.
Why the Pre-Commit Hook Is Not Enough
The Husky and lint-staged setup from the previous chapter catches most problems before they reach a commit. It still runs on a developer's machine, so it can be bypassed:
git commit --no-verifyskips every hook.- A contributor who cloned the repository but never ran
npm installhas no hooks installed. - Commits made through the GitHub web editor, or by bots, never run local hooks.
- lint-staged checks only staged files. A rule change in
eslint.config.jscan make untouched files fail, and the hook won't notice.
Continuous integration (CI) closes these gaps. The CI server checks out the code on a clean machine, installs the exact dependency versions from the lockfile, and runs both tools across the whole project. If either tool fails, the build fails, and branch protection can block the merge.
How CI Decides Pass or Fail
CI systems judge each step by its process exit code. Zero means success. Any other value fails the step. To make the gate reliable, you need to know what each tool returns.
| Command | Exit 0 | Exit 1 | Exit 2 |
|---|---|---|---|
| `eslint .` | No errors (warnings allowed) | At least one error, or too many warnings with `--max-warnings` | Config error or crash |
| `prettier . --check` | All files formatted | At least one file not formatted | Prettier itself failed, such as a syntax error |
The first row contains the most common surprise. Rules set to "warn" never fail the build on their own. That is often what you want locally, but in CI the warnings scroll by and nobody reads them. Over time they pile up into hundreds. The --max-warnings flag turns them into a hard limit:
npx eslint . --max-warnings 0With 0, any warning fails the run. Some teams on a legacy codebase set the limit to the current count and lower it as they clean up.
A Production-Ready GitHub Actions Workflow
The minimal workflow from the Topic Spine runs npm run lint and then npm run format:check in one job. It works, but if ESLint fails, the Prettier step never runs. The developer fixes the lint errors, pushes, and only then learns about formatting problems. Splitting the checks into parallel jobs reports both results at once.
Save this as .github/workflows/lint.yml:
name: Lint
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
concurrency:
group: lint-${{ github.ref }}
cancel-in-progress: true
jobs:
eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- name: Run ESLint
run: npm run lint -- --max-warnings 0
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- name: Check formatting
run: npm run format:checkEach part does a specific job:
onruns the workflow on every pull request and on pushes tomain. Without the branch filter, a push to a PR branch would trigger it twice, once forpushand once forpull_request.permissions: contents: readgives the job's token the least access it needs. Lint checks never write to the repository.concurrencycancels an older run on the same branch when a new commit arrives, which saves runner minutes.cache: npmcaches npm's download cache, keyed onpackage-lock.json, which speeds upnpm ci.npm ciinstalls exactly what the lockfile specifies and fails ifpackage.jsonand the lockfile disagree. Don't usenpm installhere. It can resolve different versions than the ones developers use.--passes--max-warnings 0throughnpm runto theeslintcommand.
Make the Check Required
A failing workflow only shows a red X. It still doesn't stop anyone from clicking Merge. To block merges, open the repository settings on GitHub and add a branch protection rule or ruleset for main. Enable "Require status checks to pass" and select the eslint and prettier jobs. The job names in the YAML are what appear in that list, so choose stable names and don't rename them casually.
Keeping Local and CI Results Identical
A check that passes locally and fails in CI wastes time and erodes trust. Most of these mismatches come from a few causes.
Version drift
Prettier's output can change between minor versions. If one developer has Prettier 3.3 and CI resolves 3.4, they may disagree on formatting. Prettier's documentation recommends pinning an exact version:
npm install --save-dev --save-exact prettierCommit package-lock.json, and make sure your scripts call the local binary (which npm run does) rather than a globally installed one.
Line endings on Windows
Prettier defaults to "endOfLine": "lf". On Windows, Git's core.autocrlf setting can check files out with CRLF endings, and then prettier --check reports every file. Add a .gitattributes file so every checkout uses LF:
* text=auto eol=lfChecking files you didn't mean to check
prettier . --check and eslint . scan the whole working tree. In CI that tree may contain generated output from earlier steps, such as dist/ or coverage/. Prettier 3 respects .gitignore and .prettierignore by default, and ESLint flat config ignores node_modules and .git only. Add an explicit ignores entry to eslint.config.js for build directories:
export default [
{ ignores: ["dist/", "coverage/"] },
// ...the rest of your config
];Making Failures Easy to Fix
When the Prettier check fails, the log lists the unformatted files and tells the developer to run Prettier. Point people at the fix directly. A short note in your contributing guide such as "run npm run lint:fix && npm run format before pushing" resolves most failures in seconds.
You might be tempted to have CI run --fix and --write and commit the result automatically. That approach needs a token with write access, can trigger loops of workflow runs, and changes code under the author's feet. For most teams, a read-only check with a clear error message is simpler and safer.
Speeding Up Large Projects
On large codebases, ESLint dominates the run time. Two options help:
- ESLint's cache.
eslint . --cache --cache-location .eslintcacheskips files that haven't changed since the last run. In CI you must persist that file between runs withactions/cache. Key the cache on the lockfile and config hash, or a rule change won't trigger a re-lint. - Linting only changed files on PRs. This is faster, but it repeats lint-staged's blind spot: config changes don't re-check untouched files. If you do this, still run a full lint on pushes to
main.
For most projects, a full uncached run takes under a minute, and that simplicity is worth more than the saved seconds.
Exercise
Your config sets "no-unused-vars" to "warn". A pull request adds three unused variables, and the CI workflow runs npm run lint with no extra flags. Does the ESLint job pass or fail, and what single change makes CI reject this PR without changing the rule's severity in the config?
Show answer
It passes, because ESLint exits with code 0 when it finds only warnings. Run the script as npm run lint -- --max-warnings 0 in the workflow. ESLint then exits with code 1 when there is any warning, which fails the job.
Exercise
A teammate on Windows reports that npm run format:check lists every file in the project as unformatted, while the same commit passes on the Ubuntu CI runner. What is the likely cause, and how do you fix it for everyone?
Show answer
Git's core.autocrlf setting is checking files out with CRLF line endings, and Prettier's default "endOfLine": "lf" flags them. Commit a .gitattributes file containing "* text=auto eol=lf" so every checkout uses LF, then have the teammate re-checkout the files.
Exercise
In the original single-job workflow, lint and format checks run as consecutive steps. Name one drawback of this layout and one way to fix it.
Show answer
If the ESLint step fails, the Prettier step is skipped, so developers find out about formatting problems only after a second push. Split the checks into two parallel jobs, as shown in this chapter, so both report results in the same run. Another option is to add if: ${{ !cancelled() }} to the second step.