-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathazure-pipelines.yml
402 lines (369 loc) · 14.9 KB
/
azure-pipelines.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
400
401
402
# self-hosted agent "mydockeragent" in pool "Default".
# Requires permission approval first time to run
#################### important #######################
# set secret env vars for prod,
# e.g. REACT_APP_REMOTE_SERVER_URL in azure devops
# containerRegistry is the verbatim name given to the Azure DevOps "Service Connection"
# TODOs
# - cache node modules and venv
# https://stackoverflow.com/questions/65958925/caching-node-modules-in-azure-pipelines-takes-longer-than-installing-them
parameters:
- name: SelfHostedAgent
type: boolean
default: false
trigger:
branches:
include:
- main
resources:
- repo: self
#! these are the actual environment variables that will be set
#! env vars in docker compose tasks and the docker-compose itself are useless
#! and will override them, so ONLY set them here:
#! regardless, if our code has to read a .env.* file, a file must be there with duplicated values
#! else we get <Config '{key}' is missing, and has no default.>
variables:
# for docker-compose instead of .env file read
# FRONTEND_PORT_CI: 80
# SERVER_PORT_CI: 8002
# DB_PORT_CI: 5633
# # backend
# UNIQUE_KEY: supersecret
# POSTGRES_USER: postgres
# POSTGRES_PASSWORD: postgres
# POSTGRES_SERVER_CI: db_ci
# POSTGRES_PORT: 5432
# POSTGRES_DB: postgres
# ACCESS_TOKEN_EXPIRE_MINUTES: 9450
# # frontend
# FRONTEND_URL: https://myapp.testing.localhost
# REMOTE_SERVER_URL_STAGING: https://myapp-backend.testing.localhost/api
System.Debug: true # see debug logs for every pipeline
# tag: "$(Build.BuildNumber)" # incremental build id as image tag
# DOCKER_HUB_FRONTEND: danicc097/fastapi-react-postgres-docker-traefik-template-frontend
# DOCKER_HUB_BACKEND: danicc097/fastapi-react-postgres-docker-traefik-template-backend
TRAEFIK_CERTIFICATES_DIR: $(Build.SourcesDirectory)/traefik/certificates/
stages:
# - stage: check_env_variables
# displayName: Check secrets and variables
# jobs:
# - job:
# ${{ if eq(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Default
# demands:
# - agent.name -equals mydockeragent
# ${{ if ne(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Azure Pipelines
# vmImage: ubuntu-20.04
# steps:
# - bash: |
# if [ -z "$REACT_APP_REMOTE_SERVER_URL" ]; then
# echo "REACT_APP_REMOTE_SERVER_URL is empty"
# exit 1
# fi
# displayName: Check env vars are set
# # map secret and regular env vars defined in azure devops
# env:
# # the recommended way to map to a secret env variable
# REACT_APP_REMOTE_SERVER_URL: $(REACT_APP_REMOTE_SERVER_URL)
# Don't build staging images directly inside compose.ci in testing stage so that we
# can ensure images will work as expected when we build them in push_production stage
# after merging
# - stage: push_staging
# condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
# displayName: Build and push staging images to Registry
# dependsOn: check_env_variables
# jobs:
# - job: build_push_backend
# cancelTimeoutInMinutes: 30
# displayName: Build and push backend
# ${{ if eq(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Default
# demands:
# - agent.name -equals mydockeragent
# ${{ if ne(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Azure Pipelines
# vmImage: ubuntu-20.04
# steps:
# - task: Docker@2
# displayName: Build an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_BACKEND)
# command: build
# dockerfile: "$(Build.SourcesDirectory)/backend/Dockerfile" # dont use prod Dockerfile (we need dev deps)
# tags: |
# staging
# - task: Docker@2
# displayName: Push an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_BACKEND)
# command: push
# tags: |
# staging
# - job: build_push_frontend
# cancelTimeoutInMinutes: 30
# displayName: Build and push frontend
# ${{ if eq(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Default
# demands:
# - agent.name -equals mydockeragent
# ${{ if ne(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Azure Pipelines
# vmImage: ubuntu-20.04
# steps:
# - task: Docker@2
# displayName: Build an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_FRONTEND)
# command: build
# arguments: "--build-arg REACT_APP_BUILD_NUMBER=$(tag) --build-arg
# REACT_APP_REMOTE_SERVER_URL=$(REMOTE_SERVER_URL_STAGING)"
# dockerfile: "$(Build.SourcesDirectory)/frontend/Dockerfile.prod"
# tags: |
# staging
# - task: Docker@2
# displayName: Push an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_FRONTEND)
# command: push
# tags: |
# staging
- stage: test
condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
displayName: Compose up and test
jobs:
- job: compose_up_and_test
displayName: Bring up compose services and run tests
${{ if eq(parameters['SelfHostedAgent'], 'true' ) }}:
pool:
name: Default
demands:
- agent.name -equals mydockeragent
${{ if ne(parameters['SelfHostedAgent'], 'true' ) }}:
pool:
name: Azure Pipelines
vmImage: ubuntu-20.04
steps:
- bash: |
docker network create traefik-net
displayName: Create external network
- bash: |
mkdir -p $TRAEFIK_CERTIFICATES_DIR
displayName: Create certificates directory
env:
# the recommended way to map to a secret env variable
TRAEFIK_CERTIFICATES_DIR: $(TRAEFIK_CERTIFICATES_DIR)
- task: Bash@3
displayName: mkcert for Traefik
inputs:
targetType: inline
script: |
wget --no-verbose -O mkcert https://github.com/FiloSottile/mkcert/releases/download/v1.4.3/mkcert-v1.4.3-linux-amd64
chmod +x ./mkcert
sudo apt-get install libnss3-tools
./mkcert -install
./mkcert --cert-file localhost.pem --key-file localhost-key.pem \
"app.localhost" "*.app.localhost" \
"e2e.localhost" "*.e2e.localhost" \
"dev.localhost" "*.dev.localhost" \
"prod.localhost" "*.prod.localhost" \
workingDirectory: "$(TRAEFIK_CERTIFICATES_DIR)"
failOnStderr: false
# noProfile: true
- task: DockerCompose@0
displayName: Traefik compose up
inputs:
dockerComposeFile: "**/traefik/docker-compose.yml"
action: "Run services"
buildImages: true
abortOnContainerExit: true
- task: Bash@3
displayName: Tooling installation
inputs:
targetType: inline
script: |
sudo bash backend/docs/sudo_install_tooling.sh && \
bash backend/docs/install_sqlc_custom.sh && \
pip install pydantic-to-typescript alembic && \
sudo apt-get install -y npm && sudo npm install -g pnpm
workingDirectory: "$(Build.SourcesDirectory)"
failOnStderr: false
- task: Bash@3
displayName: Frontend package installation
inputs:
targetType: inline
script: |
cd frontend && pnpm i
workingDirectory: "$(Build.SourcesDirectory)"
failOnStderr: false
- task: Bash@3
displayName: E2E package installation
inputs:
targetType: inline
script: |
cd e2e && npm install
workingDirectory: "$(Build.SourcesDirectory)"
failOnStderr: false
- task: Bash@3
displayName: Build with dcompose
timeoutInMinutes: 10
inputs:
targetType: inline
script: |
bin/run-e2e-tests ci
workingDirectory: "$(Build.SourcesDirectory)"
failOnStderr: false
# - bash: |
# docker exec backend_myapp_ci /bin/bash -c "
# printf '
# \n FRONTEND_PORT_CI=$FRONTEND_PORT_CI
# \n SERVER_PORT_CI=$SERVER_PORT_CI
# \n DB_PORT_CI=$DB_PORT_CI
# \n POSTGRES_SERVER_CI=$POSTGRES_SERVER_CI
# \n UNIQUE_KEY=$UNIQUE_KEY
# \n POSTGRES_USER=$POSTGRES_USER
# \n POSTGRES_PASSWORD=$POSTGRES_PASSWORD
# \n POSTGRES_PORT=$POSTGRES_PORT
# \n POSTGRES_DB=$POSTGRES_DB
# \n ACCESS_TOKEN_EXPIRE_MINUTES=$ACCESS_TOKEN_EXPIRE_MINUTES
# '"
# displayName: Print backend env vars
# # requires downgrading to base first else it cant upgrade
# # (maybe az reuses the same pool or something, who knows)
# - bash: |
# docker exec backend_myapp_ci /bin/bash -c "alembic downgrade base"
# docker exec backend_myapp_ci /bin/bash -c "alembic upgrade head"
# displayName: Migrate backend schema
# - bash: |
# docker exec backend_myapp_ci /bin/bash -c "python3 -m initial_data.e2e"
# displayName: Create initial data for E2E
# - bash: |
# docker exec backend_myapp_ci /bin/bash -c "pytest -n auto --dist loadscope"
# displayName: Run backend tests
# - bash: |
# until [ "$(curl -s -o /dev/null -w "%{http_code}" "https://myapp.testing.localhost")" == "200" ] \
# && [ "$(curl -s -o /dev/null -w "%{http_code}" "https://myapp-backend.testing.localhost/docs")" == "200" ]; do
# echo 'retrying...'
# sleep 5
# done
# displayName: Wait for 200 status from frontend and backend
# continueOnError: true
# timeoutInMinutes: 1
# E2E tests
# References for more efficient local dev setup sharing volumes
# https://jdlm.info/articles/2020/05/24/testing-node-docker-compose-end-to-end.html
# - bash: |
# npm install
# npm run test:ci
# workingDirectory: "$(Build.SourcesDirectory)/e2e"
# displayName: Run E2E tests
# - bash: |
# if docker exec puppeteer_ci /bin/sh -c 'npm run test:ci ; exit $?'; then
# printf '\nE2E tests passed\n'
# exit 0
# else
# printf '\nE2E tests failed\n'
# exit 1
# fi
# displayName: Run E2E tests
# # not possible to build and then push even with the same agent in MS hosted pipeline
# - stage: push_production
# # do not push to registry on PR checks
# dependsOn: check_env_variables
# condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
# displayName: Build and push production images to Registry
# jobs:
# - job: build_push_backend
# cancelTimeoutInMinutes: 30
# displayName: Build and push backend
# ${{ if eq(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Default
# demands:
# - agent.name -equals mydockeragent
# ${{ if ne(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Azure Pipelines
# vmImage: ubuntu-20.04
# steps:
# - task: Docker@2
# displayName: Build an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_BACKEND)
# command: build
# dockerfile: "$(Build.SourcesDirectory)/backend/Dockerfile.prod" # excludes devDeps
# tags: |
# $(tag)
# latest
# - task: Docker@2
# displayName: Push an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_BACKEND)
# command: push
# tags: |
# $(tag)
# latest
# - job: build_push_frontend
# cancelTimeoutInMinutes: 30
# displayName: Build and push frontend
# ${{ if eq(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Default
# demands:
# - agent.name -equals mydockeragent
# ${{ if ne(parameters['SelfHostedAgent'], 'true' ) }}:
# pool:
# name: Azure Pipelines
# vmImage: ubuntu-20.04
# steps:
# - task: Docker@2
# displayName: Build an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_FRONTEND)
# command: build
# arguments: "--build-arg REACT_APP_BUILD_NUMBER=$(tag) --build-arg
# REACT_APP_REMOTE_SERVER_URL=$(REACT_APP_REMOTE_SERVER_URL)"
# dockerfile: "$(Build.SourcesDirectory)/frontend/Dockerfile.prod"
# tags: |
# $(tag)
# latest
# - task: Docker@2
# displayName: Push an image
# inputs:
# containerRegistry: "Docker Hub"
# repository: $(DOCKER_HUB_FRONTEND)
# command: push
# tags: |
# $(tag)
# latest
#######################################################
######## # # # # # # UTILITIES # # # # # # # # ########
#######################################################
#* check what files are available in remote server
#* if we just need to see the git checkout we just set the debug variable above
# - bash: |
# SEARCH_PATH=/home/vsts/work # or any colon-delimited list of paths
# IFS=':' read -r -a PathDirs <<< "$SEARCH_PATH"
# echo "##[debug] Found directories"
# for element in "${PathDirs[@]}"; do
# echo "$element"
# done;
# echo;
# echo;
# echo "##[debug] Found files"
# for element in "${PathDirs[@]}"; do
# find "$element" -type f
# done