Skip to content
tezvyn:
Menu

How do you keep one build artifact immutable across environments?

Source: 12factor.netMediumHow cards are made

This tests separation of build and run stages. A strong answer packages one artifact with zero embedded config, then injects env vars or mounted secrets at deploy time via the platform. Red flags include per-stage rebuilds or config baked into the image.

What's really being asked

This question probes whether you understand the strict separation between build and run stages defined by the Twelve-Factor App methodology, specifically factors three, five, and ten. The interviewer wants to see that you know how to keep an artifact immutable while still allowing environment-specific configuration, and that you can articulate why rebuilding per stage destroys parity and auditability.

The full answer

First, the build stage should compile code and resolve dependencies into a single artifact such as a container image or binary, and this artifact must contain no environment-specific configuration. Second, configuration must be supplied at runtime through mechanisms that do not modify the artifact, such as environment variables, mounted secret volumes like Kubernetes secrets or AWS Secrets Manager, or external configuration stores like Consul or etcd that the application reads on startup. Third, the release stage combines the immutable artifact with the current environment configuration to create a runnable instance, and this mapping should be versioned and auditable. Fourth, you should mention that secrets require additional protections such as encryption at rest, least-privilege access, and rotation policies.

The mistakes people make

A major red flag is suggesting that the CI pipeline rebuilds or re-tags the image for each environment with a different config file baked in, because this creates divergent artifacts and makes it impossible to guarantee that what was tested in staging is exactly what runs in production. Another mistake is proposing build-time substitution of variables inside the Dockerfile or build script, which again couples configuration to the artifact. Templating config files into the image during build is also incorrect for the same reason. Some candidates suggest storing secrets in the source repository or build context, which violates security principles.

What usually comes next

The interviewer may ask how you would rotate a database credential without restarting the process, how you handle configuration that changes frequently versus rarely, or how you validate that an artifact has not been tampered with as it moves between registries. They might also ask for a comparison between environment variables and mounted secret files, or how you manage configuration for local development without polluting production settings.

A concrete example

A team builds a Java application into a container image and pushes it to a registry with a semantic version tag. The image contains only the compiled JAR and a base JVM, with no application properties file. In the dev environment, the deployment manifest mounts a ConfigMap and a sealed secret as files under a directory named config, and the application reads these on startup. The identical image is promoted to staging and production, where different ConfigMaps and secrets are mounted, but the image digest remains unchanged across all three environments. If production breaks, the team can instantly roll back by pointing the production manifest to the previous image digest, confident that the artifact itself is identical to the one that passed staging tests.

Interview question

Which approach preserves artifact immutability while allowing environment-specific database credentials?

  • a.Bake a properties file into the image for each environment
  • b.Store secrets in the source repository and include them in the build context
  • c.Mount environment-specific secrets at deploy time while keeping the image unchangedCorrect
  • d.Rebuild the image with environment variables substituted during the build stage
Why?

Mounting secrets at deploy time keeps the image identical across environments, satisfying the Twelve-Factor build-run separation. Rebuilding with substituted variables at build time creates divergent artifacts, so you cannot guarantee that what was tested is exactly what runs in production.

Just read this? Test yourself on what you have been reading.

Read the original → 12factor.net

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ci/cd — each one lists the topics its interview covers.

See open roles