How to Lint Only Changed Files in CI
Passing only the changed files to ESLint keeps PR lint time proportional to the diff, though it can miss violations that pre-existed in untouched files.
Compute the changed file list from the diff, filter to lintable extensions, and pass them to ESLint. This is ideal for fast PR feedback; keep a full lint on main so latent issues still surface.
Steps
- Get changed files with tj-actions/changed-files or git diff.
- Filter to lintable extensions with a
files:glob. - Pass the list to ESLint only when there is at least one file.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 }
- uses: actions/setup-node@v4
with: { node-version: 20 }
- run: npm ci
- id: changed
uses: tj-actions/changed-files@v45
with:
files: '**/*.{js,ts,tsx}'
- if: steps.changed.outputs.any_changed == 'true'
run: npx eslint ${{ steps.changed.outputs.all_changed_files }}Gotchas
- Linting only the diff will not catch a rule violation that already lives in a file you did not touch; run a full lint on the default branch.
- A rule change (config edit) should trigger a full lint, since it can flag files that were previously clean.
Related guides
How to List Changed Files With tj-actions/changed-files in CIGet the exact list of files changed in a push or pull request with tj-actions/changed-files, then loop over t…
How to Select Tests by Impact Analysis in CIRun only the tests affected by a change using test impact analysis, mapping changed files to the tests that e…
How to Skip CI for Docs-Only ChangesAvoid running full CI for documentation or comment-only changes using paths-ignore, so README and markdown ed…