Video in progress
What is the "build once, deploy many" principle, and what problems does breaking it cause?
The artifact (image) is built once, and exactly that digest moves through dev, stage and prod; only the configuration changes. If you rebuild for each environment, what passed the tests and what went to prod can be different things.
📖 Answer
A common anti-pattern: release/stage and release/prod branches, each built in its own pipeline. Or a Maven profile, -Pprod, producing a separate jar for prod.
Why it is dangerous:
- if a dependency is
1.4.+orSNAPSHOT, a new version may appear between the two builds; - the base image tag
temurin:21-jrechanges to a new patch within a week; - the build environment (Maven cache, JDK patch version) differs;
- a build per profile changes the code itself: the code tested in stage is not the one in prod.
The result: "it worked in stage, not in prod", and you cannot recreate it.
The right flow:
commit → build → image@sha256:abc → test
→ dev (digest abc) → stage (digest abc) → prod (digest abc)
Promotion is not a new build, it is updating the digest in the environment manifest. In GitOps that is an ordinary PR.
What this takes:
- configuration is not inside the image: Spring profiles and properties come from outside (env, ConfigMap). You can put
application-prod.ymlin the image, but the active profile is supplied from outside; - a reproducible build: strict dependency versions (a lock file or exact versions), the base image by digest;
- artifact metadata: the git SHA, build time and pipeline ID in the image labels, so you can immediately find which commit is running in prod.
One exception: signing or attestation can be added at promotion time. That does not change the artifact, it adds metadata next to it.
Sign up to mark studied questions and save your progress.