DevPath · Learn to code ESPTEN

Deployment, environments and secrets

Environments, configuration and secrets

Three environments, one same code

The same code runs in several environments:

What changes between environments is not the code: it's the configuration (database URL, API keys, log level...).

Environment variables and 12-factor

The 12-factor methodology proposes, among other things, separating the configuration from the code and reading it from the environment. In Node that's process.env:

const config = {
  port: process.env.PORT || 3000,
  database: process.env.DATABASE_URL,
  environment: process.env.NODE_ENV || "development",
};

This way the same artifact (the same build) runs in dev, staging and prod just by changing the variables. Never put production values hardcoded in the code.

Secret management

Keys and passwords are secrets. Golden rules:

# .env stays IGNORED by Git; only the example without real values is versioned
echo ".env" >> .gitignore
git add .env.example   # documents WHICH variables are needed, not their values

Feature flags

A feature flag is a switch that enables or disables a feature without deploying again. It serves to launch to a percentage of users, do A/B tests or turn off something that's failing instantly.

if (featureActive(flags, "new-checkout", user)) {
  showNewCheckout();
} else {
  showClassicCheckout();
}
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← Where to deployInfrastructure as code →