-
Notifications
You must be signed in to change notification settings - Fork 131
461 lines (407 loc) · 17.6 KB
/
Copy pathdbt_ci_modes.yml
File metadata and controls
461 lines (407 loc) · 17.6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
name: Tuva CI
run-name: >-
Tuva CI / PR #${{
github.event_name == 'workflow_dispatch' &&
github.event.inputs.pr_number ||
github.event.pull_request.number
}} / ${{
github.event_name == 'workflow_dispatch' &&
(
github.event.inputs.command_label != '' &&
github.event.inputs.command_label ||
format('{0}-{1}', github.event.inputs.operation, github.event.inputs.target)
) ||
'run-snowflake'
}}
on:
pull_request:
branches:
- main
types:
- opened
workflow_dispatch:
inputs:
pr_number:
description: Pull request number to run against
required: true
type: string
dbt_command:
description: Full dbt command or ordered command sequence, for example `dbt seed --select tag:tuva_demo dbt run --select tag:tuva_demo`
required: false
type: string
default: ""
targets_csv:
description: Comma-separated warehouse list, or `all`
required: false
type: string
default: ""
command_label:
description: Short label for the run name
required: false
type: string
default: ""
operation:
description: CI operation to run
required: false
type: choice
default: run
options:
- run
- build
target:
description: Warehouse target
required: false
type: choice
default: snowflake
options:
- all
- snowflake
- bigquery
- databricks
- fabric
- redshift
- duckdb
permissions:
contents: read
pull-requests: read
jobs:
resolve_request:
name: Resolve CI Request
runs-on: ubuntu-latest
outputs:
pr_number: ${{ steps.resolve.outputs.pr_number }}
adapter_matrix: ${{ steps.resolve.outputs.adapter_matrix }}
checkout_ref: ${{ steps.resolve.outputs.checkout_ref }}
dbt_commands_json: ${{ steps.validate.outputs.dbt_commands_json }}
dbt_command_display: ${{ steps.validate.outputs.dbt_command }}
subcommand: ${{ steps.validate.outputs.subcommand }}
requires_seed_baseline: ${{ steps.validate.outputs.requires_seed_baseline }}
refreshes_seeds: ${{ steps.validate.outputs.refreshes_seeds }}
steps:
- name: Checkout workflow utilities
uses: actions/checkout@v4
- name: Validate requested dbt command
id: validate
run: python scripts/parse_ci_command.py resolve-dispatch
env:
CI_DBT_COMMAND: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.dbt_command || '' }}
CI_OPERATION: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.operation || 'run' }}
CI_TARGET: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.target || 'snowflake' }}
CI_TARGETS_CSV: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.targets_csv || '' }}
- name: Resolve target matrix and guardrails
id: resolve
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
const isDispatch = context.eventName === "workflow_dispatch";
const inputs = context.payload.inputs || {};
const requestedCommand = `${{ steps.validate.outputs.dbt_command }}`;
const requestedTargets = JSON.parse(String.raw`${{ steps.validate.outputs.targets_json }}`);
const requestedTargetsCsv = `${{ steps.validate.outputs.targets_csv }}`;
const refreshesSeeds = `${{ steps.validate.outputs.refreshes_seeds }}` === "true";
const requiresSeedBaseline = `${{ steps.validate.outputs.requires_seed_baseline }}` === "true";
let prNumber;
if (isDispatch) {
const rawPrNumber = String(inputs.pr_number || "").trim();
if (!/^\d+$/.test(rawPrNumber)) {
core.setFailed(`Invalid workflow_dispatch input pr_number: "${rawPrNumber}".`);
return;
}
prNumber = Number(rawPrNumber);
} else {
prNumber = context.payload.pull_request.number;
}
const { data: pr } = await github.rest.pulls.get({
owner,
repo,
pull_number: prNumber
});
if (pr.base.ref !== "main") {
core.setFailed(`PR #${prNumber} does not target main (targets ${pr.base.ref}).`);
return;
}
const repoFullName = `${owner}/${repo}`.toLowerCase();
const headRepoFullName = String(pr.head.repo.full_name || "").toLowerCase();
if (headRepoFullName !== repoFullName) {
core.setFailed(
`PR #${prNumber} comes from fork \`${pr.head.repo.full_name}\`. This secrets-backed CI workflow only runs on in-repo PRs. Use the outside contributor bridge workflow first.`
);
return;
}
const files = await github.paginate(github.rest.pulls.listFiles, {
owner,
repo,
pull_number: prNumber,
per_page: 100
});
const changed = files.map((f) => f.filename);
const runGuardPrefixes = ["seeds/", "integration_tests/seeds/"];
const runGuardExact = new Set([
"dbt_project.yml",
"integration_tests/dbt_project.yml",
"integration_tests/seeds/_seeds.yml",
"integration_tests/models/_sources.yml",
"macros/cross_database_utils/load_seed.sql"
]);
const requiresBuild = changed.some(
(path) =>
runGuardExact.has(path) ||
runGuardPrefixes.some((prefix) => path.startsWith(prefix))
);
const touchesSeedState = (paths) =>
paths.some(
(path) =>
runGuardExact.has(path) ||
runGuardPrefixes.some((prefix) => path.startsWith(prefix))
);
function parseDisplayTitle(run) {
const prefix = `Tuva CI / PR #${prNumber} / `;
const displayTitle = String(run.display_title || "");
if (!displayTitle.startsWith(prefix)) {
return null;
}
const label = displayTitle.slice(prefix.length);
const separator = label.indexOf(": ");
if (separator === -1) {
return null;
}
return {
targetLabel: label.slice(0, separator).trim().toLowerCase(),
commandLabel: label.slice(separator + 2).trim().toLowerCase(),
};
}
function labelIncludesTarget(targetLabel, targetName) {
if (targetLabel === "all") {
return true;
}
return targetLabel
.split(",")
.map((part) => part.trim())
.filter(Boolean)
.includes(targetName);
}
function labelRefreshesSeeds(commandLabel) {
return commandLabel
.split(" -> ")
.map((part) => part.trim())
.some((part) => part.startsWith("dbt seed") || part.startsWith("dbt build"));
}
async function hasReusableSeedRefresh(targetName) {
const runs = await github.paginate(github.rest.actions.listWorkflowRuns, {
owner,
repo,
workflow_id: "dbt_ci_modes.yml",
event: "workflow_dispatch",
per_page: 100,
});
for (const run of runs) {
if (run.status !== "completed" || run.conclusion !== "success") {
continue;
}
const parsedTitle = parseDisplayTitle(run);
if (!parsedTitle) {
continue;
}
if (!labelIncludesTarget(parsedTitle.targetLabel, targetName)) {
continue;
}
if (!labelRefreshesSeeds(parsedTitle.commandLabel)) {
continue;
}
if (run.head_sha === pr.head.sha) {
return true;
}
try {
const comparison = await github.rest.repos.compareCommits({
owner,
repo,
base: run.head_sha,
head: pr.head.sha,
});
const comparisonPaths = (comparison.data.files || []).map((file) => file.filename);
if (!touchesSeedState(comparisonPaths)) {
return true;
}
} catch (error) {
core.info(
`Unable to compare ${run.head_sha} to ${pr.head.sha} while checking seed baseline reuse: ${error.message}`
);
}
}
return false;
}
if (isDispatch && requiresBuild && requiresSeedBaseline) {
const reusableTargets = [];
for (const targetName of requestedTargets) {
if (await hasReusableSeedRefresh(targetName)) {
reusableTargets.push(targetName);
}
}
if (reusableTargets.length !== requestedTargets.length) {
const sample = changed
.filter((path) => touchesSeedState([path]))
.slice(0, 8)
.join(", ");
const ciTargetPrefix = requestedTargetsCsv === "all" ? "all" : requestedTargets.join(" ");
core.setFailed(
`This PR changes seed/config files (${sample}). The requested sequence \`${requestedCommand}\` reaches dbt run/test before refreshing seeds. Run a seed-refreshing command such as \`/ci ${ciTargetPrefix} dbt seed\` or \`/ci ${ciTargetPrefix} dbt build --full-refresh\` before running this sequence.`
);
return;
}
}
core.setOutput("pr_number", String(prNumber));
core.setOutput("adapter_matrix", JSON.stringify(requestedTargets));
core.setOutput("checkout_ref", `refs/pull/${prNumber}/merge`);
run_dbt:
name: >-
Commands / DW: ${{
matrix.adapter == 'snowflake' && 'Snowflake' ||
matrix.adapter == 'bigquery' && 'BigQuery' ||
matrix.adapter == 'databricks' && 'Databricks' ||
matrix.adapter == 'fabric' && 'Fabric' ||
matrix.adapter == 'redshift' && 'Redshift' ||
matrix.adapter == 'duckdb' && 'DuckDB/MotherDuck' ||
matrix.adapter
}}
needs: resolve_request
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
adapter: ${{ fromJson(needs.resolve_request.outputs.adapter_matrix) }}
concurrency:
group: ci-pr-${{ needs.resolve_request.outputs.pr_number }}-${{ matrix.adapter }}
cancel-in-progress: true
env:
PYTHON_VERSION: "3.10"
DBT_PROJECT_DIR: "./integration_tests"
DBT_CORE_VERSION: "1.10.15"
DUCKDB_VERSION: "1.4.1"
DBT_BIGQUERY_CI_TOKEN: ${{ secrets.DBT_BIGQUERY_CI_TOKEN }}
DBT_BIGQUERY_CI_PROJECT: ${{ secrets.DBT_BIGQUERY_CI_PROJECT }}
DBT_DATABRICKS_CI_HOST: ${{ secrets.DBT_DATABRICKS_CI_HOST }}
DBT_DATABRICKS_CI_HTTP_PATH: ${{ secrets.DBT_DATABRICKS_CI_HTTP_PATH }}
DBT_DATABRICKS_CI_TOKEN: ${{ secrets.DBT_DATABRICKS_CI_TOKEN }}
DBT_DATABRICKS_CI_CATALOG: ${{ secrets.DBT_DATABRICKS_CI_CATALOG }}
DBT_MOTHERDUCK_CI_PATH: ${{ secrets.DBT_MOTHERDUCK_CI_PATH }}
DBT_MOTHERDUCK_CI_DATABASE: "my_db"
DBT_FABRIC_CI_SERVER: ${{ secrets.DBT_FABRIC_CI_SERVER }}
DBT_FABRIC_CI_DATABASE: ${{ secrets.DBT_FABRIC_CI_DATABASE }}
DBT_FABRIC_CI_SCHEMA: ${{ secrets.DBT_FABRIC_CI_SCHEMA }}
DBT_FABRIC_CI_CLIENT_ID: ${{ secrets.DBT_FABRIC_CI_CLIENT_ID }}
DBT_FABRIC_CI_CLIENT_SECRET: ${{ secrets.DBT_FABRIC_CI_CLIENT_SECRET }}
DBT_FABRIC_CI_TENANT_ID: ${{ secrets.DBT_FABRIC_CI_TENANT_ID }}
DBT_REDSHIFT_CI_HOST: ${{ secrets.DBT_REDSHIFT_CI_HOST }}
DBT_REDSHIFT_CI_USER: ${{ secrets.DBT_REDSHIFT_CI_USER }}
DBT_REDSHIFT_CI_PASSWORD: ${{ secrets.DBT_REDSHIFT_CI_PASSWORD }}
DBT_REDSHIFT_CI_DATABASE: ${{ secrets.DBT_REDSHIFT_CI_DATABASE }}
DBT_REDSHIFT_CI_THREADS: ${{ matrix.adapter == 'redshift' && '4' || '' }}
DBT_REDSHIFT_CI_SEED_BATCH_SIZE: ${{ matrix.adapter == 'redshift' && '2000' || '' }}
DBT_SNOWFLAKE_CI_ACCOUNT: ${{ secrets.DBT_SNOWFLAKE_CI_ACCOUNT }}
DBT_SNOWFLAKE_CI_DATABASE: ${{ secrets.DBT_SNOWFLAKE_CI_DATABASE }}
DBT_SNOWFLAKE_CI_PASSWORD: ${{ secrets.DBT_SNOWFLAKE_CI_PASSWORD }}
DBT_SNOWFLAKE_CI_ROLE: ${{ secrets.DBT_SNOWFLAKE_CI_ROLE }}
DBT_SNOWFLAKE_CI_SCHEMA: ${{ secrets.DBT_SNOWFLAKE_CI_SCHEMA }}
DBT_SNOWFLAKE_CI_USER: ${{ secrets.DBT_SNOWFLAKE_CI_USER }}
DBT_SNOWFLAKE_CI_WAREHOUSE: ${{ secrets.DBT_SNOWFLAKE_CI_WAREHOUSE }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: ${{ needs.resolve_request.outputs.checkout_ref }}
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install ODBC Driver 18 for SQL Server
if: ${{ matrix.adapter == 'fabric' }}
run: |
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
- name: Install dbt adapter
run: |
python -m pip install --upgrade pip
case "${{ matrix.adapter }}" in
snowflake)
pip install dbt-core==${{ env.DBT_CORE_VERSION }} dbt-snowflake
;;
bigquery)
pip install dbt-core==${{ env.DBT_CORE_VERSION }} dbt-bigquery
;;
databricks)
pip install dbt-core==${{ env.DBT_CORE_VERSION }} dbt-databricks
pip install certifi
;;
fabric)
pip install dbt-core==${{ env.DBT_CORE_VERSION }} dbt-fabric
;;
redshift)
pip install dbt-core==${{ env.DBT_CORE_VERSION }} dbt-redshift
;;
duckdb)
pip install dbt-core==${{ env.DBT_CORE_VERSION }} dbt-duckdb duckdb==${{ env.DUCKDB_VERSION }}
;;
*)
echo "Unsupported adapter: ${{ matrix.adapter }}" >&2
exit 1
;;
esac
- name: Create BigQuery credentials file
if: ${{ matrix.adapter == 'bigquery' }}
run: |
echo "${{ env.DBT_BIGQUERY_CI_TOKEN }}" | base64 --decode > ./creds.json
- name: Install dbt dependencies
run: dbt deps --project-dir ./integration_tests --profiles-dir ./integration_tests/profiles/${{ matrix.adapter }}
- name: Test connection
run: dbt debug --project-dir ./integration_tests --profiles-dir ./integration_tests/profiles/${{ matrix.adapter }}
- name: Assert baseline seed schemas exist for run modes
if: ${{ needs.resolve_request.outputs.requires_seed_baseline == 'true' }}
run: |
dbt run-operation assert_ci_seed_baseline_ready \
--project-dir ./integration_tests \
--profiles-dir ./integration_tests/profiles/${{ matrix.adapter }}
- name: Execute dbt
env:
DBT_COMMANDS_JSON: ${{ needs.resolve_request.outputs.dbt_commands_json }}
DBT_ADAPTER: ${{ matrix.adapter }}
CI_REQUIRES_SEED_BASELINE: ${{ needs.resolve_request.outputs.requires_seed_baseline }}
run: |
python - <<'PY'
import json
import os
import shlex
import subprocess
commands = json.loads(os.environ["DBT_COMMANDS_JSON"])
child_env = os.environ.copy()
seed_refreshing_subcommands = {"seed", "build"}
baseline_required = child_env.get("CI_REQUIRES_SEED_BASELINE") == "true"
seed_refreshed_in_sequence = False
for index, raw_command in enumerate(commands, start=1):
command = list(raw_command)
subcommand = command[1].lower()
use_baseline_seeds = (
subcommand in seed_refreshing_subcommands
or seed_refreshed_in_sequence
or (baseline_required and subcommand in {"run", "test"})
)
command.extend(
[
"--project-dir",
"./integration_tests",
"--profiles-dir",
f"./integration_tests/profiles/{os.environ['DBT_ADAPTER']}",
]
)
child_env["DBT_CI_USE_BASELINE_SEEDS"] = "true" if use_baseline_seeds else "false"
if child_env["DBT_CI_USE_BASELINE_SEEDS"] == "true" and "--no-partial-parse" not in command:
command.append("--no-partial-parse")
print(f"Step {index}: DBT_CI_USE_BASELINE_SEEDS={child_env['DBT_CI_USE_BASELINE_SEEDS']}")
print(f"Running: {shlex.join(command)}")
subprocess.run(command, check=True, env=child_env)
if subcommand in seed_refreshing_subcommands:
seed_refreshed_in_sequence = True
PY