Skip to content

Feature/vars per environment#12814

Open
andeerlb wants to merge 4 commits intodbt-labs:mainfrom
andeerlb:feature/vars-per-environment
Open

Feature/vars per environment#12814
andeerlb wants to merge 4 commits intodbt-labs:mainfrom
andeerlb:feature/vars-per-environment

Conversation

@andeerlb
Copy link
Copy Markdown

@andeerlb andeerlb commented Apr 11, 2026

https://docs.getdbt.com/docs/build/project-variables

Problem

dbt did not support environment-specific or test-specific variable files. All variables had to be managed in a single vars.yml or dbt_project.yml, making it difficult to isolate configuration for different environments (dev, staging, prod) or for test runs.

Solution

  • Added support for loading and merging vars.yml and vars_<target>.yml at the project root. When running dbt with a specific target (via --target), variables from vars_<target>.yml override those in vars.yml for that environment.
  • Added support for tests/vars.yml and tests/vars_<target>.yml. When running dbt test, these files are loaded and merged (with target-specific vars taking precedence), and their variables override project-level vars for tests only.
  • The loader for test vars is robust: if the test vars files are missing or malformed, they are ignored and dbt continues without error. Project vars remain available for all commands, including tests.
  • Test-specific vars are only loaded and merged during dbt test. Other commands (dbt run, dbt build, etc.) are unaffected and ignore the test vars files.

Why is this useful?

  • Environment-specific configuration:
    Allows teams to manage variables for different environments (dev, staging, prod, etc.) in separate files, reducing risk of accidental changes and making configuration clearer.
  • Test isolation:
    Enables tests to use their own variables without affecting production runs or requiring changes to project-level vars.

How to use

  • Place environment-specific vars in vars.yml and vars_<target>.yml at the project root.
  • Place test-specific vars in tests/vars.yml and tests/vars_<target>.yml.
  • When running dbt test, test vars will override project vars for tests only.
  • When running other commands, only project vars are used.
  • Here is an example how to use https://github.com/andeerlb/dbt_config_per_env_example/tree/master

Checklist

  • I have read the contributing guide and understand what's expected of me.
  • I have run this code in development, and it appears to resolve the stated issue.
  • This PR includes tests, or tests are not required or relevant for this PR.
  • This PR has no interface changes (e.g., macros, CLI, logs, JSON artifacts, config files, adapter interface, etc.) or this PR has already received feedback and approval from Product or DX.
  • This PR includes type annotations for new and modified functions.

@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

@github-actions github-actions bot added the community This PR is from a community member label Apr 11, 2026
@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Apr 11, 2026

Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA.

In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, please reach out through a comment on this PR.

CLA has not been signed by users: @andeerlb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community This PR is from a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant