Managing dbt Projects on Snowflake using Snowflake CLI¶
Note
The dbt Projects on Snowflake features in Snowflake CLI are available only in version 3.13.0 or later.
You can use Snowflake CLI to manage dbt project objects with the following operations:
- Deploying a dbt project object
- Listing all available dbt project objects
- Executing a dbt project object command
- Describing a dbt project object
- Dropping a dbt project object
Deploying a dbt project object¶
The snow dbt deploy command uploads local files to a temporary stage and creates a new dbt project object or updates it by
making a new version. A valid dbt project must contain dbt_project.yml and one of the supported profile files:
-
dbt_project.yml: A standard dbt configuration file that specifies the profile to use. -
dbt_projects_profiles.ymlorprofiles.yml: A dbt connection profile definition referenced indbt_project.yml. The selected profile file must define the database, role, schema, and type. If both files are present, Snowflake usesdbt_projects_profiles.ymland ignoresprofiles.ymlduring deployment, compilation, and subsequent commands.- By default, dbt Projects on Snowflake uses your target schema (
target.schema) specified from your dbt environment or profile. When you execute a dbt project object, dbt attempts to create the target schema specified indbt_projects_profiles.ymlorprofiles.ymlif it doesn’t already exist. For more information, see Understand schema generation and customization.
- By default, dbt Projects on Snowflake uses your target schema (
The following examples illustrate how to use the snow dbt deploy command:
Warning
Don’t use --force unless you intentionally want to recreate the dbt project object. In snow dbt deploy, --force runs CREATE OR REPLACE DBT PROJECT, which removes all existing versions and run history.
-
Deploy a dbt project object named
jaffle_shop: -
Deploy a project named
jaffle_shopfrom a specified directory, using a profile file from a separate directory. The CLI looks fordbt_projects_profiles.ymlfirst and usesprofiles.ymlonly ifdbt_projects_profiles.ymlisn’t present. The CLI copies the file into the root of the deployed project object with the same filename, overwriting a file with the same name in this location: -
Deploy a project named
jaffle_shopfrom a specified directory, supplying a profile file from outside the project, setting a default target, pinning a dbt version, and enabling external access integrations: -
Deploy a project named
jaffle_shopand set a specific version for the dbt project object: -
Deploy a project named
jaffle_shop, pull in anenv.ymlfile from a separate directory, and set the default environment for compilation and later executions:The
--env-file-dirflag points the CLI at anenv.ymlfile elsewhere in your repo (similar to--profiles-dir) and pulls it into the deployed object, overwriting the object’s rootenv.ymlif one already exists. The--default-envflag sets the environment used for compilation and subsequent executions. These flags require Snowflake CLI 3.21 or later. For more information, see Using SQL environment variables and private Git packages for dbt Projects on Snowflake.
Listing all available dbt project objects¶
The snow dbt list command lists all available dbt project objects on Snowflake.
The following examples illustrate how to use the snow dbt list command:
-
List all available dbt project objects:
-
List dbt project objects in the
productdatabase whose names begin withJAFFLE:
Executing a dbt project object command¶
The snow dbt execute command executes one of the following dbt commands on a Snowflake dbt project object:
For more information about using dbt commands, see the dbt Command reference.
The following examples illustrate how to use the snow dbt execute command:
-
Execute the dbt
testcommand: -
Execute the
rundbt command asynchronously: -
Execute the
rundbt command with a specific dbt version: -
Execute the
rundbt command against a selected environment, overriding individual variables for this run:The
--envflag selects the environment defined in the project’senv.ymlfile, and--env-varsapplies inline key/value overrides for this execution. Use--use-shell-env-varsto pullDBT_-prefixed shell variables (excludingDBT_ENV_SECRET_*variables) into the run. These flags require Snowflake CLI 3.21 or later. For more information, see Use the Snowflake CLI.
Describing a dbt project object¶
The snow dbt describe command describes a dbt project object on Snowflake.
The following example describes the dbt project object named my_dbt_project on Snowflake:
Dropping a dbt project object¶
The snow dbt drop command deletes a dbt project object on Snowflake.
The following example deletes the dbt project object named my_dbt_project on Snowflake:
Use snow dbt commands in a CI/CD workflow¶
Note
When building CI/CD workflows, you only need your Git server, such as Github, and Snowflake CLI. A Git repository object is not required.
You can run dbt commands with Snowflake CLI to build CI/CD pipelines. These pipelines are commonly used to test new code, such as new pull requests, or to update production applications whenever something is merged to the main branch.
To build a CI/CD workflow with snow dbt commands, follow these steps:
-
Prepare your dbt project:
- Download your dbt project or start a new one.
-
Ensure that the main project directory contains
dbt_project.ymland eitherdbt_projects_profiles.ymlorprofiles.yml. -
Verify that the profile name referenced in
dbt_project.ymlis defined indbt_projects_profiles.ymlorprofiles.yml. If both files are present, Snowflake usesdbt_projects_profiles.yml.Note
Ensure that credentials are excluded from the profile file. Leave
accountanduseras placeholder strings and let your CI/CD platform supply the connection through secrets or environment variables.
-
- Download your dbt project or start a new one.
-
Set up Snowflake CLI GitHub Action.
Follow the guidelines for setting up GitHub Action for Snowflake CLI and verify your connection to Snowflake.
-
Define your workflow.
Determine which commands your workflow needs to run based on your organization’s needs. The following example illustrates a CI workflow that updates the version of the dbt project object named
product_pipelinewith new files, runs the transformations, and finally runs tests: