Skip to main content

04. Advanced Environment Strategies

️ Advanced Environment Strategies in dbt​

Managing multiple environments effectively is crucial for robust CI/CD, testing, and production deployments.
Advanced environment strategies help ensure isolation, consistency, and reliable deployments across dev, staging, and production.


Key Environment Strategies​

Environment separation – dev, staging, prod schemas or databases
Dynamic profiles – configure targets dynamically based on environment
Feature branches – test new models without impacting production
Environment-specific variables – control behavior per environment


️ 1. Using Profiles for Multiple Environments​

Define multiple targets in profiles.yml:

my_project:
target: dev
outputs:
dev:
type: snowflake
account: <account>
user: <dev_user>
password: <dev_password>
role: <dev_role>
database: my_db
warehouse: my_wh
schema: dev_schema
prod:
type: snowflake
account: <account>
user: <prod_user>
password: <prod_password>
role: <prod_role>
database: my_db
warehouse: my_wh
schema: prod_schema
  • Switch environments via --target in dbt CLI:
dbt run --target prod

️ 2. Environment-Specific Variables​

  • Define variables in dbt_project.yml:
vars:
dev:
data_refresh_days: 7
prod:
data_refresh_days: 1
  • Access in SQL using Jinja:
select *
from {{ source('app', 'orders') }}
where order_date >= dateadd(day, -{{ var('data_refresh_days') }}, current_date)

️ 3. Feature Branches and CI/CD​

  • Use Git feature branches for development and testing
  • Merge into main or release branches after CI/CD validation
  • Ensures production stability while allowing iterative development

️ 4. Best Practices​

Keep environments isolated
Use environment-specific variables for flexibility
Automate deployment via CI/CD pipelines
Monitor environment performance and resource usage
Test incremental models and snapshots in staging before prod