Project 04 · Orchestration & CI/CD

ETL with Orchestration and CI/CD

Billing, product usage and CRM data are extracted every night and loaded into Snowflake by an Airflow DAG. dbt turns them into tested models, from staging views to a finance mart with monthly recurring revenue and churn. Every change goes through a pull request: CI lints the SQL, builds only the changed models and their children in a throwaway schema and runs their tests, and merging deploys to production. Terraform manages the warehouses, databases, roles and grants, so the platform is reviewed like the code that runs on it.

  • Apache Airflow
  • dbt
  • Snowflake
  • GitHub Actions
  • Terraform
Sample data Last 14 nightly runs · Sep 23 – Oct 6, 2026 Download data (CSV)

Nightly DAG: task status

elt_nightly, one column per run, final state of each task

dbt lineage

Sources to the reports that depend on them; amber outlines carry test warnings

Run time per night

Whole DAG, with the 14-run average. Sep 29 (red) stopped early.

Monthly recurring revenue

Month-end MRR from fct_mrr

dbt tests, latest run

Oct 6: 413 of 415 passed, 2 warned

Snowflake objects in Terraform

Nightly terraform plan checks for drift

Recent pull requests

CI checks and production deploys

How it works

Code review covers everything: the SQL, the DAG and the infrastructure.

  1. 01 · Extract

    Python tasks on Airflow

    Billing API, product events and CRM exports pulled nightly to S3 as JSON, in parallel, with retries and backoff.

  2. 02 · Load

    Snowflake COPY INTO

    Raw tables keep each file's rows plus load metadata. Loads are idempotent by file name.

  3. 03 · Transform

    dbt

    64 models, from staging views to incremental marts, with 415 tests and freshness checks on every source.

  4. 04 · Orchestrate

    Apache Airflow

    One nightly DAG with retries for flaky APIs, timeouts on long steps and alerts on failure.

  5. 05 · Test & deploy

    GitHub Actions

    Every pull request lints the SQL and builds only changed models and their children in a throwaway schema.

  6. 06 · Provision

    Terraform

    Warehouses, databases, roles and grants are code, reviewed and applied from the same pipeline.

  • Slim CI. Pull requests build state:modified+ against production's manifest and defer everything else to prod, so a typical check takes 6 to 8 minutes instead of a full rebuild.
  • Idempotent loads. COPY INTO skips files it has already loaded and every run is keyed by its logical date, so reruns and backfills never duplicate rows.
  • Tests that block. unique and relationships tests fail the build; softer checks only warn. The Sep 29 failure stopped bad numbers before the finance dashboard refreshed.
  • Everything is code. Terraform owns warehouses, roles and grants. A nightly terraform plan reports drift, like the grant someone changed by hand.

Data model

A raw table as loaded, the MRR mart dbt builds from it, and the Airflow task history behind the status grid (the CSV download).

raw.billing_invoicesSnowflake table · loaded by COPY INTO
payloadvariant
One invoice as returned by the billing API
_file_namevarchar
Source file in the S3 stage
_file_rownumber
Row number within the file
_loaded_attimestamp_ltz
Load time
marts.fct_mrrdbt incremental · merge on month + account
monthdate
First day of the month
account_idvarchar
Customer account
mrr_usd / prev_mrr_usdnumber(12,2)
MRR this month and last month
mrr_change_usdnumber(12,2)
Difference between the two
movementvarchar
new, expansion, contraction, churn or flat
dag_task_runsAirflow task history · the CSV download
run_datedate
Logical date of the nightly run
task_idstring
Task, with the mapped source for extracts
statestring
success, failed or upstream_failed
try_numberint
2 means the task succeeded on retry
started_at / duration_stimestamp / int
When it started and how long it ran

Key code

The nightly DAG, the MRR model, the CI workflow and the Terraform for Snowflake.

The nightly Airflow DAG. Extracts fan out per source, then load, freshness checks and dbt build run in order.

"""Nightly ELT: extract to S3, COPY INTO Snowflake, dbt build, publish."""
from datetime import datetime, timedelta

from airflow.decorators import dag, task
from airflow.operators.bash import BashOperator
from airflow.providers.common.sql.operators.sql import SQLExecuteQueryOperator

from include.extract import extract_to_s3  # writes JSON files to s3://raw-landing/<source>/<ds>/

DBT = {"cwd": "/opt/dbt", "env": {"DBT_TARGET": "prod"}, "append_env": True}


@dag(
    schedule="0 2 * * *",
    start_date=datetime(2026, 1, 1),
    catchup=False,
    max_active_runs=1,
    dagrun_timeout=timedelta(hours=2),
    default_args={"retries": 2, "retry_delay": timedelta(minutes=5), "retry_exponential_backoff": True},
    tags=["elt", "finance"],
)
def elt_nightly():
    @task(pool="api_extracts")
    def extract(source: str, ds=None) -> str:
        return extract_to_s3(source, ds)

    files = extract.expand(source=["billing", "product_events", "crm"])

    load_raw = SQLExecuteQueryOperator(
        task_id="load_raw",
        conn_id="snowflake_loader",
        sql="sql/copy_into_raw.sql",  # COPY INTO raw.<table> FROM @landing/<source>/{{ ds }}/
    )
    freshness = BashOperator(task_id="dbt_source_freshness", bash_command="dbt source freshness", **DBT)
    staging = BashOperator(task_id="dbt_build_staging", bash_command="dbt build --select staging", **DBT)
    marts = BashOperator(
        task_id="dbt_build_marts",
        bash_command="dbt build --select intermediate+ --exclude staging",
        execution_timeout=timedelta(hours=1),
        **DBT,
    )
    publish = BashOperator(task_id="publish_exposures", bash_command="python publish_exposures.py", **DBT)

    @task
    def notify_finance(ds=None):
        from include.alerts import post_message
        post_message("#finance-data", f"Finance mart refreshed for {ds}.")

    files >> load_raw >> freshness >> staging >> marts >> publish >> notify_finance()


elt_nightly()

Engineering notes

The Sep 29 failure did its job

A billing API retry sent one invoice twice. The unique test on fct_invoices failed, so the finance dashboard kept the previous day's numbers instead of showing double revenue. PR #477 deduplicated invoices and the next run was clean.

CI that catches things

Slim CI built 6 models per pull request on average instead of 64, and stopped a broken accepted_values test (PR #475) before it reached production.

Runs got 12% faster

Making the largest marts incremental in PR #474 cut the median nightly run by about six minutes.

Drift is visible

A grant changed by hand in the Snowflake UI showed up in the nightly terraform plan. It was added to the code instead of being silently reverted on the next apply.

About the data. Every number on this page is sample data from a seeded generator written for this project. It is shaped to behave like a real workload, with daily peaks, late events and the odd incident, but it does not come from any employer or client. Download it as dag_task_runs.csv or read data.json.