Skip to content
Latchkey

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

Run this faster and cheaper on Latchkey managed runners. Start free →