Sub topic 5
Writing your first lint and format workflow
This chapter builds the workflow file that runs your lint and format checks on every pull request and every push to main. It assumes you already have lint and format:check scripts in package.json, know basic YAML, and have opened a pull request on GitHub. The examples use ESLint and Prettier. The same structure works for other tools.
The scripts the workflow calls
The workflow runs npm scripts instead of calling the tools directly. That way the commands in CI are the same ones developers run locally.
{
"scripts": {
"lint": "eslint .",
"format": "prettier --write .",
"format:check": "prettier --check ."
}
}lint and format:check only report problems. format rewrites files, so it is for local use and CI never calls it.
The complete workflow
Save this file as .github/workflows/lint.yml. GitHub only reads workflows from that directory.
name: Lint
on:
pull_request:
push:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run format:checkReading it top to bottom
nameis the label shown in the Actions tab and on pull requests.onlists the events that start the workflow.pull_requestruns it when a pull request is opened or updated.pushwithbranches: [main]runs it after a merge. Without the branch filter, every push to every branch would run it a second time alongside the pull request run.jobsholds one job namedlint. A job is a group of steps that runs on one fresh virtual machine.runs-on: ubuntu-latestpicks the machine image.stepsrun in order. A step either uses a prebuilt action (uses) or runs a shell command (run).
What each step does
actions/checkout@v4copies your repository onto the runner. The runner starts empty, so nothing works without this step.actions/setup-node@v4installs Node 20. Thecache: npmoption saves npm's download cache between runs, keyed on your lockfile.npm ciinstalls exactly whatpackage-lock.jsonlists. Unlikenpm install, it fails if the lockfile andpackage.jsondisagree, which is what you want in CI.npm run lintruns ESLint.npm run format:checkruns Prettier in check mode.
Seeing a failure
Open a pull request with a deliberately unused variable, or a file with inconsistent indentation. The job turns red, and the log of the failing step shows the tool's output. Because steps run in order, a lint failure stops the job before format:check runs. You see only one kind of problem per run. The chapter on parallel jobs shows how to report both at once.
Common mistakes
- Wrong folder. A file in
.github/workflow/(singular) or at the repository root never runs. - Tabs in the YAML. YAML allows only spaces for indentation.
- Missing
checkout. The linter reports that it found no files, or the install fails with a missingpackage.json. - Using
npm run format. This rewrites files on the runner, then discards the changes when the job ends. The check passes without telling you anything.
Exercise
Your workflow runs npm run format instead of npm run format:check. A teammate pushes a badly formatted file. Does the job fail, and why does this make the check useless?
Show answer
The job passes. prettier --write fixes the formatting on the runner and exits with status 0, so nothing signals a problem. The fixes are lost when the runner is discarded, and the badly formatted file stays in the repository. prettier --check exits non-zero when files differ from the expected format, which is what fails the job.
Exercise
Why does the push trigger include branches: [main] while pull_request has no filter?
Show answer
Pull requests already run the workflow on every update, so also running it on every push to a feature branch would duplicate each run. Limiting push to main checks the merged result once after merging and avoids the duplicates.