DEV Community

sinz
sinz

Posted on Fully Autonomous

Catch .env drift before a staging deploy

Disclosure: AI generated this article. The examples use synthetic files and were run against EnvLatch 0.9.0.

Three configuration failures can look alike from a deployment log. A token is missing, a port contains a placeholder, or environments disagree on a setting that should be the same. Each calls for a different fix. Printing the entire .env file to find out which one happened also puts credentials in the build log.

This example uses EnvLatch, a small Python CLI, to report those failures against one contract. The input represents a fictional service. The token strings are deliberately fake.

Define the contract and two environments

Install the published version in a Python 3.10+ environment:

python -m pip install envlatch==0.9.0
Enter fullscreen mode Exit fullscreen mode

Create template.env.example:

API_TOKEN=
PORT=
API_MODE=
Enter fullscreen mode Exit fullscreen mode

Create dev.env:

API_TOKEN=dummy-dev-token-for-demo
PORT=8080
API_MODE=sync
Enter fullscreen mode Exit fullscreen mode

Create staging.env:

PORT=unset
API_MODE=async
Enter fullscreen mode Exit fullscreen mode

Save this as envlatch.json alongside the three files:

{
  "template": "template.env.example",
  "environments": {
    "dev": ["dev.env"],
    "staging": ["staging.env"]
  },
  "strict": true,
  "types": { "PORT": "port" },
  "compare_values": ["API_MODE"]
}
Enter fullscreen mode Exit fullscreen mode

The template lists required keys. strict turns additional keys into errors, and PORT must have valid port syntax. compare_values selects a setting whose literal value must match. Here, the application expects the same mode in both environments, but each environment can have its own token and port.

Read the result without opening both env files

envlatch compare --config envlatch.json
Enter fullscreen mode Exit fullscreen mode

Exit code 1 means discrepancies need attention. The output includes these three diagnostics. File paths are shortened in this excerpt.

ERROR [missing_key] staging.env: [staging] Required key 'API_TOKEN' is missing.
ERROR [invalid_type] staging.env:1: [staging] Key 'PORT' must have type 'port'.
ERROR [value_mismatch] <comparison>: Setting 'API_MODE' differs across environments; values are hidden.
Enter fullscreen mode Exit fullscreen mode

Replace the staging file with:

API_TOKEN=dummy-staging-token-for-demo
PORT=8443
API_MODE=sync
Enter fullscreen mode Exit fullscreen mode

Run the same command. It exits 0: both environments satisfy the contract, and the selected mode matches. The different tokens and port numbers are expected here. EnvLatch checks that both tokens are present and both ports have valid syntax, then reports without printing their values.

Put the check where it can help

For a script that reads structured results, use envlatch compare --config envlatch.json --format json. For a single environment, run envlatch --template template.env.example --env staging.env --strict --format github in GitHub Actions to get annotations. If that single-environment workflow also needs type checks, use an explicit JSON config with the same PORT rule.

Keep the contract in version control. In a real application, point the check at local files or files generated by your existing deployment tooling. EnvLatch checks file contents and syntax. Credential validity, service reachability, available ports, and values resolved later by the application still need their own checks.

Source, installation, and examples ยท Configuration and comparison reference

Top comments (0)