-
Notifications
You must be signed in to change notification settings - Fork 7
399 lines (340 loc) · 13.3 KB
/
deploy.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
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
name: TRM Infrastructure Deployment
on:
push:
branches:
- staging
- production
jobs:
infrastructure_common:
name: Manage Common Infrastructure
runs-on: ubuntu-latest
env:
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
TF_VAR_project: techlabs-trm
TF_VAR_region: europe-west3
TF_VAR_domain: trm.techlabs.org
outputs:
artifact_registry_repository_prefix: ${{ steps.terraform_output.outputs.artifact_registry_repository_prefix }}
defaults:
run:
working-directory: infrastructure/common
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.13.5
terraform_wrapper: false
- name: terraform init
run: terraform init
- name: terraform apply
run: terraform apply -auto-approve
- name: terraform output
id: terraform_output
run: |
echo "::set-output name=artifact_registry_repository_prefix::$(terraform output artifact_registry_repository_prefix)"
hasura_image:
name: Build & Push Hasura Image
runs-on: ubuntu-latest
needs: [infrastructure_common]
defaults:
run:
working-directory: database
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: dorny/paths-filter@e2bed85912bf87322673b976228bde722213a6df
id: changes
with:
base: ${{ github.ref }}
filters: |
database:
- 'database/**'
- name: Setup gcloud
if: steps.changes.outputs.database == 'true'
uses: google-github-actions/setup-gcloud@v0
with:
version: '308.0.0'
service_account_key: ${{ secrets.GOOGLE_CREDENTIALS }}
export_default_credentials: true
- name: Configure gcloud Auth for Docker
if: steps.changes.outputs.database == 'true'
run: gcloud auth configure-docker europe-west3-docker.pkg.dev
- name: Try to pull existing image
if: steps.changes.outputs.database == 'true'
continue-on-error: true
run: docker pull ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/hasura:${GITHUB_REF##*/}
- name: Build Hasura image
if: steps.changes.outputs.database == 'true'
# ${GITHUB_REF##*/} gets branch name
run: docker build -t ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/hasura:${GITHUB_REF##*/} .
- name: Push Hasura image
if: steps.changes.outputs.database == 'true'
# ${GITHUB_REF##*/} gets branch name
run: docker push ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/hasura:${GITHUB_REF##*/}
gotenberg_image:
name: Build & Push Gotenberg Image
runs-on: ubuntu-latest
needs: [infrastructure_common]
defaults:
run:
working-directory: pdf-generator
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: dorny/paths-filter@e2bed85912bf87322673b976228bde722213a6df
id: changes
with:
base: ${{ github.ref }}
filters: |
pdf_generator:
- 'pdf-generator/**'
- name: Setup gcloud
if: steps.changes.outputs.pdf_generator == 'true'
uses: google-github-actions/setup-gcloud@v0
with:
version: '308.0.0'
service_account_key: ${{ secrets.GOOGLE_CREDENTIALS }}
export_default_credentials: true
- name: Configure gcloud Auth for Docker
if: steps.changes.outputs.pdf_generator == 'true'
run: gcloud auth configure-docker europe-west3-docker.pkg.dev
- name: Build image
if: steps.changes.outputs.pdf_generator == 'true'
run: docker build -t ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/gotenberg:${GITHUB_REF##*/} .
- name: Push image
if: steps.changes.outputs.pdf_generator == 'true'
# ${GITHUB_REF##*/} gets branch name
run: docker push ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/gotenberg:${GITHUB_REF##*/}
certificate_generator_image:
name: Build & Push Certificate Generator Image
runs-on: ubuntu-latest
needs: [infrastructure_common]
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: dorny/paths-filter@e2bed85912bf87322673b976228bde722213a6df
id: changes
with:
base: ${{ github.ref }}
filters: |
code_changed:
- 'functions/certificate-generator/**'
- 'functions/lib/trm-api/**'
- name: Setup gcloud
if: steps.changes.outputs.code_changed == 'true'
uses: google-github-actions/setup-gcloud@v0
with:
version: '308.0.0'
service_account_key: ${{ secrets.GOOGLE_CREDENTIALS }}
export_default_credentials: true
- name: Configure gcloud Auth for Docker
if: steps.changes.outputs.code_changed == 'true'
run: gcloud auth configure-docker europe-west3-docker.pkg.dev
- name: Try to pull existing image
if: steps.changes.outputs.code_changed == 'true'
continue-on-error: true
run: docker pull ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/certificate-generator:${GITHUB_REF##*/}
- name: Build image
if: steps.changes.outputs.code_changed == 'true'
# ${GITHUB_REF##*/} gets branch name
run: |
docker-compose build fn-certificate-generator
docker tag trm_fn-certificate-generator:latest ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/certificate-generator:${GITHUB_REF##*/}
- name: Push image
if: steps.changes.outputs.code_changed == 'true'
# ${GITHUB_REF##*/} gets branch name
run: docker push ${{ needs.infrastructure_common.outputs.artifact_registry_repository_prefix }}/certificate-generator:${GITHUB_REF##*/}
infrastructure_app:
name: Manage App Infrastructure
runs-on: ubuntu-latest
needs: [hasura_image, gotenberg_image, certificate_generator_image]
env:
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
TF_VAR_database_passwords: ${{ secrets.TF_VAR_DATABASE_PASSWORDS }}
TF_VAR_hasura_jwt_keys: ${{ secrets.TF_VAR_HASURA_JWT_KEYS }}
TF_VAR_hasura_passwords: ${{ secrets.TF_VAR_HASURA_PASSWORDS }}
TF_VAR_oauth_credentials: ${{ secrets.TF_VAR_OAUTH_CREDENTIALS }}
TF_VAR_edyoucated_username: ${{ secrets.TF_VAR_EDYOUCATED_USERNAME }}
TF_VAR_edyoucated_password: ${{ secrets.TF_VAR_EDYOUCATED_PASSWORD }}
TF_VAR_edyoucated_user_pool_id: ${{ secrets.TF_VAR_EDYOUCATED_USER_POOL_ID }}
TF_VAR_edyoucated_identity_pool_id: ${{ secrets.TF_VAR_EDYOUCATED_IDENTITY_POOL_ID }}
TF_VAR_edyoucated_client_id: ${{ secrets.TF_VAR_EDYOUCATED_CLIENT_ID }}
TF_VAR_edyoucated_aws_region: ${{ secrets.TF_VAR_EDYOUCATED_AWS_REGION }}
TF_VAR_edyoucated_api_url: ${{ secrets.TF_VAR_EDYOUCATED_API_URL }}
TF_VAR_edyoucated_organization_id: ${{ secrets.TF_VAR_EDYOUCATED_ORGANIZATION_ID }}
TF_VAR_trm_data_folder_id: ${{ secrets.TF_VAR_TRM_DATA_FOLDER_ID }}
TF_VAR_google_impersonate_subject: ${{ secrets.TF_VAR_GOOGLE_IMPERSONATE_SUBJECT }}
TF_VAR_project: techlabs-trm
TF_VAR_region: europe-west3
TF_VAR_domain: trm.techlabs.org
TF_VAR_gsuite_domain: techlabs.org
outputs:
hasura_url: ${{ steps.terraform_output.outputs.hasura_url }}
functions_auth_url: ${{ steps.terraform_output.outputs.functions_auth_url }}
defaults:
run:
working-directory: infrastructure/app
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Prepare function dependencies
run: |
cp -rp functions/lib/trm-api functions/typeform
cp -rp functions/lib/trm-api functions/form-response
cp -rp functions/lib/trm-api functions/activity-import
cp -rp functions/lib/trm-api functions/export
cp -rp functions/lib/trm-api functions/auth
sed -i -e 's/file:\.\.\/lib\/trm-api/file:\.\/trm-api/g' functions/*/package*.json
working-directory: .
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.13.5
terraform_wrapper: false
- name: terraform init
run: terraform init
- name: terraform workspace select
run: terraform workspace select ${GITHUB_REF##*/}
- name: terraform apply
run: terraform apply -auto-approve
- name: terraform output
id: terraform_output
run: |
echo "::set-output name=hasura_url::$(terraform output hasura_url)"
echo "::set-output name=functions_auth_url::$(terraform output functions_auth_url)"
- name: Upload frontend config
uses: actions/upload-artifact@v2
with:
name: frontend-config
path: infrastructure/app/output/config.*.js
if-no-files-found: error
frontend_build:
name: Build Frontend
runs-on: ubuntu-latest
needs: [infrastructure_app]
outputs:
changed: ${{ steps.changes.outputs.frontend }}
defaults:
run:
working-directory: frontend
steps:
- name: Checkout
uses: actions/checkout@v2
# TODO path filter needs to check if frontend config changed and take it into account
# - uses: dorny/paths-filter@e2bed85912bf87322673b976228bde722213a6df
# id: changes
# with:
# base: ${{ github.ref }}
# filters: |
# frontend:
# - 'frontend/**'
- name: Download frontend config
# if: steps.changes.outputs.frontend == 'true'
uses: actions/download-artifact@v2
with:
name: frontend-config
path: frontend/src
- name: Rename frontend config
# if: steps.changes.outputs.frontend == 'true'
run: mv src/config.${GITHUB_REF##*/}.js src/config.js
- uses: actions/setup-node@v2-beta
# if: steps.changes.outputs.frontend == 'true'
with:
node-version: '14'
- name: Get yarn cache directory path
# if: steps.changes.outputs.frontend == 'true'
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
# if: steps.changes.outputs.frontend == 'true'
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-v2-${{ hashFiles('frontend/**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-v2
- name: yarn install
# if: steps.changes.outputs.frontend == 'true'
run: yarn install
- name: yarn build
# if: steps.changes.outputs.frontend == 'true'
run: yarn build
- name: Upload frontend build
# if: steps.changes.outputs.frontend == 'true'
uses: actions/upload-artifact@v2
with:
name: frontend-build
path: frontend/build
if-no-files-found: error
frontend_deploy_staging:
name: Deploy Frontend (Staging)
runs-on: ubuntu-latest
needs: [frontend_build]
if: github.ref == 'refs/heads/staging' # && needs.frontend_build.outputs.changed == 'true'
steps:
- name: Get deploy repo
id: deploy-repo
run: echo "::set-output name=repository::TechLabs-Berlin/trm-frontend-${GITHUB_REF##*/}"
- name: Checkout deploy repo
uses: actions/checkout@v2
with:
repository: ${{ steps.deploy-repo.outputs.repository }}
path: deploy
ssh-key: ${{ secrets.DEPLOY_SSH_KEY_STAGING }}
- name: Download frontend build
uses: actions/download-artifact@v2
with:
name: frontend-build
path: build
- name: Copy frontend build to deploy repo
run: |
cp -r deploy/.git deploy/CNAME deploy/README.md build/
- name: Configure git
run: |
git config --global user.email "[email protected]"
git config --global user.name "TechLabs Berlin GitHub Workflow"
- name: Commit deploy repo
run: |
git add --all --force .
git commit -m "Add frontend build at $(date +%FT%T%Z)" --allow-empty
working-directory: build
- name: Push deploy repo
run: git push
working-directory: build
frontend_deploy_production:
name: Deploy Frontend (Production)
runs-on: ubuntu-latest
needs: [frontend_build]
if: github.ref == 'refs/heads/production' # && needs.frontend_build.outputs.changed == 'true'
steps:
- name: Get deploy repo
id: deploy-repo
run: echo "::set-output name=repository::TechLabs-Berlin/trm-frontend-${GITHUB_REF##*/}"
- name: Checkout deploy repo
uses: actions/checkout@v2
with:
repository: ${{ steps.deploy-repo.outputs.repository }}
path: deploy
ssh-key: ${{ secrets.DEPLOY_SSH_KEY_PRODUCTION }}
- name: Download frontend build
uses: actions/download-artifact@v2
with:
name: frontend-build
path: build
- name: Copy frontend build to deploy repo
run: |
cp -r deploy/.git deploy/CNAME deploy/README.md build/
- name: Configure git
run: |
git config --global user.email "[email protected]"
git config --global user.name "TechLabs Berlin GitHub Workflow"
- name: Commit deploy repo
run: |
git add --all --force .
git commit -m "Add frontend build at $(date +%FT%T%Z)" --allow-empty
working-directory: build
- name: Push deploy repo
run: git push
working-directory: build