Performing a blue/green deploy of your dbt project on Snowflake

Sorry about the thread necro but wanted to add to this body of knowledge for folks that are wanting to do blue/green deploys but with schema swaps instead in a single dbt Cloud job (should work with dbt-core too) that has the following steps:

  1. dbt run on the blue_ schema.
  2. dbt test on the blue_ schema.
  3. dbt run-operation swap_schema to swap blue_ schema with the green_ schema.
  4. dbt docs generate on the green_ schema.

Note: the swap_schema macro is left as an exercise to the reader.

With a custom generate_schema_name macro like so:

{% macro generate_schema_name(custom_schema_name, node) -%}

    {%- set default_schema = target.schema -%}

    {%- if var('is_pre_swap') is not none -%}
        blue_{{ default_schema }}
    {%- else -%}
        green_{{ default_schema }}
    {% endif -%}
 
{%- endmacro %}

And a sequence of run steps like so:

$ dbt run --vars 'is_pre_swap: 1'
$ dbt test --vars 'is_pre_swap: 1'
$ dbt run-operation swap_schema
$ dbt docs generate

This allows a single dbt Cloud job to do what we want since at the final step (docs generate), the is_pre_swap variable will be evaluated to none so dbt will look to generate docs on the green_ schemas correctly.

2 Likes