Concept Explanation
Imagine you have a team of 10 developers. If everyone just "Uploads" their files to the server, it becomes a mess. CI (Continuous Integration): Every time you save code, a robot automatically runs your tests. If a test fails, you aren't allowed to merge your code. CD (Continuous Deployment): Once the tests pass, a robot automatically pushes your code to the live server.
This ensures that the live website is ALWAYS working and ALWAYS has the latest features.
The deeper reason CI matters is not automation for its own sake. It is that bugs get exponentially more expensive the later you find them. A failing test on your laptop costs thirty seconds. The same bug found in code review costs an hour of someone else's attention. Found in production, it costs an incident, a rollback, and a customer email. CI is a machine that moves discovery as early as possible.
A useful distinction: Continuous Delivery means every green build could ship, but a human presses the button. Continuous Deployment means it ships automatically. Most teams should start with delivery and graduate.
Syntax or Core Structure
A basic GitHub Action (.github/workflows/main.yml)
name: Node.js CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm install
- run: npm test
A realistic pipeline
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
verify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # reuse the dependency cache
- run: npm ci # reproducible install from the lockfile
- run: npm run lint
- run: npm run typecheck
- run: npm test -- --coverage
- run: npm run build
deploy:
needs: verify # only runs if verify passed
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production # can require a manual approval
steps:
- uses: actions/checkout@v4
- run: ./scripts/deploy.sh
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
The Stages, and Why They Are In That Order
Cheap and fast checks go first, so you fail in seconds rather than minutes:
lint -> typecheck -> unit tests -> build -> integration/e2e -> deploy
~5s ~15s ~30s ~1m ~5m
Then add two safety nets around the deploy itself: run the migration before the new code goes out, and keep the previous build one command away so a rollback is not an archaeology project.
Real World Example
The branch protection rule that saved a weekend. A team enabled "require status checks to pass before merging" plus a required review. A well-intentioned refactor removed a null check that only one integration test covered. The test failed, the merge button stayed grey, and the author fixed it in four minutes. Without the gate, that change would have merged on a Friday afternoon and produced a paging alert at 2am. CI's value is mostly invisible: it is measured in the incidents that did not happen.
Common Mistakes
- Using
npm installinstead ofnpm ci.installmay resolve different versions than your lockfile, so CI stops being a faithful reproduction of anyone's machine. - Putting secrets in the workflow file. Anything in the repo is public to everyone with read access, and it is in the history forever. Use repository secrets.
- Echoing a secret in a debug step. Logs are often broadly readable. Never
echoa token, even temporarily. - Letting a flaky test live. One test that fails 5% of the time trains the whole team to re-run the job without reading it, which defeats the point of having tests.
- A pipeline that takes 40 minutes. Developers start batching changes to avoid the wait, and large batches are exactly what CI exists to prevent. Parallelise and cache.
- Deploying without a rollback path. Automating the way forward and not the way back is half a system.
- No branch protection. A pipeline that reports failures nobody is obliged to act on is decoration.
Best Practices
- Run lint, typecheck, test and build on every pull request; deploy only from the main branch.
- Cache dependencies and split slow suites across parallel jobs to keep the whole run under about ten minutes.
- Store credentials as encrypted secrets scoped to an environment, and rotate them.
- Pin action versions (
actions/checkout@v4) rather than tracking a moving branch. - Make the pipeline the only way to production. If a human can bypass it with an SFTP client, eventually someone will.
- Deploy small and often. Ten small deploys are far safer than one large one.
Practice Exercises
- The Workflow: Look at the YAML file above. Which step would cause the whole process to fail if your tests were broken?
- The Secret Key: Research why you should never put "API Keys" inside your code. How do "GitHub Secrets" help with this?
- The Semantic Versioner: Research "SemVer" (v1.2.3). What is the difference between a "Major," "Minor," and "Patch" update?