Cross-Environment Workflows

One of the most powerful hibase workflow patterns is running the same SQL pipeline across multiple environments — such as staging and production — without duplicating workflows or queries.

How It Works

The pattern uses three components working together:

  1. A mapping table (meta.environments) that maps environment names to project IDs
  2. Workflow parameters that declare which environments to target
  3. Parameter iteration on steps that need to execute per environment

1. The Mapping Table

Create a table (e.g. meta.environments) that maps each environment name to its concrete project ID. Here is an example of what this table looks like:

-- Example: contents of meta.environments
CREATE OR REPLACE TABLE `mycompany-bi-meta.meta.environments` AS
SELECT * FROM UNNEST([
  STRUCT('staging'    AS environment_name, 'mycompany-bi-staging' AS project_id),
  STRUCT('production' AS environment_name, 'mycompany-bi-prod'    AS project_id)
]);

The resulting table:

environment_name project_id
staging mycompany-bi-staging
production mycompany-bi-prod

This table is your single source of truth for environment configuration. All cross-environment workflows reference it.

2. Workflow Parameters

In the workflow's parameter config, declare which environments this workflow targets:

{
  "environments": ["staging", "production"]
}

You can also have workflows that only target a single environment:

{
  "environments": ["production"]
}

3. Parameter Query with Parameter Iteration

On your SQL step, enable parameter iteration and add a parameter query that resolves the environment parameters:

-- Parameter Query
SELECT
  environment_name,
  project_id
FROM meta.environments
WHERE environment_name IN ({{ $environments }})

This returns one row per environment. When parameter iteration is enabled, the step's main SQL will execute once for each row — with $project_id and $environment_name available as step-level parameters.

4. Environment-Aware SQL

In the main SQL step, reference the project ID using raw_value():

CREATE OR REPLACE TABLE
`{{ raw_value($project_id) }}.datamarts.my_table`
OPTIONS (description = "My datamart table. PK = id")
AS

SELECT *
FROM `{{ raw_value($project_id) }}.core.source_table`
WHERE ranking = 1

The {{ raw_value($project_id) }} interpolation injects the project ID without quoting, allowing it to be used in BigQuery table references.

Cross-Environment Assertions

When running data quality assertions across multiple environments, you can use the STRING_AGG + FORMAT pattern to dynamically build and run a single query that checks all environments at once.

Cross-Environment Uniqueness Assertion

Parameter Query (generates dynamic SQL):

SELECT
  STRING_AGG(
    FORMAT(
      """
        SELECT
          '%s' AS environment_name,
          COUNT(*) = COUNT(DISTINCT(primary_key)) AS count_match
        FROM `%s.datamarts.my_table`
      """,
      environment_name,
      project_id
    ),
    '\n UNION ALL \n'
  ) AS assertion_sql
FROM meta.environments
WHERE environment_name IN ({{ $environments }})

Step SQL (runs the generated query):

SELECT COUNT(*) AS result
FROM (
  {{ raw_value($assertion_sql) }}
)
WHERE count_match IS FALSE

Evaluate expression:

{{ $response.result == 0 }}

This pattern generates a UNION ALL query that checks uniqueness in every environment, then counts how many failed. If the result is 0, all environments pass.

Cross-Environment Non-Null Assertion

The same STRING_AGG + FORMAT pattern works for non-null assertions:

Parameter Query:

SELECT
  STRING_AGG(
    FORMAT(
      """
      SELECT
        '%s' AS environment_name,
        COUNT(*) = 0 AS count_match
      FROM `%s.core.my_table`
      WHERE FALSE
        OR db_row_timestamp IS NULL
        OR tenant IS NULL
        OR user_id IS NULL
      """,
      environment_name,
      project_id
    ),
    '\n UNION ALL \n'
  ) AS assertion_sql
FROM meta.environments
WHERE environment_name IN ({{ $environments }})

The WHERE FALSE OR ... pattern is a clean way to list all the columns that must not be null.

When to Use This Pattern

  • Data modeling — same transformation logic across staging and production BigQuery projects
  • Cross-environment assertions — validate data quality in all environments with one workflow
  • Gradual rollouts — test in staging first, then add production to the parameters

Tips

  • Keep the meta.environments table as your single source of truth for environment configuration
  • Use a workflow path like cross-environment WFs to tag workflows that use this pattern
  • Some workflows intentionally exclude environments (e.g. "environments": ["production"]) when a feature isn't available in staging
  • For assertions, always set both On False expression (assertion failed) and On Error (query failed) notifications