Sub topic 6
Staging as a Rehearsal for Production
This chapter assumes you know basic Git, CI concepts, containers, and YAML. You have already seen that one immutable image is built and promoted. Here you will look at the environment that image visits before production: staging.
What staging is for
Staging is an environment that exists to answer one question: will this exact build work in production? It is not a place to develop features or to share half-finished work. Its value comes from being as close to production as you can afford.
Because the artifact is identical in both places, any difference in behavior must come from the environment around it. Staging lets you find those differences while a failure still costs nothing.
What "production-like" means
Production-like does not mean production-sized. It means the things that change behavior are the same kind, even if smaller.
| Aspect | Match production | May differ |
|---|---|---|
| Runtime and OS image | Same base image, same versions | None |
| Orchestrator | Same platform and manifests | Replica count |
| Database engine | Same engine and major version | Data volume |
| Network path | Same ingress, TLS, and proxies | Domain name |
| Configuration shape | Same keys and formats | Values such as URLs |
| Secrets | Same mechanism of delivery | The secret values |
A common failure is a staging database on a different engine, such as SQLite in staging and PostgreSQL in production. Queries that pass in one can fail in the other.
Deploying to staging
The deploy job should be the same code path used for production. Only the target changes. In the pipeline from earlier chapters, both jobs call one script:
#!/usr/bin/env bash
# deploy.sh <environment> <image>
set -euo pipefail
env="$1"
image="$2"
kubectl --namespace "$env" set image deployment/app app="$image"
kubectl --namespace "$env" rollout status deployment/app --timeout=120sThe script changes only the image. Everything else comes from the environment's own configuration. If staging deploys with this script and production deploys with a different one, you have rehearsed the wrong thing.
deploy-staging:
needs: build
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh staging "${{ needs.build.outputs.image }}"The needs: build line means staging receives the image reference that the build job produced. Nothing is rebuilt.
What to rehearse
Staging is more useful when you treat it as a full dress rehearsal and not just a place where the container starts.
- The deployment itself. Does the rollout finish within the timeout? Do health checks pass?
- Configuration and secrets. Are all required keys present and valid for this environment?
- Migrations. Does the database change apply to a copy of realistic data?
- Integrations. Do calls to third-party sandboxes, queues, and internal services work?
- Rollback. Can you return to the previous revision? Practice it here, not for the first time during an incident.
kubectl --namespace staging rollout undo deployment/app
kubectl --namespace staging rollout status deployment/app --timeout=120sData in staging
Realistic data finds realistic bugs. Empty tables hide slow queries and edge cases. Two approaches are common:
- Generate synthetic data at production-like volume.
- Copy production data after anonymizing it.
Keeping staging honest
Staging drifts. Someone changes a setting by hand, a service is pinned to an old version, or a test fixture is left behind. Over time, a green staging run stops predicting production.
Reduce drift with these habits:
- Define staging and production from the same infrastructure code, with different variable files.
- Forbid manual changes. Fix problems by changing the code and redeploying.
- Redeploy staging from scratch on a schedule to expose hidden state.
- Compare configuration between environments and treat unexplained differences as bugs.
Where staging sits in the flow
The deploy-production job lists deploy-staging in needs, so a failed staging deploy blocks production automatically. The next chapter covers what the checks in the diamond should contain.
Limits of staging
Staging reduces risk but cannot remove it. Production has real traffic patterns, real data shapes, and scale that staging rarely matches. Some problems only appear there. This is why later chapters add gradual rollouts and rollback plans. Staging catches the failures you can predict. Production strategies handle the rest.
Exercise
A team's staging environment uses a single replica and a small database, but the same base image, orchestrator manifests, and database engine as production. A release passes staging but fails in production under load. Is staging at fault, and what would you change?
Show answer
Not necessarily. Staging matched production in the aspects that affect correctness, and size differences are an accepted limit. The failure came from load, which staging did not exercise. Add a load or performance check in staging, and use a gradual production rollout so a load-related failure affects only a small share of users and can be rolled back quickly.
Exercise
Why should staging and production use the same deploy script?
Show answer
A shared script means the staging deployment rehearses the exact steps that production will run. If the scripts differ, a passing staging deploy says nothing about whether the production steps will work.