Sub topic 1
Why teams need CI/CD
Prerequisites
This chapter assumes you can do the following:
- Use Git for commits, branches, and pull requests.
- Build and run the tests of a small application.
- Run basic commands in a terminal.
- Read simple YAML.
The problem: getting code to users by hand
Writing code is only part of delivering software. The code also has to be combined with teammates' work, built, tested, and put in front of users. When these steps are manual, they cause predictable problems.
- Late integration. Developers work on long-lived branches for days or weeks. When they finally merge, their changes conflict with each other, and nobody remembers the details well enough to resolve the conflicts quickly.
- "Works on my machine." Each developer has a slightly different setup, such as a different runtime version or different installed libraries. A change can pass locally and fail everywhere else.
- Skipped or inconsistent checks. Tests that depend on someone remembering to run them are sometimes not run. Under deadline pressure, they are the first thing dropped.
- Risky, rare releases. If a release means following a long checklist, teams do it as seldom as possible. Each release then bundles many changes, so a failure is harder to diagnose.
- Slow feedback. A bug found a week after it was written costs far more to fix than one found within minutes.
What CI/CD does about it
CI/CD is a set of practices and tools that automatically build, test, and release software each time the code changes. The name covers three related ideas, which later chapters treat in detail:
- Continuous integration (CI): every change is merged into a shared branch often, and each merge is built and tested automatically.
- Continuous delivery: every change that passes the checks is kept in a state where it can be released at any time, usually with a manual approval step.
- Continuous deployment: every change that passes the checks is released to production automatically.
The idea behind all three is the same. Replace rare, manual, error-prone steps with frequent, automated, repeatable ones.
A before-and-after view
The diagram below contrasts the two ways of working.
What a first automated check looks like
You do not need to understand every line yet. This GitHub Actions workflow runs the tests on every push to main and on every pull request, so a broken change is flagged before anyone has to ask.
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 file does three things that address the problems above. It runs on a clean machine, so "works on my machine" no longer hides failures. It runs the same steps every time, so checks cannot be skipped. It reports a result on each pull request, which shortens feedback to minutes.
What you gain
| Without CI/CD | With CI/CD |
|---|---|
| Integration happens late and in large batches | Integration happens many times a day in small changes |
| Test runs depend on memory and discipline | Tests run automatically on every change |
| Releases are rare and stressful | Releases are routine and repeatable |
| Bugs are found long after they are written | Bugs are found within minutes |
Smaller, more frequent changes are also easier to reverse. When a release contains one change instead of fifty, finding the cause of a failure is much faster.
Exercise
A team merges to the main branch once every two weeks, and each release is built and tested by hand. Name two problems this causes and one CI/CD practice that addresses each.
Show answer
Possible answers: (1) Late integration produces large merge conflicts. Merging small changes often into a shared branch, with an automatic build on each merge, addresses this. (2) Manual testing is skipped or inconsistent. Running the automated tests on every push and pull request addresses this. Rare, risky releases are also valid, addressed by automating the release steps so they can be repeated often.
Exercise
In the workflow shown above, which lines make sure the tests run on a clean, identical machine each time, and why does that matter?
Show answer
The runs-on: ubuntu-latest line starts a fresh machine for each run, and npm ci installs the exact dependencies from the lock file. Together they remove differences between developers' machines, so a test failure points to the code and not to a local setup.