Sub topic 3
Keeping configuration outside the artifact
This chapter assumes you can read YAML and Dockerfiles, know what a container image is, and have used environment variables. It covers one rule of build-once-promote pipelines: the artifact must not contain anything that differs between environments.
Why configuration cannot live in the artifact
You build one image and promote it from staging to production. Staging and production still differ. They use different database hosts, different API endpoints, different log levels and different feature settings.
If those values are baked into the image, you have two options, and both are bad:
- Build a separate image per environment. The production image is then not the one you tested, which defeats the purpose of promoting.
- Edit the image after the build. This breaks immutability, and you lose the ability to say what is running.
The fix is to build the image with no environment-specific values. The environment supplies them when the container starts.
What counts as configuration
Configuration is any value that can legitimately change between deployments of the same code. Typical examples:
| Value | Staging | Production |
|---|---|---|
| `DATABASE_HOST` | `db.staging.internal` | `db.prod.internal` |
| `LOG_LEVEL` | `debug` | `info` |
| `REPLICAS` | 1 | 6 |
| `PAYMENTS_API_URL` | sandbox URL | live URL |
Code, dependencies and default behavior belong in the artifact. Anything in the table above belongs outside it. Credentials are also configuration, but they need extra care. The next chapter covers them.
Where configuration comes from
The application reads its settings at startup from its surroundings. Common sources are environment variables, mounted config files, and a configuration service. In Kubernetes, a ConfigMap holds non-secret settings for one namespace.
The deploy script from the pipeline already relies on this. It changes only the image and leaves everything else to the environment:
#!/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=120sEach namespace has its own ConfigMap with the same keys and different values:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: staging
data:
LOG_LEVEL: "debug"
DATABASE_HOST: "db.staging.internal"
PAYMENTS_API_URL: "https://sandbox.payments.example.com"The production namespace has a ConfigMap with the same name and keys, but production values. The Deployment references app-config by name, so the same image picks up whichever ConfigMap exists in the namespace where it runs.
Reading configuration in the application
The application should read values at startup and fail fast if one is missing. A missing value then stops the rollout instead of causing a subtle bug later.
import os
def require(name: str) -> str:
value = os.environ.get(name)
if not value:
raise RuntimeError(f"Missing required setting: {name}")
return value
DATABASE_HOST = require("DATABASE_HOST")
LOG_LEVEL = os.environ.get("LOG_LEVEL", "info")A crash at startup makes kubectl rollout status fail, so the deploy job fails and production is never reached with a broken setup. Safe defaults are fine for harmless values such as the log level. Do not give a default to anything that points at a real system, such as a database host. A default there could silently connect staging to production data.
Common mistakes
- Build arguments for environment values.
docker build --build-arg API_URL=...writes the value into the image layers. Use build arguments only for things that are the same everywhere. - Frontend bundles with embedded URLs. Static sites often compile API addresses into JavaScript. Serve a small config file at runtime instead, or have the page request its settings on load.
- Config drift. Staging gets a new setting and production does not. Keep the keys identical across environments and compare them in review, or have the app fail at startup when a key is missing, as shown above.
- Configuration that is not versioned. Store non-secret config in Git next to the deploy definition. Then a configuration change has a history and can be rolled back like code.
Testing the rule
A quick check: could you take the image running in production, start it with the staging configuration, and have it work? If yes, the artifact is clean. If it fails because a value is baked in, that value should move out.
Exercise
Your pipeline builds the image with --build-arg DATABASE_HOST=db.staging.internal, then tags it and promotes it to production, where it connects to the staging database. Which rule is broken, and how do you fix it?
Show answer
The artifact contains an environment-specific value. Remove the build argument and have the application read DATABASE_HOST from the environment at startup. Give each environment its own ConfigMap (or equivalent) that supplies the right host, and make the app fail at startup if the value is missing.