Skip to content

Commit eba4cfd

Browse files
committed
wip
remove matrix add model changed, sync metabase and upload
1 parent 6f77322 commit eba4cfd

File tree

1 file changed

+220
-36
lines changed

1 file changed

+220
-36
lines changed

.github/workflows/deploy-dbt-docs.yml

Lines changed: 220 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: Deploy dbt docs
22

33
on:
4-
schedule:
5-
- cron: "0 8 * * 1" # 8am UTC (1am PST) every Monday
4+
# schedule:
5+
# - cron: "0 8 * * 1" # 8am UTC (1am PST) every Monday
66
push:
77
branches:
88
- main
@@ -21,26 +21,42 @@ concurrency:
2121
env:
2222
PYTHON_VERSION: '3.11'
2323
POETRY_VERSION: '2.0.1'
24+
SERVICE_ACCOUNT: ${{ github.ref == 'refs/heads/main' && 'github-actions-service-account@cal-itp-data-infra.iam.gserviceaccount.com' || 'github-actions-service-account@cal-itp-data-infra-staging.iam.gserviceaccount.com' }}
25+
WORKLOAD_IDENTITY_PROVIDER: ${{ github.ref == 'refs/heads/main' && 'projects/1005246706141/locations/global/workloadIdentityPools/github-actions/providers/data-infra' || 'projects/473674835135/locations/global/workloadIdentityPools/github-actions/providers/data-infra' }}
26+
PROJECT_ID: ${{ github.ref == 'refs/heads/main' && 'cal-itp-data-infra' || 'cal-itp-data-infra-staging' }}
27+
DBT_TARGET: ${{ github.ref == 'refs/heads/main' && 'prod' || 'staging' }}
28+
DBT_ARTIFACTS_BUCKET: ${{ github.ref == 'refs/heads/main' && 'calitp-dbt-artifacts' || 'calitp-staging-dbt-artifacts' }}
29+
DBT_DOCS_BUCKET: ${{ github.ref == 'refs/heads/main' && 'calitp-dbt-docs' || 'calitp-staging-dbt-docs' }}
30+
DBT_SOURCE_DATABASE: ${{ github.ref == 'refs/heads/main' && 'cal-itp-data-infra' || 'cal-itp-data-infra-staging' }}
31+
METABASE_DESTINATION_DATABASE: ${{ github.ref == 'refs/heads/main' && 'Data Marts (formerly Warehouse Views)' || '(Internal) Staging Warehouse Views' }}
2432

2533
jobs:
26-
compile:
27-
name: Compile dbt docs
34+
models-changed:
35+
name: Detect dbt model changes
36+
runs-on: ubuntu-latest
37+
38+
outputs:
39+
any_changed: ${{ steps.changed-files-warehouse.outputs.any_changed }}
40+
41+
steps:
42+
- name: Checkout
43+
uses: actions/checkout@v4
44+
with:
45+
fetch-depth: 0
46+
47+
- uses: tj-actions/changed-files@v46
48+
id: changed-files-warehouse
49+
with:
50+
files: 'warehouse/models/**/*.sql'
51+
52+
export_exposures:
53+
name: Export exposures
2854
runs-on: ubuntu-latest
2955

3056
permissions:
3157
contents: read
3258
id-token: write
3359

34-
strategy:
35-
matrix:
36-
include:
37-
- service_account: 'github-actions-service-account@cal-itp-data-infra-staging.iam.gserviceaccount.com'
38-
workload_identity_provider: 'projects/473674835135/locations/global/workloadIdentityPools/github-actions/providers/data-infra'
39-
project_id: 'cal-itp-data-infra-staging'
40-
dbt_target: 'staging'
41-
dbt_artifacts_bucket: 'calitp-staging-dbt-artifacts'
42-
dbt_docs_bucket: 'calitp-staging-dbt-docs'
43-
4460
steps:
4561
- name: Checkout
4662
uses: actions/checkout@v4
@@ -49,9 +65,9 @@ jobs:
4965
uses: google-github-actions/auth@v2
5066
with:
5167
create_credentials_file: 'true'
52-
project_id: ${{ matrix.project_id }}
53-
workload_identity_provider: ${{ matrix.workload_identity_provider }}
54-
service_account: ${{ matrix.service_account }}
68+
project_id: ${{ env.PROJECT_ID }}
69+
service_account: ${{ env.SERVICE_ACCOUNT }}
70+
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
5571

