· Sourceplane engineering · Delivery · 4 min read

Environments that tell the truth

If a change can pass staging and fail in production, staging is not telling you the truth. How to build environments that match, promote the same artifact through them, and keep them from drifting.

Every team has heard “it worked on staging”. It is usually said after an incident, by someone who did everything right. The change was tested, reviewed, deployed to staging, checked, and promoted. Then production behaved differently.

When that happens, the problem is not the change. It is that staging was not telling the truth about production. Environments exist to answer one question, “will this work when it reaches users?”, and an environment that answers it wrongly is worse than none, because it gives false confidence.

Why environments drift

Environments rarely start different. They drift, for ordinary reasons:

  • Built by hand. Staging was set up in a console two years ago; production was set up separately; nobody can list the differences.
  • Fixed in place. A setting was changed in production during an incident and never written back anywhere.
  • Different versions. Staging runs a newer database, an older runtime or a different instance size.
  • Different configuration. Feature flags, timeouts and limits were tuned in one place and not the other.
  • Different data. Staging has a few hundred tidy rows; production has years of edge cases.

Each difference is small. Together they mean a passing test in staging says less and less about production.

Declare every environment from the same code

The most effective fix is structural: every environment is built from the same definitions, with only the values that must differ held separately.

  • Infrastructure is declared as code, once, and applied per environment.
  • The differences between environments, such as instance sizes, domain names and replica counts, are explicit variables, in one place, reviewable.
  • Nothing is changed by hand. If production needs a fix during an incident, the fix goes back into code before the incident is closed.

This turns “what is different about staging?” from an investigation into a file you can read.

Build once, promote the same artifact

A common source of surprises is building separately for each environment. Two builds from the same commit can still differ: a dependency resolved differently, a build flag set differently, a cache that was warm in one place.

Build once. Produce one artifact, give it an identity, and promote that exact artifact from environment to environment. What reaches production is byte for byte what passed staging.

Separate configuration and secrets from code

Configuration that differs between environments, such as URLs, credentials and limits, should be injected at deploy time from a configuration store and a secrets manager. It should never be compiled into the artifact or committed to the repository. This is what makes promoting one artifact through several environments possible.

Make data realistic enough

Most “worked on staging” incidents involving data come from staging having too little of it, or data that is too tidy. Options, in increasing order of effort:

  • Seed staging with generated data that covers the shapes and sizes production has.
  • Use anonymised snapshots of production data, refreshed on a schedule.
  • Run critical migrations against a copy of production before running them for real.

Use preview environments where they earn their place

Short-lived preview environments, created for each pull request and destroyed when it merges, are excellent for frontends and for services with few dependencies. For a service that needs half the system around it, they can cost more than they return. Use them where a reviewer genuinely benefits from clicking through a change.

Check the truth after every deploy

Even matching environments need verification. A short smoke test after every deploy, run against the real environment, catches the differences that slipped through: the missing secret, the wrong URL, the route that does not answer. If the smoke test fails, the deploy rolls back automatically.

A checklist

  • Every environment is declared from the same code; differences are explicit variables.
  • Nothing is changed by hand, ever, or it is written back before the day ends.
  • One build, one artifact, promoted through every environment.
  • Configuration and secrets are injected at deploy time.
  • Staging data has the shape and size of production.
  • A smoke test runs after every deploy, and a failure rolls back.

Environments built like this stop being a ritual and start being evidence.


How we help. We set up pipelines and environments that match, with promotion, smoke tests and rollback built in. See CI/CD and environments, or talk to an engineer.

Share:
All articles