-
Notifications
You must be signed in to change notification settings - Fork 1.4k
169 lines (141 loc) · 7.09 KB
/
Copy pathdbt_run.yml
File metadata and controls
169 lines (141 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: DBT Run Reusable Workflow
# Called per project by:
# - dbt_pr_run.yml (fork-safe PR CI): runs in the privileged base-repo context. Only the dbt code
# is retrieved from the untrusted PR
# - dbt_full_run.yml (manual): trigger_run_id/pr_number/head_sha are omitted, so the artifact
# overlay and PR status reporting are skipped and the run uses the checked-out branch as-is.
on:
workflow_call:
inputs:
project:
description: 'DBT run for project'
required: true
type: string
trigger_run_id:
description: 'Run id of the "Receive dbt PR" run that holds the PR code artifacts (empty: run in-repo code as-is)'
required: false
type: string
default: ''
pr_number:
description: 'PR number, used in the CI schema name'
required: false
type: string
default: ''
head_sha:
description: 'PR head commit to report commit statuses to (empty: no status reporting)'
required: false
type: string
default: ''
jobs:
dbt-test:
runs-on: ubuntu-latest
permissions:
contents: read
actions: read
statuses: write
timeout-minutes: 90
steps:
- name: Report pending status to PR
if: inputs.head_sha != ''
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api "repos/${{ github.repository }}/statuses/${{ inputs.head_sha }}" \
-f "state=pending" \
-f "context=dbt-ci/${{ inputs.project }}" \
-f "description=dbt CI running" \
-f "target_url=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
- name: Check out repository code (trusted)
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
- name: Drop main-branch dbt code
if: inputs.trigger_run_id != ''
run: rm -rf dbt_subprojects sources dbt_macros/shared
- name: Download PR dbt subprojects (untrusted)
if: inputs.trigger_run_id != ''
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7
with:
name: dbt_subprojects
path: dbt_subprojects
run-id: ${{ inputs.trigger_run_id }}
github-token: ${{ github.token }}
- name: Download PR sources (untrusted)
if: inputs.trigger_run_id != ''
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7
with:
name: sources
path: sources
run-id: ${{ inputs.trigger_run_id }}
github-token: ${{ github.token }}
- name: Download PR shared macros (untrusted)
if: inputs.trigger_run_id != ''
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7
with:
name: dbt_macros_shared
path: dbt_macros/shared
run-id: ${{ inputs.trigger_run_id }}
github-token: ${{ github.token }}
- name: Set up Python 3.13
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
with:
python-version: "3.13"
- name: Set up uv
uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
with:
enable-cache: true
- name: Sync Python dependencies
run: uv sync --locked
- name: Configure dbt
env:
DBT_PROFILES_YML_BASE64: ${{ secrets.DBT_PROFILES_YML_BASE64 }}
run: |
mkdir $HOME/.dbt
echo $DBT_PROFILES_YML_BASE64 | base64 --decode > $HOME/.dbt/profiles.yml
- name: Setup variables
run: |
CI_ID="${{ inputs.pr_number != '' && format('pr{0}', inputs.pr_number) || 'manual' }}"
echo "DBT_CI_SCHEMA=dune_spellbook_ci__tmp_${CI_ID}_${{ github.run_id }}_${{ github.run_attempt }}" >> $GITHUB_ENV
echo "PROFILE=--profiles-dir $HOME/.dbt --profile dunesql --target ci" >> $GITHUB_ENV
echo "S3_LOCATION=s3://manifest-spellbook-dunesql/${{inputs.project}}" >> $GITHUB_ENV
PROJECT_DIR=dbt_subprojects/${{ inputs.project }}
echo "PROJECT_DIR=$PROJECT_DIR" >> $GITHUB_ENV
- name: Get latest manifest
run: "aws s3 cp $S3_LOCATION/manifest.json $PROJECT_DIR/manifest.json --no-sign-request --region=eu-west-1"
- name: dbt dependencies
working-directory: ${{env.PROJECT_DIR}}
run: "uv run dbt deps"
- name: Activate DuneSQL Cluster
run: "uv run bash ./scripts/ensure_cluster.sh"
- name: dbt compile to create manifest to compare to
run: "uv run dbt --warn-error compile $PROFILE --project-dir ${PROJECT_DIR}"
- name: Validate meta.monitoring declarations
run: "uv run python scripts/validate_monitoring.py --manifest ${PROJECT_DIR}/target/manifest.json"
- name: check schemas
run: "uv run bash ./scripts/check_schema.sh"
- name: dbt seed
run: "uv run dbt seed $PROFILE --select @state:modified.body @state:modified.macros --exclude tag:prod_exclude tag:remove --state . --project-dir ${PROJECT_DIR}"
- name: list downstream models
run: "uv run bash ./scripts/list_modified_downstream.sh"
- name: dbt run initial model(s)
run: "uv run dbt -x run $PROFILE --select state:modified.body state:modified.macros --exclude tag:prod_exclude tag:remove --defer --state . --project-dir ${PROJECT_DIR}"
- name: dbt test initial model(s)
run: "uv run dbt test $PROFILE --select state:new state:modified.body state:modified.macros --exclude tag:prod_exclude tag:remove --defer --state . --project-dir ${PROJECT_DIR}"
- name: Set environment variable for incremental model count
run: |
echo "INC_MODEL_COUNT=$(uv run dbt ls $PROFILE --select state:modified.body,config.materialized:incremental state:modified.macros,config.materialized:incremental --state . --resource-type model --project-dir ${PROJECT_DIR} | wc -l)" >> $GITHUB_ENV
- name: dbt run incremental model(s) if applicable
if: env.INC_MODEL_COUNT > 0
run: "uv run dbt run $PROFILE --select state:modified.body,config.materialized:incremental state:modified.macros,config.materialized:incremental --exclude tag:prod_exclude tag:remove --defer --state . --project-dir ${PROJECT_DIR}"
- name: dbt test incremental model(s) if applicable
if: env.INC_MODEL_COUNT > 0
run: "uv run dbt test $PROFILE --select state:modified.body,config.materialized:incremental state:modified.macros,config.materialized:incremental --exclude tag:prod_exclude tag:remove --defer --state . --project-dir ${PROJECT_DIR}"
- name: Report final status to PR
if: always() && inputs.head_sha != ''
env:
GH_TOKEN: ${{ github.token }}
run: |
if [ "${{ job.status }}" == "success" ]; then state=success; else state=failure; fi
gh api "repos/${{ github.repository }}/statuses/${{ inputs.head_sha }}" \
-f "state=$state" \
-f "context=dbt-ci/${{ inputs.project }}" \
-f "description=dbt CI $state" \
-f "target_url=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"