5672
- name: Setup GCloud utilities
5773
uses: google-github-actions/setup-gcloud@v2
@@ -97,11 +113,15 @@ jobs:
97113

98114
- name: Print dbt environment
99115
working-directory: warehouse
100-
run: poetry run dbt debug --target ${{ matrix.dbt_target }}
116+
run: poetry run dbt debug --target ${{ env.DBT_TARGET }}
101117

102118
- name: Compile dbt
103119
working-directory: warehouse
104-
run: poetry run dbt compile --target ${{ matrix.dbt_target }} --full-refresh
120+
run: poetry run dbt compile --target ${{ env.DBT_TARGET }} --full-refresh
121+
122+
- name: List target dir
123+
working-directory: warehouse/target
124+
run: ls
105125

106126
- name: Create Metabase folder
107127
working-directory: warehouse/models
@@ -111,13 +131,92 @@ jobs:
111131
working-directory: warehouse
112132
run: poetry run dbt-metabase exposures -v --output-grouping="type" --output-path=./models/metabase/ --manifest-path=./target/manifest.json --metabase-url="https://dashboards.calitp.org" --metabase-api-key="${{ secrets.METABASE_API_KEY }}"
113133

134+
- name: Archive metabase exposures
135+
uses: actions/upload-artifact@v4
136+
with:
137+
name: exposures
138+
path: warehouse/models/metabase/*
139+
140+
compile:
141+
name: Compile dbt docs
142+
runs-on: ubuntu-latest
143+
needs: [export_exposures]
144+
145+
permissions:
146+
contents: read
147+
id-token: write
148+
149+
steps:
150+
- name: Checkout
151+
uses: actions/checkout@v4
152+
153+
- name: Download Metabase exposures
154+
uses: actions/download-artifact@v4
155+
with:
156+
name: exposures
157+
path: warehouse/models/metabase/*
158+
159+
- name: Authenticate Google Service Account
160+
uses: google-github-actions/auth@v2
161+
with:
162+
create_credentials_file: 'true'
163+
project_id: ${{ env.PROJECT_ID }}
164+
service_account: ${{ env.SERVICE_ACCOUNT }}
165+
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
166+
167+
- name: Setup GCloud utilities
168+
uses: google-github-actions/setup-gcloud@v2
169+
170+
- name: Setup Graphviz
171+
uses: ts-graphviz/setup-graphviz@v2
172+
173+
- name: Setup Python
174+
uses: actions/setup-python@v5
175+
with:
176+
python-version: ${{ env.PYTHON_VERSION }}
177+
178+
- name: Cache Poetry
179+
uses: actions/cache@v3
180+
with:
181+
path: ~/.cache/pypoetry
182+
key: poetry-cache-${{ runner.os }}-python-${{ env.PYTHON_VERSION }}-poetry-${{ env.POETRY_VERSION }}
183+
184+
- name: Setup Poetry
185+
uses: abatilo/actions-poetry@v3
186+
with:
187+
poetry-version: ${{ env.POETRY_VERSION }}
188+
189+
- name: Cache Python packages
190+
uses: actions/cache@v3
191+
with:
192+
path: ~/.local
193+
key: python-cache-${{ runner.os }}-python-${{ env.PYTHON_VERSION }}-lock-${{ hashFiles('warehouse/poetry.lock') }}-${{ hashFiles('.github/workflows/*.yml') }}
194+
195+
- name: Install dependencies
196+
working-directory: warehouse
197+
run: poetry install
198+
199+
- name: Cache dbt packages
200+
uses: actions/cache@v3
201+
with:
202+
path: warehouse/dbt_packages
203+
key: python-cache-${{ runner.os }}-python-${{ env.PYTHON_VERSION }}-lock-${{ hashFiles('warehouse/poetry.lock') }}-dbt-packages-${{ hashFiles('warehouse/packages.yml') }}
204+
205+
- name: Install dbt dependencies
206+
working-directory: warehouse
207+
run: poetry run dbt deps
208+
209+
- name: Print dbt environment
210+
working-directory: warehouse
211+
run: poetry run dbt debug --target ${{ env.DBT_TARGET }}
212+
114213
- name: Compile dbt with exposures
115214
working-directory: warehouse
116-
run: poetry run dbt compile --target ${{ matrix.dbt_target }} --full-refresh
215+
run: poetry run dbt compile --target ${{ env.DBT_TARGET }} --full-refresh
117216

118217
- name: Generate dbt documentation
119218
working-directory: warehouse
120-
run: poetry run dbt docs generate --target ${{ matrix.dbt_target }} --no-compile
219+
run: poetry run dbt docs generate --target ${{ env.DBT_TARGET }} --no-compile
121220

122221
- name: Archive compilation artifacts
123222
uses: actions/upload-artifact@v4
@@ -127,21 +226,106 @@ jobs:
127226
warehouse/target/*.json
128227
warehouse/target/*.html
129228
130-
- name: Archive metabase exposures
131-
uses: actions/upload-artifact@v4
229+
metabase:
230+
name: Sync Metabase
231+
runs-on: ubuntu-latest
232+
needs: [models-changed, compile]
233+
234+
if: ${{ needs.models-changed.outputs.any_changed == 'true' }}
235+
236+
permissions:
237+
contents: read
238+
id-token: write
239+
240+
steps:
241+
- name: Checkout
242+
uses: actions/checkout@v4
243+
244+
- name: Download compilation artifacts
245+
uses: actions/download-artifact@v4
132246
with:
133-
name: exposures
134-
path: warehouse/models/metabase/*
247+
name: dbt
248+
path: warehouse/target
249+
250+
- name: Authenticate Google Service Account
251+
uses: google-github-actions/auth@v2
252+
with:
253+
create_credentials_file: 'true'
254+
project_id: ${{ env.PROJECT_ID }}
255+
service_account: ${{ env.SERVICE_ACCOUNT }}
256+
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
257+
258+
- name: Setup GCloud utilities
259+
uses: google-github-actions/setup-gcloud@v2
260+
261+
- name: Setup Graphviz
262+
uses: ts-graphviz/setup-graphviz@v2
263+
264+
- name: Setup Python
265+
uses: actions/setup-python@v5
266+
with:
267+
python-version: ${{ env.PYTHON_VERSION }}
268+
269+
- name: Cache Poetry
270+
uses: actions/cache@v3
271+
with:
272+
path: ~/.cache/pypoetry
273+
key: poetry-cache-${{ runner.os }}-python-${{ env.PYTHON_VERSION }}-poetry-${{ env.POETRY_VERSION }}
274+
275+
- name: Setup Poetry
276+
uses: abatilo/actions-poetry@v3
277+
with:
278+
poetry-version: ${{ env.POETRY_VERSION }}
279+
280+
- name: Cache Python packages
281+
uses: actions/cache@v3
282+
with:
283+
path: ~/.local
284+
key: python-cache-${{ runner.os }}-python-${{ env.PYTHON_VERSION }}-lock-${{ hashFiles('warehouse/poetry.lock') }}-${{ hashFiles('.github/workflows/*.yml') }}
285+
286+
- name: Install dependencies
287+
working-directory: warehouse
288+
run: poetry install
289+
290+
- name: Cache dbt packages
291+
uses: actions/cache@v3
292+
with:
293+
path: warehouse/dbt_packages
294+
key: python-cache-${{ runner.os }}-python-${{ env.PYTHON_VERSION }}-lock-${{ hashFiles('warehouse/poetry.lock') }}-dbt-packages-${{ hashFiles('warehouse/packages.yml') }}
295+
296+
- name: Install dbt dependencies
297+
working-directory: warehouse
298+
run: poetry run dbt deps
299+
300+
- name: Download latest artifacts from GCS
301+
working-directory: warehouse
302+
# if: ${{ env.DBT_TARGET == 'staging' }}
303+
run: gsutil cp -r gs://${{ env.DBT_ARTIFACTS_BUCKET }}/latest/ ./target/
304+
305+
- name: Print dbt environment
306+
working-directory: warehouse
307+
run: poetry run dbt debug --target ${{ env.DBT_TARGET }}
308+
309+
- name: Run changed models
310+
working-directory: warehouse
311+
# if: ${{ env.DBT_TARGET == 'staging' }}
312+
run: poetry run dbt run --select state:modified+ --target ${{ env.DBT_TARGET }} --state ./target/latest
313+
314+
- name: Synchronize Metabase
315+
working-directory: warehouse
316+
run: poetry run dbt-metabase models -v --manifest-path=target/manifest.json --exclude-schemas="*staging, payments" --skip-sources --docs-url="https://dbt-docs.calitp.org" --metabase-url="https://dashboards.calitp.org" --metabase-database="${{ env.METABASE_DESTINATION_DATABASE }}" --metabase-api-key="${{ secrets.METABASE_API_KEY}}"
135317

136318
upload:
137319
name: Upload to Google Cloud Storage
138-
needs: [compile]
139320
runs-on: ubuntu-latest
321+
needs: [compile, metabase]
140322

141323
permissions:
142324
contents: read
143325
id-token: write
144326

327+
if: ${{ always() && !failure() && !cancelled() }}
328+
145329
steps:
146330
- name: Download compilation artifacts
147331
uses: actions/download-artifact@v4
@@ -159,9 +343,9 @@ jobs:
159343
uses: google-github-actions/auth@v2
160344
with:
161345
create_credentials_file: 'true'
162-
project_id: ${{ matrix.project_id }}
163-
workload_identity_provider: ${{ matrix.workload_identity_provider }}
164-
service_account: ${{ matrix.service_account }}
346+
project_id: ${{ env.PROJECT_ID }}
347+
service_account: ${{ env.SERVICE_ACCOUNT }}
348+
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
165349

166350
- name: Setup GCloud utilities
167351
uses: google-github-actions/setup-gcloud@v2
@@ -187,7 +371,7 @@ jobs:
187371
glob: 'catalog.json'
188372
parent: false
189373
process_gcloudignore: false
190-
destination: "${{ matrix.dbt_artifacts_bucket }}/catalog.json/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
374+
destination: "${{ env.DBT_ARTIFACTS_BUCKET }}/catalog.json/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
191375

192376
- name: Upload manifest.json
193377
uses: google-github-actions/upload-cloud-storage@v1
@@ -196,7 +380,7 @@ jobs:
196380
glob: 'manifest.json'
197381
parent: false
198382
process_gcloudignore: false
199-
destination: "${{ matrix.dbt_artifacts_bucket }}/manifest.json/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
383+
destination: "${{ env.DBT_ARTIFACTS_BUCKET }}/manifest.json/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
200384

201385
- name: Upload run_results.json
202386
uses: google-github-actions/upload-cloud-storage@v1
@@ -205,7 +389,7 @@ jobs:
205389
glob: 'run_results.json'
206390
parent: false
207391
process_gcloudignore: false
208-
destination: "${{ matrix.dbt_artifacts_bucket }}/run_results.json/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
392+
destination: "${{ env.DBT_ARTIFACTS_BUCKET }}/run_results.json/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
209393

210394
- name: Upload index.html
211395
uses: google-github-actions/upload-cloud-storage@v1
@@ -214,7 +398,7 @@ jobs:
214398
glob: 'index.html'
215399
parent: false
216400
process_gcloudignore: false
217-
destination: "${{ matrix.dbt_artifacts_bucket }}/index.html/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
401+
destination: "${{ env.DBT_ARTIFACTS_BUCKET }}/index.html/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
218402

219403
- name: Upload artifacts to latest
220404
uses: google-github-actions/upload-cloud-storage@v1
@@ -223,15 +407,15 @@ jobs:
223407
glob: '{catalog.json,manifest.json,index.html,run_results.json}'
224408
parent: false
225409
process_gcloudignore: false
226-
destination: "${{ matrix.dbt_artifacts_bucket }}/latest/"
410+
destination: "${{ env.DBT_ARTIFACTS_BUCKET }}/latest/"
227411

228412
- name: Upload Metabase exposures
229413
uses: google-github-actions/upload-cloud-storage@v1
230414
with:
231415
path: './warehouse/models/metabase'
232416
parent: false
233417
process_gcloudignore: false
234-
destination: "${{ matrix.dbt_artifacts_bucket }}/exposures/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
418+
destination: "${{ env.DBT_ARTIFACTS_BUCKET }}/exposures/dt=${{ steps.current-date.outputs.formattedTime }}/ts=${{ steps.current-time.outputs.formattedTime }}/"
235419

236420
- name: Upload documentation
237421
uses: google-github-actions/upload-cloud-storage@v1
@@ -240,4 +424,4 @@ jobs:
240424
glob: '{catalog.json,manifest.json,index.html}'
241425
parent: false
242426
process_gcloudignore: false
243-
destination: ${{ matrix.dbt_docs_bucket }}
427+
destination: ${{ env.DBT_DOCS_BUCKET }}

0 commit comments

Comments
 (0)