Version Control & CI/CD
Everything in Version Control
If a change to production does not appear in version control, nobody can review it, reproduce it or undo it reliably. Mature teams keep almost everything in repositories: application code, infrastructure definitions, configuration, dashboards, alert rules, runbooks and the pipelines themselves.
What Belongs in the Repository
- Yes: source code, Dockerfiles, infrastructure as code, deployment manifests, pipeline definitions, configuration templates, database migrations, documentation.
- No: secrets (store references to a secret manager instead), build outputs, large binary artifacts (use an artifact registry), and environment-specific values that change without review.
Protect the main branch: require pull requests, passing checks and at least one review, and disallow force pushes. These rules are your change-approval process, recorded automatically.
Make Changes Traceable
Every running artifact should be traceable to a commit, and every commit to a reason. Tag images and packages with the commit SHA or release version, reference the ticket or incident in commit messages, and expose the running version through an endpoint or a label. During an incident, “which commit is running?” should take seconds to answer.
Branching Strategies
A branching strategy decides how long changes live apart from each other before they are integrated. The longer they live apart, the larger and riskier the eventual merge and release.
Trunk-Based Development
Developers integrate small changes into the main branch (the trunk) at least daily, using short-lived feature branches that last hours or a few days. The trunk is always releasable. Incomplete features are merged behind feature flags rather than kept on long branches. Research such as the DORA reports associates trunk-based development with higher delivery performance, and it is the natural fit for continuous delivery.
Release Branches and GitFlow
Some products must support several released versions at once — installed software, mobile apps, libraries. They cut a release branch per version and backport fixes to it, as described in Release Management. GitFlow adds long-lived develop and release branches on top. That structure suits scheduled releases, but for a continuously deployed web service it mostly adds merge work and delays.
Pipelines as Quality Gates
A pipeline is a sequence of automated gates. Each gate either promotes the change to the next stage or stops it with a clear reason. The gates replace manual sign-offs with checks that run on every change.
Figure: Automated CI/CD promotion gates with canary health checks and automatic rollback.
Typical Gates
| Stage | Gate | Fails on |
|---|---|---|
| Commit | Lint, unit tests, build, secret scan | Broken code, leaked credentials |
| Integration | Integration and contract tests, dependency and image vulnerability scan | Broken interfaces, known critical vulnerabilities |
| Staging | Deploy, migrations, smoke and end-to-end tests | Deployment or configuration errors |
| Production | Gradual rollout with automated health checks | Error rate or latency regressions |
Keep the commit stage fast — under about ten minutes — so developers wait for it instead of switching context. Move slow suites to later stages that run in parallel.
A Deployment Pipeline
This GitHub Actions workflow builds an image once, tags it with the commit SHA, and deploys the same image to staging and then to production. Only one production deployment runs at a time, and production requires the protected environment's approval rules:
name: Deliver
on:
push:
branches: [main]
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
outputs:
image: ${{ steps.meta.outputs.image }}
steps:
- uses: actions/checkout@v4
- id: meta
run: echo "image=ghcr.io/${{ github.repository }}:${{ github.sha }}" >> "$GITHUB_OUTPUT"
- run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
- run: docker build -t "${{ steps.meta.outputs.image }}" .
- run: docker push "${{ steps.meta.outputs.image }}"
staging:
needs: build
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- run: ./deploy/deploy.sh staging "${{ needs.build.outputs.image }}"
- run: ./deploy/smoke-test.sh https://staging.example.org
production:
needs: [build, staging]
runs-on: ubuntu-latest
environment: production # protected: required reviewers, main branch only
concurrency:
group: production
cancel-in-progress: false # never cancel a deployment halfway
steps:
- uses: actions/checkout@v4
- run: ./deploy/deploy.sh production "${{ needs.build.outputs.image }}"
- run: ./deploy/smoke-test.sh https://www.example.org
The scripts in deploy/ hold the platform-specific steps, which keeps the pipeline readable and lets operators run the same commands by hand in an emergency.
Deployment Strategies
How a new version replaces the old one determines how many users a bad release can hurt and how quickly it can be undone.
Rolling, Blue-Green and Canary
| Strategy | How it works | Rollback | Cost |
|---|---|---|---|
| Recreate | Stop all old instances, start new ones. | Redeploy old version; downtime both ways. | Cheapest; causes downtime. |
| Rolling | Replace instances a few at a time behind the load balancer. | Roll forward the old version the same way. | Low; old and new run side by side briefly. |
| Blue-green | Start the full new environment (green) beside the old (blue), then switch traffic. | Switch traffic back instantly. | Double capacity during the switch. |
| Canary | Send a small share of traffic (1–5%) to the new version, compare metrics, then increase. | Route the canary share back to the old version. | Needs traffic splitting and good metrics. |
Every strategy except recreate runs two versions at the same time, so both must work with the same database schema and the same message formats. Plan migrations with the expand-and-contract pattern described in Data Modeling.
Feature Flags
Feature flags separate deploying code from releasing a feature. The code ships turned off, then is enabled for internal users, a percentage of customers, and finally everyone — and can be turned off in seconds without a deployment. Treat flags as inventory with a cost: give each one an owner and a removal date, because stale flags multiply the code paths to test.
Rollback Practice
A deployment process is only as good as its way back. Decide in advance how each kind of change is undone, and practice it while nothing is on fire.
Roll Back or Roll Forward
Rolling back redeploys the previous known-good artifact; it is the fastest, safest response to a bad release and should be a single command or button. Rolling forward ships a new fix; it is appropriate when the problem is small and understood, or when rollback is impossible — for example, after a migration that dropped a column. Make the rule explicit: if users are affected and the cause is not obvious within minutes, roll back first and investigate afterwards.
Keep Rollback Possible
- Keep previous artifacts in the registry for a defined period; never overwrite a published tag.
- Keep database migrations backward compatible for at least one release, so the previous version still runs.
- Record the version deployed to each environment, so “the previous version” is unambiguous.
- Automate rollback on failed health checks after deployment where the platform supports it.
- Rehearse rollback in staging regularly and time it; the result belongs in the service's runbook.
Practice Lab
Use a small web application in a repository with CI enabled and two environments, which can be two containers on one machine.
Build a Gated Pipeline
- Protect the main branch with required checks and one review.
- Create a pipeline that builds an artifact tagged with the commit SHA and deploys it to staging, then to production after a smoke test.
- Break the smoke test on purpose and confirm that production is never deployed.
Practice a Rollback
- Deploy two versions, then introduce a bug in a third that makes the health endpoint fail.
- Roll back to the second version using one command, and measure the time from decision to recovery.
- Hide the third version's change behind a feature flag, redeploy it disabled, and enable it only for yourself.