-
Notifications
You must be signed in to change notification settings - Fork 0
317 lines (310 loc) · 14.2 KB
/
frontend_svelte.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
name: Frontend
on:
workflow_dispatch:
workflow_run:
workflows: [Infrastructure]
types: completed
branches:
- dev
- stage
- main
push:
paths:
- 'frontend_svelte/**'
- '.github/workflows/frontend_svelte.yml'
# pull_request:
# paths:
# - 'frontend/**'
env:
REGISTRY: ghcr.io
# COMMIT_SHA: ${{ github.event.after }}
COMMIT_SHA: ${{ github.sha }}
jobs:
test:
runs-on: ubuntu-24.04
environment: test
env:
KEYVAULT_HEALTH: ${{ vars.KEYVAULT_HEALTH }}
POSTGRES_DB: ${{ vars.POSTGRES_DB }}
POSTGRES_HOST: ${{ vars.POSTGRES_HOST }}
POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
REDIS_HOST: ${{ vars.REDIS_HOST }}
REDIS_PORT: ${{ vars.REDIS_PORT }}
MONGODB_HOST: ${{ vars.MONGODB_HOST }}
MONGODB_PORT: ${{ vars.MONGODB_PORT }}
APP_REG_CLIENT_ID: ${{ secrets.APP_REG_CLIENT_ID }}
APP_CLIENT_SECRET: ${{ secrets.APP_CLIENT_SECRET }}
AZ_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
API_SCOPE: ${{ secrets.API_SCOPE }}
steps:
- uses: actions/checkout@v4
- name: Build
run: |
docker compose \
-f compose.yml \
-f compose.override.test.yml \
build
- name: Spin up containers
run: |
docker compose \
-f compose.yml \
-f compose.override.test.yml \
up -d
# - name: run inside the container and show content
# run: |
# docker compose \
# -f compose.yml \
# -f compose.override.test.yml \
# run --rm frontend_svelte \
# sh -c "ls -la node_modules"
# - name: show image contents
# run: |
# docker compose \
# -f compose.yml \
# -f compose.override.test.yml \
# exec -T frontend_svelte \
# sh -c "ls -la node_modules"
# TBD change all commands to docker compose run - as the container is up and runnig already!
- name: Code Formating
run: |
docker compose \
-f compose.yml \
-f compose.override.test.yml \
exec -T frontend_svelte \
sh -c "npm run format"
- name: Linting
run: |
docker compose \
-f compose.yml \
-f compose.override.test.yml \
exec -T frontend_svelte \
sh -c "npm run lint"
- name: Unit testing
run: |
docker compose \
-f compose.yml \
-f compose.override.test.yml \
exec -T frontend_svelte \
sh -c "npm run test:unit"
- name: Stop containers
run: |
docker compose \
-f compose.yml \
-f compose.override.test.yml \
down
check_for_infrastructure_change:
runs-on: ubuntu-24.04
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check for infrastructure changes
id: check_for_infrastructure_change
# replace --exit-code with --quite for less output on console if necessary!
run: |
if git diff --exit-code HEAD^ HEAD -- infrastructure/ .github/workflows/infrastructure.yml; then
echo "No infrastructure changes detected."
echo "infrastructure_changed=FALSE" >> $GITHUB_OUTPUT
else
echo "Infrastructure changes detected."
echo "infrastructure_changed=TRUE" >> $GITHUB_OUTPUT
fi
echo "=== Contents of $GITHUB_OUTPUT ==="
cat $GITHUB_OUTPUT
outputs:
infrastructure_changed: ${{ steps.check_for_infrastructure_change.outputs.infrastructure_changed }}
containerize:
# if: ${{ github.event.ref == 'refs/heads/main' || github.event.ref == 'refs/heads/stage' }}
# only run on stage and main branch
# only run if no infrastructure changes were detected, but do run, if the workflow was triggered by a workflow_run event (then the github.event.workflow_run.head_sha is not null)
# TBD: split into multiline if statement:
if: ${{((github.event.ref == 'refs/heads/main' || github.event.ref == 'refs/heads/stage') && needs.check_for_infrastructure_change.outputs.infrastructure_changed == 'FALSE' ) || ( ( github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'stage' ) && github.event.workflow_run.name == 'Infrastructure' && github.event.workflow_run.conclusion == 'success') }}
needs: [test, check_for_infrastructure_change]
runs-on: ubuntu-24.04
permissions:
id-token: write
contents: read
packages: write
environment: stage
steps:
# - name: echos github.event.after variable
# run: |
# echo "=== Is this the correct tag: github.event.after? ==="
# echo ${{ github.event.after }}
# echo $CONTAINER_TAG
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build for production
run: |
docker compose -f compose.yml -f compose.prod.yml build --build-arg COMMIT_SHA=${{env.COMMIT_SHA}} frontend_svelte
- name: Show images
run: docker image list
- name: Tag and push with latest and commit hash
run: |
docker tag \
${{github.event.repository.name}}-frontend_svelte:latest \
${{env.REGISTRY}}/${{github.repository}}-frontend_svelte:latest
docker tag \
${{github.event.repository.name}}-frontend_svelte:latest \
${{env.REGISTRY}}/${{github.repository}}-frontend_svelte:$COMMIT_SHA
docker push \
${{env.REGISTRY}}/${{github.repository}}-frontend_svelte:latest
docker push \
${{env.REGISTRY}}/${{github.repository}}-frontend_svelte:$COMMIT_SHA
deploy_stage:
needs: containerize
# if: ${{ github.event.ref == 'refs/heads/main' || github.event.ref == 'refs/heads/stage' }}
# if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/stage' }}
if: ${{((github.event.ref == 'refs/heads/main' || github.event.ref == 'refs/heads/stage') && needs.check_for_infrastructure_change.outputs.infrastructure_changed == 'FALSE' ) || ( ( github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'stage' ) && github.event.workflow_run.name == 'Infrastructure' && github.event.workflow_run.conclusion == 'success') }}
runs-on: ubuntu-24.04
permissions:
id-token: write
packages: read
environment: stage
# env:
# AZURE_AUTHORITY: https://login.microsoftonline.com/${{ secrets.AZURE_TENANT_ID }}
steps:
# - name: echos github.event.after variable
# run: |
# echo "=== Is this the correct tag: github.event.after? ==="
# echo ${{ github.event.after }}
# echo $CONTAINER_TAG
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_GITHUBACTIONSMANAGEDIDENTITY_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Deploy to staging
# TBD: changing mode to single here, as there is a terraform bug with the time-out, when setting containerapp to single!
# env no longer necessary: all variables coming from terraform now!
# env:
# IDENTITY_REF: "/subscriptions/${{secrets.AZURE_SUBSCRIPTION_ID}}/resourcegroups/${{vars.AZURE_RESOURCE_GROUP}}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/${{vars.AZURE_FRONTENDIDENTITY_NAME}}"
run: |
az containerapp revision set-mode \
--name ${{ vars.AZURE_CONTAINERAPP_FRONTEND }} \
--resource-group ${{vars.AZURE_RESOURCE_GROUP}} \
--mode single
az containerapp update \
--name ${{ vars.AZURE_CONTAINERAPP_FRONTEND }} \
--resource-group ${{vars.AZURE_RESOURCE_GROUP}} \
--image ${{env.REGISTRY}}/${{github.repository}}-frontend_svelte:$COMMIT_SHA
# TBD: remove AZ_KEYVAULT_HOST here - coming from terraform now!
# REMOVED NOW: all env variables coming from terraform now!
# --set-env-vars \
# "AZ_KEYVAULT_HOST=${{ vars.AZURE_KEYVAULT_HOST }}"
# remember also to remove from Github environment!
# az containerapp revision set-mode \
# --name ${{ vars.AZURE_CONTAINERAPP_FRONTEND }} \
# --resource-group ${{vars.AZURE_RESOURCE_GROUP}} \
# --mode single
# az containerapp secret set \
# --name ${{ vars.AZURE_CONTAINERAPP_FRONTEND }} \
# --resource-group ${{vars.AZURE_RESOURCE_GROUP}} \
# --secrets \
# "keyvault-health=keyvaultref:${{ vars.AZURE_KEYVAULT_HOST }}/secrets/keyvault-health,identityref:$IDENTITY_REF" \
# "app-reg-client-id"=keyvaultref:${{ vars.AZURE_KEYVAULT_HOST }}/secrets/app-reg-client-id,identityref:$IDENTITY_REF \
# "azure-authority=${{ env.AZURE_AUTHORITY }}"
# sleep 10
# az containerapp update \
# --name ${{ vars.AZURE_CONTAINERAPP_FRONTEND }} \
# --resource-group ${{vars.AZURE_RESOURCE_GROUP}} \
# --image ${{env.REGISTRY}}/${{github.repository}}-frontend_svelte:$COMMIT_SHA \
# --set-env-vars \
# "AZURE_KEYVAULT_HOST=${{ vars.AZURE_KEYVAULT_HOST }}" \
# "KEYVAULT_HEALTH=secretref:keyvault-health" \
# "BACKEND_HOST=${{ vars.AZURE_CONTAINERAPP_BACKEND }}" \
# "APP_REG_CLIENT_ID=secretref:app-reg-client-id" \
# "AZURE_AUTHORITY=secretref:azure-authority"
# TBD: this is weird: here the variable is named AZ_KEYVAULT_HOST, but in the container it is named AZURE_KEYVAULT_HOST
# when renaming it here - conatinerapp fails to start with managed identity error - cannot get to keyvault!
# Set by terraform already in container: "app-reg-client-id=keyvaultref:${{ vars.AZURE_KEYVAULT_HOST }}/secrets/app-reg-client-id,identityref:$IDENTITY_REF" \
# TBD: consider deleting all existing environment variables before setting the new ones?
# No: don't otherwise the ones set from Terraform get deleted!
# implemented as in https://learn.microsoft.com/en-us/azure/container-apps/manage-secrets?tabs=azure-cli
# TBD: consider putting all of this into a deploymentscript and reuse it and in deploy_prod!
- name: Logout from Azure
uses: azure/CLI@v2
with:
inlineScript: |
az logout
az cache purge
az account clear
deploy_prod:
needs: [ deploy_stage ]
# if: ${{ github.event.ref == 'refs/heads/main' }}
if: ${{ github.event.ref == 'refs/heads/main' || github.event.workflow_run.head_branch == 'main' }}
# as this environmnet requires a manual review:
# also wait on the pull request from postgres migrations to be merged
# if check migrations detected changes.
runs-on: ubuntu-24.04
permissions:
id-token: write
packages: read
environment: prod
# env:
# AZURE_AUTHORITY: https://login.microsoftonline.com/${{ secrets.AZURE_TENANT_ID }}
steps:
# - name: echos github.event.after variable
# run: |
# echo "=== Is this the correct tag: github.event.after? ==="
# echo ${{ github.event.after }}
# echo $CONTAINER_TAG
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_GITHUBACTIONSMANAGEDIDENTITY_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Deploy to production
# TBD: changing mode to single here, as there is a terraform bug with the time-out, when setting containerapp to single!
# env no longer necessary: all variables coming from terraform now!
# env:
# IDENTITY_REF: "/subscriptions/${{secrets.AZURE_SUBSCRIPTION_ID}}/resourcegroups/${{vars.AZURE_RESOURCE_GROUP}}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/${{vars.AZURE_FRONTENDIDENTITY_NAME}}"
run: |
az containerapp revision set-mode \
--name ${{ vars.AZURE_CONTAINERAPP_FRONTEND }} \
--resource-group ${{vars.AZURE_RESOURCE_GROUP}} \
--mode single
az containerapp update \
--name ${{ vars.AZURE_CONTAINERAPP_FRONTEND }} \
--resource-group ${{vars.AZURE_RESOURCE_GROUP}} \
--image ${{env.REGISTRY}}/${{github.repository}}-frontend_svelte:$COMMIT_SHA
# TBD: remove AZ_KEYVAULT_HOST here - coming from terraform now!
# REMOVED NOW: all env variables coming from terraform now!
# --set-env-vars \
# "AZ_KEYVAULT_HOST=${{ vars.AZURE_KEYVAULT_HOST }}"
# remember also to remove from Github environment!
# Set by terraform already in container: "app-reg-client-id=keyvaultref:${{ vars.AZURE_KEYVAULT_HOST }}/secrets/app-reg-client-id,identityref:$IDENTITY_REF" \
# TBD: consider deleting all existing environment variables before setting the new ones?
# No: don't otherwise the ones set from Terraform get deleted!
# implemented as in https://learn.microsoft.com/en-us/azure/container-apps/manage-secrets?tabs=azure-cli
# TBD: consider putting all of this into a deploymentscript and reuse it and in deploy_prod!
- name: Logout from Azure
uses: azure/CLI@v2
with:
inlineScript: |
az logout
az cache purge
az account clear