Sub topic 3
Automating the build
This chapter assumes you can use Git, build and run a small application locally, and read simple YAML.
What a build is
A build turns source code into something that can run. It usually installs dependencies, compiles or bundles the code, and writes the result to an output folder such as dist/. Build automation means a server runs those steps for you. It starts from a clean machine, uses only what is in the commit, and runs the same commands every time.
Why automate it
- Repeatability. A clean machine has no leftover files or global tools. If the build works there, it does not depend on your laptop.
- Fast feedback. A broken build shows up minutes after a push, while the change is still fresh in your mind.
- Shared knowledge. The build steps live in the repository. A new teammate does not need to learn them from a wiki page.
The steps of an automated build
Most automated builds follow the same sequence.
If any step fails, the build stops and reports a failure. Nothing after that step runs.
A build job
This workflow builds a Node.js project on every push to main and on every pull request.
name: build
on:
push:
branches: [main]
pull_request:
jobs:
build:
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 buildEach part controls part of the build:
actions/checkout@v4copies the commit's files onto the runner. A fresh runner has no code until you do this.node-version: 20pins the tool version, so the build does not change when a newer default appears.cache: npmsaves npm's download cache between runs. This makes installs faster. It does not savenode_modules.npm ciinstalls exactly the versions inpackage-lock.json. It fails if the lockfile andpackage.jsondisagree.npm installcan quietly change versions.npm run buildruns the build script defined inpackage.json.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Passes locally, fails in CI | An undeclared dependency, an uncommitted file, or a different tool version | Declare every dependency, commit needed files, and pin tool versions |
| `npm ci` fails | `package.json` and `package-lock.json` are out of sync | Run `npm install` locally and commit the updated lockfile |
| Build output changes with no code change | Unpinned versions such as `latest` | Pin versions and rely on the lockfile |
| Builds are slow | Every run downloads all dependencies again | Turn on dependency caching |
Check your understanding
Exercise
A build passes on your laptop but fails in CI with "module not found". Name two likely causes and one way to prevent the problem.
Show answer
One cause is a dependency you installed locally but never added to package.json. Another is a file that exists on your machine but was never committed. The CI runner starts clean and only has what is in the commit, so it exposes both. To prevent it, run npm ci and the build on a fresh clone before you push, or let CI do that on every pull request.
Exercise
Why does the example use npm ci instead of npm install?
Show answer
npm ci installs exactly the versions in the lockfile and fails if the lockfile does not match package.json. This makes the build repeatable. npm install can change versions, so two builds of the same commit could differ.