CI/CD · Ship safely, repeatedly

When should you use environment variables versus config files?

Easyconfigurationenvironment variablestwelve-factorUpdated Oct 2026
Quiz this track

Short answer

Use environment variables for deployment-specific values and secrets injected at runtime. Use committed config files for non-sensitive defaults and structure, and validate all configuration at startup.

The twelve-factor principle is to separate config from code. Environment variables are convenient because the same artifact can run in different environments, but they are a flat string-only interface with no schema.

  • Environment variables suit ports, service URLs, feature defaults and injected credentials
  • Config files suit structured non-secret settings that belong in version control
  • Never commit a populated .env file; commit an example with placeholder keys
  • Validate required variables at startup and fail fast with a clear message
  • Document precedence when defaults, files and environment overrides interact
js
function required(name) {
  const value = process.env[name];
  if (!value) {
    throw new Error('Missing required environment variable: ' + name);
  }
  return value;
}

const databaseUrl = required('DATABASE_URL');
const port = Number(process.env.PORT || '8080');
Environment variables are visible to child processes and sometimes to crash dumps or debug endpoints. They are acceptable for secrets only when the platform protects them and the workload is trusted.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the twelve-factor approach to configuration?
  2. 2How do you validate configuration at startup?
  3. 3What are the downsides of environment variables?
  4. 4How do you handle secrets in local development?

Say it in the interview

I use environment variables for values that change between deployments and for credentials injected at runtime. Structured non-secret defaults can live in a committed config file. I validate everything at startup so a missing variable fails fast instead of causing a strange runtime error. I do not commit populated env files, and I document the precedence between defaults, files and environment overrides.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live CI/CD mock interview and get scored feedback on depth, clarity and confidence.