Sub topic 8
Environments from development to production
This chapter assumes you know basic Git, can build and test a small application, and can read simple YAML. It also assumes you have seen a pipeline that builds and tests code. Here you will see where that code runs once it is built.
What an environment is
An environment is a place where your application runs, together with the configuration, data, and access rules that go with it. The same build can run in several environments. Each one answers a different question.
| Environment | Purpose | Who uses it | Data |
|---|---|---|---|
| Development | Try changes while coding | Developers | Fake or small samples |
| Staging | Check a release candidate under production-like conditions | Developers, QA | Anonymized or synthetic copy |
| Production | Serve real users | Customers | Real data |
Some teams add a test or QA environment between development and staging. Others create a temporary preview environment for each pull request. The names vary, but the idea is the same: a change gains trust as it moves toward production.
Why separate environments exist
Running untested code directly in production puts real users and real data at risk. Separate environments let you find problems where they are cheap to fix. Staging catches faults that unit tests miss, such as a wrong database setting, a missing network rule, or a slow query on realistic data.
Staging only helps if it resembles production. Use the same operating system, runtime version, and deployment method. Scale can differ, but the shape should not.
Configuration changes, code does not
The artifact you deploy should be identical in every environment. What differs is configuration, such as database URLs, API endpoints, and secrets. Keep configuration outside the artifact. Read it from environment variables or a configuration service at start-up.
import os
DATABASE_URL = os.environ["DATABASE_URL"]
LOG_LEVEL = os.environ.get("LOG_LEVEL", "info")
print(f"Connecting to {DATABASE_URL.split('@')[-1]} with log level {LOG_LEVEL}")Run this with DATABASE_URL=postgres://user:pw@staging-db/app python app.py and it connects to staging. Change the variable and the same code targets production. The print statement shows only the host part, so the password is not written to the logs.
Promotion through environments
Promotion means moving the same artifact from one environment to the next after it passes the checks for the current one.
Environments in a pipeline
In GitHub Actions, a job can name an environment. The environment has its own secrets and protection rules, set in the repository settings.
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- run: ./scripts/deploy.sh dist/With environment: production, the job can wait for a required reviewer before it starts. This approval step is what turns automated deployment into continuous delivery. Secrets defined for production are available only to jobs that target it.
Common errors
- Drift: staging and production diverge over time, so staging results stop predicting production behavior. Create both from the same code, such as shared templates.
- Rebuilding per environment: building again for production means you ship something you did not test. Build once and promote.
- Real data in lower environments: copying production data into development exposes personal information. Anonymize it or use synthetic data.
- Shared staging: many teams deploying to one staging environment overwrite each other's changes. Use preview environments or a deployment queue.
Exercise
A team builds the application separately for staging and for production, using the same commit. Staging passes, but production fails. Give two reasons this setup is risky, and say what they should change.
Show answer
The two builds can differ, for example through different dependency versions or build settings, so the tested artifact is not the one that ships. Environment-specific values may also be baked into each build instead of supplied as configuration. They should build once, store the artifact, and promote that same artifact through each environment, supplying environment-specific configuration at deploy time.