Continuous Delivery & Deployment
Master Continuous Delivery (CD): automated release packaging, staging environment validation, approval gates, and zero-downtime deployment.
In the early days of software, releasing a new version was often a stressful, weekend-long ordeal. Teams scheduled maintenance windows where servers were taken down for hours. An engineer worked through a long checklist, copying files manually, running database scripts, and updating configuration files on live servers. Inevitably, late at night, something went wrong. A setting was missing or a database change failed to apply. The team spent hours troubleshooting, and sometimes had to roll back the entire release in exhaustion by Monday morning. Continuous Delivery was created to make software deployments routine and predictable. It ensures that software is always in a release-ready state, and that deploying to any environment (from a test server to production) uses the exact same automated, dependable pipeline.
Definition
Continuous Delivery is a software engineering approach where teams produce software in short cycles, ensuring that code can be reliably released to production at any time through automated packaging, environment setup, and testing, with the final release triggered on demand.
Why It Matters
When deployments are rare and manual, they carry high risk. Because they carry high risk, teams delay them. Because teams delay them, each release contains hundreds of changes. And because releases are huge, they frequently fail. Continuous Delivery breaks this cycle by making releases small, automated, and routine. Deploying daily or weekly removes deployment anxiety, reduces business risk, and gets new features into users' hands much faster.
How It Works
Continuous Delivery functions as an automated promotion pipeline built on several core ideas: 1. Immutable Artifact Promotion: An application is built and packaged once (such as a Docker container image) during the CI stage. That exact same image is then promoted sequentially through Development, Staging, and Production. Differences like database passwords or hostnames are supplied at runtime via environment variables or secret managers. 2. Staging Environment Parity: The staging environment should closely match production in operating system, database versions, and network structure. Automated integration and smoke tests run in staging to catch environmental issues before production. 3. Continuous Delivery vs Continuous Deployment: • Continuous Delivery: The pipeline automates building, testing, and deploying to staging. The application is ready to deploy to production at any time, but requires an explicit approval step or button click. • Continuous Deployment: There is no manual approval gate. Every commit that passes the automated pipeline deploys straight into live production within minutes. 4. Zero-Downtime Deployment Patterns: Modern pipelines use strategies like Blue-Green, Canary, or Rolling updates behind load balancers so that new application instances take over traffic smoothly without disconnecting users.
The Continuous Delivery Environment Promotion Flow
Key Points
- Software should stay in a deployable state throughout its development.
- Build once, deploy everywhere: do not recompile an application for different target environments.
- Continuous Delivery uses a human approval step before production; Continuous Deployment is completely automated.
- Database changes must be backward-compatible to allow zero-downtime rolling updates.
- Automated rollbacks ensure that if health checks fail, the system restores the previous version without human intervention.
Steps
Package Applications in Containers
Use Docker to package your application and its dependencies into a consistent container image that runs identically everywhere.
Keep Configuration Outside the Code
Follow 12-Factor App principles by supplying environment variables and secrets at runtime so the same container image runs in all environments.
Automate Staging Deployments
Set your pipeline to deploy passing builds to a staging environment automatically, followed by automated smoke tests.
Use Backward-Compatible Database Migrations
Apply the Expand/Contract pattern for database changes so older and newer application versions can run alongside each other during updates.
Set Up Automatic Rollback Triggers
Define health checks so that if a new release experiences elevated error rates, traffic immediately reverts to the previous stable release.
Examples
The Train Schedule Analogy
Traditional deployments are like chartering a tour bus: you spend weeks organizing passengers and luggage, and if one person is late, the entire trip is delayed. Continuous Delivery is like a metro train arriving every three minutes: if a feature misses one train, it simply catches the next one without holding up anyone else.
Real-World Examples
Automated Canary Deployments at Scale
Large cloud platforms deploy microservice updates continuously using automated canary analysis. A new software version receives a tiny slice of live user traffic while monitoring tools compare latency, error rates, and CPU usage against the existing version. If metrics remain healthy, the new version gradually expands to 100% of traffic.
Technical Examples
name: Continuous Delivery Release Pipeline
on:
push:
branches: [main]
jobs:
# 1. DEPLOY TO STAGING
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
environment: staging
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Deploy image to staging cluster
run: |
echo "Deploying ghcr.io/myorg/api:${{ github.sha }} to Staging..."
# kubectl apply -f k8s/staging/
- name: Run automated health and smoke tests
run: |
echo "Checking staging health endpoint..."
curl --fail --retry 5 https://staging-api.example.com/healthz
# 2. DEPLOY TO PRODUCTION (Protected Environment)
deploy-production:
name: Deploy to Production
needs: deploy-staging
runs-on: ubuntu-latest
environment:
name: production # Requires designated team review in GitHub settings
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Roll out update to production
run: |
echo "Deploying ghcr.io/myorg/api:${{ github.sha }} to Production..."
# kubectl set image deployment/api api=ghcr.io/myorg/api:${{ github.sha }} -n prod
# kubectl rollout status deployment/api -n prod --timeout=120sContinuous Delivery vs Continuous Deployment
| Aspect | Continuous Delivery | Continuous Deployment |
|---|---|---|
| Release to Production | Triggered manually when business team decides | Triggered automatically when all tests pass |
| Approval Gate | Human clicks an approval button in the UI | Automated canary evaluation and health metrics |
| Risk Management | Coordinated around business schedules | Managed through automated traffic percentages and quick rollbacks |
| Regulatory Fit | Well-suited for strict compliance rules (finance, healthcare) | Well-suited for consumer web apps, SaaS, and public APIs |
| Testing Requirement | High automated test coverage | Extensive automated tests with complete confidence in test results |
Progressive Delivery & Managing Blast Radius
Advantages
- Low Deployment Stress: Automated, repeatable scripts replace risky manual checklists.
- Faster Delivery: New features and bug fixes reach users in hours or days instead of months.
- Zero Downtime: Rolling and blue-green updates eliminate user-facing maintenance windows.
- Manageable Changes: Smaller updates mean smaller risks and straightforward rollbacks.
Limitations
- Requires Database Care: Database schemas must be updated using non-breaking, backward-compatible steps.
- Staging Maintenance: Keeping staging environments closely matched with production requires regular attention.
Interview Questions
How do you perform zero-downtime deployments when a database schema change is needed?
Zero-downtime database changes use the Expand and Contract pattern across separate releases: 1. Expand: Add the new column or table while leaving old fields intact so running code continues working. 2. Dual Write: Deploy code that writes to both old and new columns. 3. Backfill: Run a background job to copy older records to the new column. 4. Read New: Update the application to read from the new column. 5. Contract: Once the old column is no longer used anywhere, remove it in a final cleanup migration.
Practice Questions
- Walk through how you would rename a database column from 'phone' to 'mobile_number' without taking down the website.
