Environment files per environment, without leaks
Published 2026-11-06
The twelve-factor idea, applied honestly
Configuration that varies between environments should live in the environment, not in code. In practice that usually means an env file read at startup. The idea is simple. The execution is where teams slip: secrets committed by accident, a production value copied into development, or a file that exists on one server and nowhere else.
Two kinds of values
Separate non-secret settings, such as hostnames, feature switches and log levels, from secrets, such as database passwords and API keys. Non-secret values can be tracked in git, one file per environment, which makes differences reviewable and merges safe. Secrets should live outside the repository, in a secrets manager or in files with restricted permissions on the server, and be injected at deploy time.
If a secret ever lands in git history, treat it as compromised and rotate it. Deleting the file in a later commit does not remove it from history.
Make the choice explicit
Have the build tooling copy the right file into place based on something deliberate, such as the directory name or a flag, and print which environment it selected. A deploy that says it is building production while standing in a development checkout is caught in seconds.
Fail on placeholders
Templates often contain obvious placeholders for values someone must fill in. Add a check that searches for them before a deploy and refuses to continue. It is a ten-line script that prevents shipping a server pointed at a database called example.
Do not log or echo configuration
Debug pages, error trackers and verbose logs have leaked more credentials than attackers have. Turn debug mode off in production, scrub known secret keys from logs and be careful with commands that print the whole environment when you paste output into a ticket or a chat.
A worked example
Imagine a small application that runs in a development and a production environment. The repository holds two tracked files, one per environment, with every non-secret value filled in and a clearly marked placeholder for each secret. A single build command copies the right one into place, chosen by the name of the directory it runs in, so nobody types an environment name and nobody forgets to.
Secrets themselves arrive another way: injected by the deployment tooling, read from a secret store, or placed on the server once by a person. The repository then documents which secrets exist and what they are for, but never their values. A new colleague can set up an environment from the documentation alone, and a leaked repository leaks nothing that matters.
Checks worth automating
- A pre-deploy search for placeholder strings, so an unfilled template stops the release.
- A comparison of the keys in both environment files, so a setting added for one is not silently missing from the other.
- A guard that refuses to start with debug mode on in production.
- A scan of the commit history for accidentally committed credentials, with a rotation plan when one is found.
When a secret does leak
Assume it will happen once. Rotate the credential first, then investigate; deleting the file from history does not help if the value was already copied. Keep rotation cheap by giving each environment and each service its own credentials, so a single leak has a small blast radius and revoking it does not take the whole platform down.