الانتقال إلى المحتوى
DevOps والـ CloudCI/CD· 7 دقائق للقراءة

Build Once, Deploy Everywhere: Why Config Doesn't Belong in Your Build

A build per environment ships an artifact nobody tested. Build one image and give it its settings when it starts.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

QA signs off on staging on Tuesday. On Wednesday, the release job checks out the same commit, builds it again with the production settings, and deploys. Checkout breaks in production, and staging can't reproduce it. The production image was built a day later than the one QA tested, and something under it had changed in between. Two builds of one commit, and production got the one nobody tested.

The common misconception

The belief: each environment (dev, staging, production) needs its own build, with its settings baked in. Staging gets a staging build, production gets a production build, and each one is complete and ready to run.

Smart developers believe this because their tools teach it. Angular's docs describe file replacements, "an option for substituting source files before executing a build", and say that combined with --configuration this "provides a mechanism for configuring environment-specific data in your application." To add staging, the same page has you copy environment.ts to environment.staging.ts and add a staging build configuration. If the official way to set an API URL is a build flag, a build per environment looks like the design.

The half-truth: the app does need to know where it runs. It can learn that when it starts, though, and that's the only way to deploy exactly what you tested.

What actually happens

Build, release and run are separate stages

The Twelve-Factor App splits a deploy into three stages. The build stage turns the code into "an executable bundle known as a build." The release stage "takes the build produced by the build stage and combines it with the deploy's current config." The run stage starts it. Config joins at release, after the build is finished, so one can become many releases: one for staging, one for production. And "Every release should always have a unique release ID", so you can always tell which build and which config are running.

A second build is a second artifact

Two builds of the same commit aren't guaranteed to be identical. The earlier articles showed why: a base image tag can move between builds, and a cached RUN step can hold different packages on different machines. Even with every input pinned, a per-environment build differs on purpose, because the settings are part of it. In our run, the staging and production images got different image IDs, so the image you tested is not the image you ship. A gets its confidence from promoting the same artifact through each stage.

Config is what varies between deploys

The Twelve-Factor definition is precise: config is "everything that is likely to vary between deploys (staging, production, developer environments, etc)", such as database handles, credentials and hostnames. Routes or how modules are wired together don't count, since that kind of setting "does not vary between deploys, and so is best done in the code." Its litmus test is worth running on your repo: could you publish the code "at any moment, without compromising any credentials"?

Values baked into an image travel with it

Docker's docs warn that "Build arguments and environment variables are inappropriate for passing secrets to your build, because they're exposed in the final image." That's true of every build-time value: anyone who can pull the image can read it. In our test, docker history printed the production URL twice, once from ENV and once from the ARG itself.

The fix: one image, config at startup

Both tabs build the same server.ts (below) on Docker 29.4.3 with node:24.22.2-alpine3.24. The wrong one bakes the URL in with a build argument:

Bash terminal
# Dockerfile: ARG PAYMENTS_URL, then ENV PAYMENTS_URL=$PAYMENTS_URLdocker build --build-arg PAYMENTS_URL=https://sandbox.payments.example \  -t shop-api:1.4.0-staging .docker build --build-arg PAYMENTS_URL=https://api.payments.example \  -t shop-api:1.4.0-prod .docker image ls --format '{{.Tag}} {{.ID}}' shop-api# 1.4.0-prod e6cf6bf4ae98# 1.4.0-staging 5911098ae226docker history --format '{{.CreatedBy}}' shop-api:1.4.0-prod | grep PAYMENTS# ENV PAYMENTS_URL=https://api.payments.example# ARG PAYMENTS_URL=https://api.payments.example

The app reads its config once, at startup, and refuses to start without it:

TypeScript server.ts
import { createServer } from "node:http";// Per-deploy config comes from the environment at startupconst paymentsUrl = process.env.PAYMENTS_URL;if (!paymentsUrl) {  throw new Error("PAYMENTS_URL is not set");}createServer((_req, res) => {  res.end("ok");}).listen(8080, () => {  console.log(`shop-api 1.4.0, payments at ${paymentsUrl}`);});

In the first tab, one commit became two images with two IDs, and the production URL is readable by anyone with the image. In the second, both containers run the image with the same ID, and only the variable differs. Starting it with no PAYMENTS_URL exits right away with Error: PAYMENTS_URL is not set, which is far better than failing on the first payment.

Build per environmentBuild once, configure at deploy
What reaches productionAn artifact built after testingThe artifact that passed staging
Changing a URLRebuild and test againChange a variable and restart
Rolling backRebuild an old commitRedeploy the previous digest
SecretsReadable in the image historyGiven to the container at run time

How to apply it in your project

  1. List what varies between deploysGo through your build scripts, Dockerfiles and pipeline for per-environment values: URLs, hostnames, credentials, feature switches. Each one is config.
  2. Move it out of the buildReplace build arguments, compile-time defines and file replacements with values read at startup, and fail fast when one is missing.
  3. Promote one artifactBuild once per commit, tag it with the version or commit SHA, record its digest, and deploy that digest to staging and then production. The latest-tag article showed why the digest matters.
  4. Give secrets to the container, not the imagePass credentials at run time from your platform's secret store, never as build arguments, and check docker history on your current images for anything that leaked.

Key takeaways

  • Build once; config joins at release, so one artifact can run in every environment.
  • A per-environment build means production runs an image that was never tested.
  • Config is whatever varies between deploys; read it at startup and fail fast when it's missing.
  • Build arguments and ENV values stay in the image, so never use them for secrets.
  • Promote the same digest from staging to production, and roll back by redeploying the previous one.

📚 المصادر

  1. III. Config — The Twelve-Factor App
  2. V. Build, release, run — The Twelve-Factor App
  3. Build variables — Docker Docs
  4. Configuration in ASP.NET Core — Microsoft Learn
  5. Build environments — Angular

التعليقات (0)