How to Run a Prettier Format Check in GitHub Actions
Formatting debates waste reviews; prettier --check makes the formatter the arbiter and fails the build when code drifts.
Run prettier --check . so unformatted files exit non-zero, turning the PR check red until the author runs prettier --write.
Steps
- Install dependencies with
npm ci. - Run
prettier --check .against the repository (respecting.prettierignore). - Let the non-zero exit fail the job so unformatted code is blocked.
Workflow
.github/workflows/prettier.yml
name: Prettier
on: [pull_request]
jobs:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: npm
- run: npm ci
- run: npx prettier --check .Notes
- Keep a
.prettierignoreso generated files do not trip the check. - On Latchkey managed runners format checks run cheaper and self-heal if a runner drops.
Related guides
How to Run ESLint and Annotate PRs in GitHub ActionsRun ESLint in GitHub Actions and surface violations as inline PR annotations so reviewers see each problem on…
How to Run a TypeScript Typecheck Job in GitHub ActionsAdd a dedicated TypeScript typecheck job to GitHub Actions with `tsc --noEmit` so type errors fail CI separat…