Sub topic 2
Defining check-only scripts in your project
This chapter assumes you know basic Git and GitHub pull requests, can use the command line, have used a linter and formatter such as ESLint and Prettier, and can read YAML and JSON. It covers how to expose lint and format checks as named scripts that CI can call without changing any files.
Why CI needs check-only commands
A CI job runs on a temporary machine. If a command rewrites files there, the changes disappear when the job ends, and nobody sees them. A job that "fixes" code and then passes gives you false confidence, because the branch you review still has the original problems.
Check-only mode solves this. The tool reads your files, reports problems, and exits with a non-zero status if it finds any. It never edits anything. The workflow then fails, and the author fixes the problem locally.
Developers still need a way to fix problems on their own machine. So a project usually defines two kinds of command:
- Check commands report problems and change nothing. CI uses these.
- Fix commands rewrite files. Developers run these locally.
Scripts in package.json
In a Node project, the scripts section of package.json gives each command a short name. Here is a setup for ESLint and Prettier.
{
"scripts": {
"lint": "eslint .",
"format": "prettier --write .",
"format:check": "prettier --check ."
}
}Each script behaves as follows:
| Script | Command | Changes files? | Intended use |
|---|---|---|---|
| `lint` | `eslint .` | No | Local and CI |
| `format` | `prettier --write .` | Yes | Local only |
| `format:check` | `prettier --check .` | No | CI |
ESLint without --fix only reports. Prettier needs an explicit choice: --write rewrites files, and --check only reports. The colon in format:check is a naming convention that groups related scripts. It has no special meaning to npm.
Run them with npm run:
npm run lint
npm run format:checkIf format:check finds a badly formatted file, Prettier lists the file and exits with a non-zero code. To repair it, run npm run format and commit the result.
Keep local and CI behavior identical
CI should call the same scripts that developers run. If the workflow typed npx eslint src/ while developers ran npm run lint, the two could check different files and disagree. A named script is one place to change the command, and both sides pick up the change.
The scripts should also share configuration files in the repository, such as eslint.config.js and .prettierrc. Pin tool versions in devDependencies and commit the lockfile, so every machine runs the same version.
What the data flow looks like
The diagram below shows how a developer and CI use the same scripts differently.
Common mistakes
- Using
prettier --writein CI. The job passes, but the pushed code is still unformatted. - Using
eslint --fixin thelintscript. It hides problems locally and edits files in CI. - Forgetting the lockfile. Without it, CI may install a newer tool version with different rules.
- Skipping an ignore file. Without
.prettierignoreand ESLint ignore settings, the tools may scannode_modulesor build output and report problems you do not own.
Exercise
A teammate sets the CI step to npm run format, where format is prettier --write .. The job always passes. Why is this a problem, and which script should CI call instead?
Show answer
prettier --write rewrites files on the temporary CI machine and exits with status 0, so the job passes even when the committed code is unformatted. Those rewrites are lost when the job ends. CI should call npm run format:check, which runs prettier --check ., reports unformatted files, and exits non-zero so the job fails.