-
Notifications
You must be signed in to change notification settings - Fork 0
208 lines (175 loc) · 7.53 KB
/
dbt_cd.yml
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
name: CD clean up and deploy to production
concurrency: dbt-deploy
on:
pull_request:
types:
- closed
permissions:
contents: read
pages: write
id-token: write
env:
DEBUG: true
DBT_PROFILE_NAME: ${{ vars.DBT_PROFILE_NAME }}
DBT_SNOWFLAKE_ACCOUNT: ${{ secrets.DBT_SNOWFLAKE_ACCOUNT }}
DBT_SNOWFLAKE_USER: ${{ vars.DBT_SNOWFLAKE_USER }}
DBT_SNOWFLAKE_ROLE: ${{ vars.DBT_SNOWFLAKE_ROLE }}
DBT_SNOWFLAKE_PASSWORD: ${{ secrets.DBT_SNOWFLAKE_PASSWORD }}
DBT_SNOWFLAKE_DATABASE: ${{ vars.DBT_SNOWFLAKE_DATABASE }}
DBT_SNOWFLAKE_WAREHOUSE: ${{ vars.DBT_SNOWFLAKE_WAREHOUSE }}
DBT_SNOWFLAKE_PRODUCTION_SCHEMA: ${{ vars.DBT_SNOWFLAKE_PRODUCTION_SCHEMA }}
DBT_SNOWFLAKE_THREADS: ${{ vars.DBT_SNOWFLAKE_THREADS }}
PR_NUMBER: ${{ github.event.pull_request.number }}
BRANCH_NAME: ${{ github.head_ref }}
DBT_PROJECT_FOLDER: ${{ vars.DBT_PROJECT_FOLDER }}
jobs:
delete_branch:
name: Delete PR branch from Snowflake
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Create profiles.yml
run: |
mkdir -p /home/$(whoami)/.dbt/
echo "${DBT_PROFILE_NAME}:" > /home/$(whoami)/.dbt/profiles.yml
echo " target: development" >> /home/$(whoami)/.dbt/profiles.yml
echo " outputs:" >> /home/$(whoami)/.dbt/profiles.yml
echo " development:" >> /home/$(whoami)/.dbt/profiles.yml
echo " type: snowflake" >> /home/$(whoami)/.dbt/profiles.yml
echo " account: ${DBT_SNOWFLAKE_ACCOUNT}" >> /home/$(whoami)/.dbt/profiles.yml
echo " user: ${DBT_SNOWFLAKE_USER}" >> /home/$(whoami)/.dbt/profiles.yml
echo " role: ${DBT_SNOWFLAKE_ROLE}" >> /home/$(whoami)/.dbt/profiles.yml
echo " password: ${DBT_SNOWFLAKE_PASSWORD}" >> /home/$(whoami)/.dbt/profiles.yml
echo " database: ${DBT_SNOWFLAKE_DATABASE}" >> /home/$(whoami)/.dbt/profiles.yml
echo " warehouse: ${DBT_SNOWFLAKE_WAREHOUSE}" >> /home/$(whoami)/.dbt/profiles.yml
echo " schema: pr" >> /home/$(whoami)/.dbt/profiles.yml
echo " threads: ${DBT_SNOWFLAKE_THREADS}" >> /home/$(whoami)/.dbt/profiles.yml
echo " client_session_keep_alive: false" >> /home/$(whoami)/.dbt/profiles.yml
- name: Print profiles.yml
if: ${{ env.DEBUG == 'true' }}
run: |
echo "----------------------------------------"
echo "Debug mode: Show content of profiles.yml"
echo "Note that secrets will be masked"
echo "----------------------------------------"
cat /home/$(whoami)/.dbt/profiles.yml
- name: Read Python version from .python-version
run: echo "python_version=$(cat .python-version)" >> "$GITHUB_ENV"
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.python_version }}
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dbt
run: |
python -m pip install --upgrade pip
grep dbt-snowflake requirements.txt | xargs -n 1 pip install
- name: Run dbt deps
run: |
cd $DBT_PROJECT_FOLDER
dbt deps
- name: Run dbt debug
if: ${{ env.DEBUG == 'true' }}
run: |
cd $DBT_PROJECT_FOLDER
echo "----------------------------------------"
echo "Debug mode: Execute dbt debug"
echo "----------------------------------------"
dbt debug
- name: Drop schema
# the "|| true" is to prevent the job from failing if the schema does not exist
run: |
cd $DBT_PROJECT_FOLDER
BRANCH_NAME=$(echo "${BRANCH_NAME}" | tr '-' '_')
schema_name="pr_${BRANCH_NAME##*/}_${PR_NUMBER}"
dbt run-operation drop_schema --args "{'schema_name': '${schema_name}'}" || true
deploy_to_production:
name: Deploy to production
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Create profiles.yml
run: |
mkdir -p /home/$(whoami)/.dbt/
echo "${DBT_PROFILE_NAME}:" > /home/$(whoami)/.dbt/profiles.yml
echo " target: development" >> /home/$(whoami)/.dbt/profiles.yml
echo " outputs:" >> /home/$(whoami)/.dbt/profiles.yml
echo " development:" >> /home/$(whoami)/.dbt/profiles.yml
echo " type: snowflake" >> /home/$(whoami)/.dbt/profiles.yml
echo " account: ${DBT_SNOWFLAKE_ACCOUNT}" >> /home/$(whoami)/.dbt/profiles.yml
echo " user: ${DBT_SNOWFLAKE_USER}" >> /home/$(whoami)/.dbt/profiles.yml
echo " role: ${DBT_SNOWFLAKE_ROLE}" >> /home/$(whoami)/.dbt/profiles.yml
echo " password: ${DBT_SNOWFLAKE_PASSWORD}" >> /home/$(whoami)/.dbt/profiles.yml
echo " database: ${DBT_SNOWFLAKE_DATABASE}" >> /home/$(whoami)/.dbt/profiles.yml
echo " warehouse: ${DBT_SNOWFLAKE_WAREHOUSE}" >> /home/$(whoami)/.dbt/profiles.yml
echo " schema: ${DBT_SNOWFLAKE_PRODUCTION_SCHEMA}" >> /home/$(whoami)/.dbt/profiles.yml
echo " threads: ${DBT_SNOWFLAKE_THREADS}" >> /home/$(whoami)/.dbt/profiles.yml
echo " client_session_keep_alive: false" >> /home/$(whoami)/.dbt/profiles.yml
- name: Print profiles.yml
if: ${{ env.DEBUG == 'true' }}
run: |
echo "----------------------------------------"
echo "Debug mode: Show content of profiles.yml"
echo "Note that secrets will be masked"
echo "----------------------------------------"
cat /home/$(whoami)/.dbt/profiles.yml
- name: Read Python version from .python-version
run: echo "python_version=$(cat .python-version)" >> "$GITHUB_ENV"
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.python_version }}
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dbt
run: |
python -m pip install --upgrade pip
grep dbt-snowflake requirements.txt | xargs -n 1 pip install
- name: Run dbt deps
run: |
cd $DBT_PROJECT_FOLDER
dbt deps
- name: Run dbt debug
if: ${{ env.DEBUG == 'true' }}
run: |
cd $DBT_PROJECT_FOLDER
echo "----------------------------------------"
echo "Debug mode: Execute dbt debug"
echo "----------------------------------------"
dbt debug
- name: Deploy models to production schema
run: |
cd $DBT_PROJECT_FOLDER
dbt build
- name: Generate docs
run: |
cd $DBT_PROJECT_FOLDER
dbt docs generate
- name: Upload pages artifact
uses: actions/upload-pages-artifact@v1
with:
path: ${{ env.DBT_PROJECT_FOLDER }}/target
deploy-pages:
name: Deployment docs to pages
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: deploy_to_production
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2