Code Quality Gates
ESLint, Prettier, TypeScript strict checks, and SonarCloud integration — automated code quality in every PR
Skills taught: cicd.eslint, cicd.prettier, cicd.typescript, cicd.sonarcloud, cicd.quality-gates
Why code quality belongs in CI
Code reviews are expensive human time. Code quality tools catch whole categories of issues before a human even opens the PR — unused variables, inconsistent formatting, type errors, and known anti-patterns. In CI, these tools become gates: the PR simply cannot merge until the code passes. They also make reviews more productive: reviewers focus on logic and design, not style nits.
ESLint + Prettier in one job
name: Code Quality
on:
pull_request:
push:
branches: [main]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '20', cache: 'npm' }
- run: npm ci
# TypeScript strict type checking
- name: Type check
run: npx tsc --noEmit
# ESLint — fail on any error (warnings allowed)
- name: Lint
run: npx eslint . --max-warnings=0
# --max-warnings=0: even warnings fail the job
# Adjust to a positive number during a codebase cleanup
# Prettier — check formatting without changing files
- name: Format check
run: npx prettier --check .
# Fails if any file would be reformatted
# Fix locally: npx prettier --write .
# Report ESLint results as GitHub annotations (inline in PR diff)
- name: ESLint with annotations
if: always()
run: |
npx eslint . --format=@microsoft/eslint-formatter-sarif \
--output-file eslint-results.sarif || true
- name: Upload ESLint SARIF
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: eslint-results.sarif
wait-for-processing: true
SonarCloud — deep quality analysis
# SonarCloud is free for open source projects
# Setup: sonarcloud.io → link your GitHub repo → get SONAR_TOKEN
- name: SonarCloud scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# SonarCloud checks:
# - Code coverage (fails if below configured threshold)
# - Code smells (anti-patterns, complexity)
# - Duplicate code
# - Security hotspots
# - Bugs found by static analysis
# All configurable in sonar-project.properties
sonar.projectKey=myorg_myrepo
sonar.organization=myorg
# Source files to analyse
sonar.sources=src
sonar.exclusions=**/*.test.ts,**/node_modules/**
# Coverage reports
sonar.javascript.lcov.reportPaths=coverage/lcov.info
# Quality Gate: fails the PR if coverage drops below 80%
# or if new code has any bugs/vulnerabilities
# Configure the threshold in SonarCloud project settings
Tip: Fix the root cause, not the symptom. When ESLint flags an error, understand why the rule exists. @typescript-eslint/no-floating-promises exists because unawaited promises silently swallow errors. no-console exists because console.log in production apps pollutes log infrastructure. Disabling a rule with // eslint-disable-next-line should be a last resort with a comment explaining why.
Frequently Asked Questions
ESLint reports thousands of errors in a legacy codebase. How do we adopt it gradually?
Start with --max-warnings=9999 (very permissive) and commit a baseline. Then use eslint-baseline (or lint-staged for new files only) to only fail on NEW violations. Ratchet the threshold down over time. This lets you adopt quality gates without stopping all development while you fix historical issues.