8.4 Environments — dev, staging, production
Why: the problem
Think about how Zomato would test a new payment screen. They would not try it first on real customers with real money. They try it on a developer's test server. Then on a copy of the real system with fake data. Only then on the real app. These three places are called environments: dev, staging and production. Dev is where developers play and break things. Staging looks like production, with the same settings, but no real users. Production, or prod, is the real thing that customers use. Many big outages happen because a change worked on a laptop but staging was skipped. A different database version. A missing setting. A wrong URL. The pipeline should push the same artifact through these places in order. Only the settings change: the database address, the number of servers, the log level. The code does not change.
What we will build
You will run the badge API in three environments using one image and three small settings files. You will use docker compose with an env file per environment, then map them to GitHub Actions environments, GitLab environments and Jenkins stages. You will also add a smoke test after each deploy.
What: every term in plain English
- Environment — A place where the app runs, with its own servers and settings.
- dev — A playground for developers. Can break any time.
- staging — A copy of production with fake or masked data. The last check before users.
- production (prod) — The real system for real users.
- Config — Settings that change per environment, like DB_URL or LOG_LEVEL.
- Env file — A file of KEY=value lines, loaded as environment variables.
- Promotion — Moving the same artifact from one environment to the next.
- Smoke test — A tiny check right after deploy, like calling /health.
- Parity — How similar staging is to production. More parity, fewer surprises.
How: step by step
-
Make three env files
Create env/dev.env, env/staging.env, env/prod.env from Example 1What it does: Only settings differ. No code here.
Output
(3 files) -
Read config in the app
Add the os.environ lines from Example 1What it does: The app reads its settings at start.
Output
(app.py updated) -
Run dev
docker compose --env-file env/dev.env up -dWhat it does: Starts the app with dev settings.
Output
Container badge-api-app-1 Started -
Smoke test dev
curl -s localhost:8000/healthWhat it does: Is it alive?
Output
{"env":"dev","status":"ok"} -
Run staging with the same image
docker compose --env-file env/staging.env up -dWhat it does: Same image tag, different settings.
Output
Container badge-api-app-1 Started -
Smoke test staging
curl -s localhost:8000/healthWhat it does: Now env says staging.
Output
{"env":"staging","status":"ok"}
Example 1: One image, three env files
Stack. Docker Compose
Goal. One image, three env files.
env/dev.env
APP_ENV=dev
LOG_LEVEL=debug
DB_URL=postgresql://badge:devpass@db-dev:5432/badge
IMAGE_TAG=1.4.0-a3f2c1d
env/staging.env
APP_ENV=staging
LOG_LEVEL=info
DB_URL=postgresql://badge:CHANGE_ME@db-staging:5432/badge
IMAGE_TAG=1.4.0-a3f2c1d
env/prod.env
APP_ENV=prod
LOG_LEVEL=warning
DB_URL=from-secret-store
IMAGE_TAG=1.4.0-a3f2c1d
compose.yaml
services:
app:
image: ravi/badge-api:${IMAGE_TAG}
ports: ["8000:8000"]
environment:
APP_ENV: ${APP_ENV}
LOG_LEVEL: ${LOG_LEVEL}
DB_URL: ${DB_URL}
app.py (health shows env)
import os
ENV = os.environ.get("APP_ENV", "dev")
@app.get("/health")
def health():
return jsonify(status="ok", env=ENV)
What each line does
IMAGE_TAG is the same in all three— Same artifact everywhere. This is promotion.LOG_LEVEL changes— More logs in dev, fewer in prod.CHANGE_ME / from-secret-store— Real passwords never sit in Git. Staging and prod read them from a secret store.${IMAGE_TAG}— Compose fills this from the env file.env=ENV— The health check says which environment answered. Very useful when testing.
Green output
$ docker compose --env-file env/dev.env up -d
$ curl -s localhost:8000/health
{"env":"dev","status":"ok"}
$ docker compose --env-file env/staging.env up -d
$ curl -s localhost:8000/health
{"env":"staging","status":"ok"}
Expected result. One image, two environments, two answers. The code never changed.
Example 2: GitHub Actions environments with a smoke test
Stack. GitHub Actions
Goal. GitHub Actions environments with a smoke test.
.github/workflows/cicd.yml (deploy jobs)
deploy-staging:
needs: build-push
runs-on: ubuntu-latest
environment:
name: staging
url: https://staging.badge.example.com
steps:
- run: echo "deploy ravi/badge-api:${{ needs.build-push.outputs.tag }} to staging"
- run: curl -fsS https://staging.badge.example.com/health
deploy-prod:
needs: deploy-staging
runs-on: ubuntu-latest
environment:
name: production
url: https://badge.example.com
steps:
- run: echo "deploy the same tag to production"
- run: curl -fsS https://badge.example.com/health
What each line does
environment: name: staging— GitHub tracks deploys per environment and can hold secrets per environment.url:— A link shown on the run page.needs.build-push.outputs.tag— The exact tag from the build job, passed forward.curl -fsS— f makes curl fail on HTTP errors, so a bad deploy turns the job red.deploy-prod needs deploy-staging— Prod only after staging is green.
Green output
deploy-staging succeeded {"env":"staging","status":"ok"}
deploy-prod succeeded {"env":"prod","status":"ok"}
Expected result. The repo page now shows Environments: staging and production, each with the last deployed commit.
Example 3: Environments in GitLab CI and Jenkins
Stack. GitLab CI, Jenkins, Bash
Goal. Environments in GitLab CI and Jenkins.
.gitlab-ci.yml
deploy-staging:
stage: deploy
environment:
name: staging
url: https://staging.badge.example.com
script:
- ./deploy.sh staging "$CI_COMMIT_SHORT_SHA"
- curl -fsS https://staging.badge.example.com/health
deploy-prod:
stage: deploy
needs: [deploy-staging]
environment:
name: production
url: https://badge.example.com
rules:
- if: $CI_COMMIT_BRANCH == "main"
script:
- ./deploy.sh prod "$CI_COMMIT_SHORT_SHA"
Jenkinsfile (deploy part)
stage('Deploy staging') {
steps {
sh './deploy.sh staging $GIT_COMMIT'
sh 'curl -fsS https://staging.badge.example.com/health'
}
}
stage('Deploy prod') {
when { branch 'main' }
steps { sh './deploy.sh prod $GIT_COMMIT' }
}
deploy.sh
#!/usr/bin/env bash
set -euo pipefail
ENV_NAME="$1"; SHA="$2"
echo "deploying sha $SHA to $ENV_NAME"
ssh deploy@"$ENV_NAME".internal "docker pull ravi/badge-api:sha-$SHA && docker compose --env-file env/$ENV_NAME.env up -d"
What each line does
rules: if branch main— Prod only from main, never from a feature branch.when { branch 'main' }— The same rule in Jenkins.deploy.sh staging SHA— One script for all environments. Only the name changes.ssh deploy@...— A deploy user with limited rights, not root.
Green output
$ ./deploy.sh staging a3f2c1d
deploying sha a3f2c1d to staging
Status: Downloaded newer image for ravi/badge-api:sha-a3f2c1d
Container badge-api-app-1 Started
Expected result. GitLab shows Deployments > Environments with history and a Rollback button. Jenkins shows the stages in the same order.
Verify: did it work?
-
Check what each environment runs
for e in staging prod; do curl -s https://$e.badge.example.com/version; echo; doneWhat it does: Both should show the same version after a full promotion.
Output
{"commit":"a3f2c1d","version":"1.4.0"} {"commit":"a3f2c1d","version":"1.4.0"} -
Check env history
Open Deployments or Environments in your CI toolWhat it does: You see which commit is in which environment.
Output
staging: a3f2c1d production: a3f2c1d
When it is red 1
curl: (22) The requested URL returned error: 502
Cause. The new container did not start, so the load balancer got no answer.
Fix, step by step
- ssh to the server and run docker compose logs app --tail 50.
- Look for a missing setting like DB_URL.
- Fix the env file or secret and redeploy.
When it is red 2
KeyError: 'DB_URL'
Cause. The app expects a setting that the environment did not give.
Fix, step by step
- Add DB_URL to that env file or secret store.
- Use os.environ.get with a clear error message in the app.
When it is red 3
Works in staging, fails in prod: relation "badges" does not exist
Cause. Staging had a table that prod does not. The database migration ran only in staging.
Fix, step by step
- Add the migration as a pipeline step before deploy in every environment.
- Keep staging and prod the same, which is called parity.
Cleanup
-
Stop local environments
docker compose downWhat it does: Stops the local containers. Images stay.
Output
Container badge-api-app-1 Removed
Practice tasks
Easy Print env in health
Why: See which env answered.
What to do: Add env to /health.
- Run with two env files.
Check: Two different answers.
Medium Add a smoke test
Why: Catch bad deploys at once.
What to do: Add curl -fsS /health after each deploy.
- Stop the app on purpose and run the job.
Check: The job turns red.
Hard Find a parity gap
Why: Prevent "works in staging".
What to do: List every difference between your staging and prod.
- Versions, sizes, settings, data.
Check: You find at least three.
Easy Say dev, staging and production in two lines
Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.
What to do: Write two short lines about dev, staging and production for a fresher who knows only Linux, Git and Docker basics.
- Write the problem it solves.
- Write the one pipeline line or command you use for it.
- Read it aloud to a friend.
Check: A fresher understands it without asking a question.
Easy Keep a throwaway lab repo
Why: Practice needs a repo you can break. If it gets messy, you delete it and start again.
What to do: Make a small practice repo called badge-api with one Python file, one test and one Dockerfile.
- mkdir badge-api and cd into it
- git init -b main
- Add app.py, test_app.py and Dockerfile from this chapter, then commit.
Check: git log shows one commit and pytest prints 1 passed.
Medium Do it in a second CI tool
Why: CI/CD is the idea. The tool is only the syntax. The same stages must work in GitHub Actions, GitLab CI and Jenkins.
What to do: Write the pipeline from this lesson in a second CI tool.
- If you used GitHub Actions, write .gitlab-ci.yml or a Jenkinsfile now.
- Keep the same stage names and the same commands.
- Only the wrapper syntax should change.
Check: Same green result, same stage names, different tool.
Medium Break it on purpose
Why: You must know the red log by face. In a real outage there is no time to read slowly.
What to do: Cause one error from the "When it is red" part of this lesson and fix it.
- Follow the error section.
- Copy the exact red line from the job log into your notes.
- Write the fix in one line.
Check: You fixed it using only the steps, without searching the web.
Medium Write a one-page runbook
Why: At 2 a.m. nobody remembers pipeline menus. A short page saves the on-call person.
What to do: Write a one-page runbook for this lesson.
- Title, when to use, exact steps or pipeline lines, how to check, how to undo.
Check: A teammate can follow it alone.
Hard Explain it like an interview
Why: Interviewers want real stories, not definitions.
What to do: Record a 60 second answer about this lesson.
- Say the problem, the pipeline lines you wrote, and one error you hit.
Check: Your answer has a real pipeline line and a real error message.
Hard Draw the flow top to bottom
Why: A drawing shows the order of work and which stage waits for which.
What to do: Draw this lesson as boxes from top to bottom.
- Start at git push, go through each stage, end at the green run or the deployed app.
Check: Each box matches a real line in your job log.
Homework: try at home
Try at home
At home, make dev and staging env files for badge-api, run the same image with both, and show that /health returns a different env each time. Then add a curl -fsS smoke test to your pipeline.
Recap
- dev, staging and prod are places with different settings.
- The same image moves through all of them.
- Config comes from env files or secret stores.
- Smoke test after every deploy.
- Prod deploys only from main.
FAQ
Do small teams need staging?
Yes, even a small one. It catches config mistakes.
Can staging use real customer data?
Not as-is. Use fake or masked data.
How many environments are too many?
If nobody uses one, delete it. Three or four is common.
Is a preview environment the same as dev?
A preview is a short-lived environment per pull request. It is deleted after merge.
Should staging be as big as prod?
Same software and settings; size can be smaller.
Interview questions
-
What is the difference between staging and production?
Staging is a prod-like copy without real users. Production serves real users.
-
Why not rebuild for production?
A rebuild can pull different libraries. Promote the tested artifact.
-
How do you handle config per environment?
Env variables, env files, or a config store, with secrets in a secret manager.
-
What is environment parity?
Keeping environments as similar as possible so problems show up before prod.
-
What is a smoke test?
A quick check right after deploy, like /health, to see the app is alive.
Quick quiz
Q1. Name three common environments.
dev, staging and production.
Q2. What changes between environments?
Only settings, not the code or image.
Q3. What is promotion?
Moving the same artifact to the next environment.
Q4. Why curl -f in a smoke test?
So an HTTP error makes the job fail.
Q5. Where do prod passwords live?
In a secret store, never in Git.
Did you understand? Let us try it once more.
Revision card
Interview card