Skip to content

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.

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.

Terminal window
diverge env export --service <name> [--format dotenv|json|shell] [--output <file>]
  • --service: The name of the service in your .diverge.yaml to export variables for.
  • --format: Output format. Defaults to dotenv. Supported: dotenv, json, shell.
  • --output: File to write to (e.g., .env). If omitted, prints to stdout.

Export to a local .env file for use with tools that automatically load it (like Node.js, Python, etc.):

Terminal window
diverge env export --service api --format dotenv --output .env

Output:

Terminal window
DATABASE_URL=postgres://user:pass@localhost:5432/mr-123-db
REDIS_HOST=localhost:6379

Export as JSON, useful for passing into scripts or CI tools:

Terminal window
diverge env export --service api --format json

Output:

{
"DATABASE_URL": "postgres://user:pass@localhost:5432/mr-123-db",
"REDIS_HOST": "localhost:6379"
}

Export as shell exports, which you can source directly into your terminal session:

Terminal window
source <(diverge env export --service api --format shell)

Output:

Terminal window
export DATABASE_URL='postgres://user:pass@localhost:5432/mr-123-db'
export REDIS_HOST='localhost:6379'

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"
}
}
]
}