Environment Export
The diverge env export command allows developers to easily retrieve environment variables and connection strings for their active preview environments. This bridges the gap between cluster-based previews and local development workflows.
What it does
Section titled “What it does”When a preview environment provisions resources like isolated databases or caches, the connection details are unique to that MR. env export securely retrieves these secrets and configuration values, formatting them for your local environment so you can run your application locally against the preview databases.
diverge env export --service <name> [--format dotenv|json|shell] [--output <file>]Options
Section titled “Options”--service: The name of the service in your.diverge.yamlto export variables for.--format: Output format. Defaults todotenv. Supported:dotenv,json,shell.--output: File to write to (e.g.,.env). If omitted, prints to stdout.
Examples
Section titled “Examples”dotenv format (Default)
Section titled “dotenv format (Default)”Export to a local .env file for use with tools that automatically load it (like Node.js, Python, etc.):
diverge env export --service api --format dotenv --output .envOutput:
DATABASE_URL=postgres://user:pass@localhost:5432/mr-123-dbREDIS_HOST=localhost:6379JSON format
Section titled “JSON format”Export as JSON, useful for passing into scripts or CI tools:
diverge env export --service api --format jsonOutput:
{ "DATABASE_URL": "postgres://user:pass@localhost:5432/mr-123-db", "REDIS_HOST": "localhost:6379"}Shell format
Section titled “Shell format”Export as shell exports, which you can source directly into your terminal session:
source <(diverge env export --service api --format shell)Output:
export DATABASE_URL='postgres://user:pass@localhost:5432/mr-123-db'export REDIS_HOST='localhost:6379'IDE Integration Tips
Section titled “IDE Integration Tips”You can configure your IDE or editor to automatically run diverge env export as a pre-launch task.
For example, in VS Code tasks.json:
{ "version": "2.0.0", "tasks": [ { "label": "Fetch Preview Env", "type": "shell", "command": "diverge env export --service api --format dotenv --output .env", "presentation": { "reveal": "silent" } } ]}