Sub topic 2
Continuous integration: merging early and verifying every change
This chapter assumes you know basic Git (commits, branches, pull requests), can build and test a small application from the command line, and can read simple YAML. It covers the working model of continuous integration (CI) and the errors teams commonly make with it.
The working model
Continuous integration is the practice of merging every developer's changes into a shared main branch often, and automatically verifying each change when it arrives. "Often" usually means at least daily. "Verifying" means a server builds the code and runs the tests without anyone starting it by hand.
The idea is to find conflicts and breakage while the change is small. A bug found ten minutes after a commit is easy to trace. A bug found after three weeks of diverging branches is not.
A CI system runs this loop for each change:
- A developer pushes a commit or opens a pull request.
- The CI server notices the event and starts a run on a clean machine.
- The run checks out the code, installs dependencies, builds, and tests.
- The result (pass or fail) is reported back on the commit or pull request.
- A failure is fixed immediately, before more work piles on top.
A minimal CI workflow
This GitHub Actions workflow runs on every push to main and on every pull request.
name: ci
on:
push:
branches: [main]
pull_request:
jobs:
test:
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 testThe on block defines the triggers. runs-on selects a fresh virtual machine. Each step runs in order, and if one fails, the run stops and is marked failed. npm ci installs exactly the versions in the lockfile, which keeps runs repeatable. This is a better choice than npm install in CI.
Habits that make CI work
The tooling is only half of CI. The practice depends on team habits:
- Integrate small changes frequently. Short-lived branches (a day or two) avoid large, painful merges.
- Keep main green. The main branch should always build and pass its tests.
- Fix a broken build first. If main goes red, repairing it takes priority over new work.
- Make the check required. Configure branch protection so a pull request cannot merge until the CI run passes.
- Keep it fast. Aim for feedback in around ten minutes or less. Slow runs push people to batch changes, which defeats the purpose.
Common errors
| Problem | Why it hurts | Typical fix |
|---|---|---|
| Long-lived feature branches | Merges are large and conflicts are hard to resolve | Merge small pieces daily, hiding unfinished work behind a flag |
| Tests pass locally but fail in CI | The local machine differs from the CI machine | Pin tool versions, use a lockfile, avoid hidden local state |
| Flaky tests | Random failures teach people to ignore red builds | Quarantine and fix them, do not just re-run |
| Red builds left alone | Every new change is hard to judge | Treat a broken main as urgent |
| CI is optional | Unverified changes reach main | Require passing checks before merge |
CI by itself stops at verification. Later chapters build on it by adding artifacts, delivery, and deployment.
Exercise
A teammate's pull request passes all tests on their laptop but fails in the CI run with a missing-package error. Name two likely causes and one change that prevents the problem.
Show answer
Likely causes: a dependency was installed locally but not declared in the project's manifest or lockfile, or the laptop uses a different tool version than CI. Prevent it by declaring all dependencies, committing the lockfile, using npm ci in the CI run, and pinning the runtime version (for example node-version: 20).