-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.yml
476 lines (439 loc) · 16.1 KB
/
config.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
version: 2.1
orbs:
aws-cli: circleci/[email protected]
commands:
revert-migrations:
description: Revert the last migration
parameters:
workflow_id:
type: string
default: ${CIRCLE_WORKFLOW_ID:0:7}
steps:
- run:
name: Revert migrations
when: on_fail
command: |
# Example: Memstash.io
SUCCESS=$(curl --insecure https://kvdb.io/LPxKGYgg6GmHRqNc9vNwNC/migration_<< parameters.workflow_id >>)
# Logic for reverting the database state
if (( $SUCCESS == 1 ));
then
cd ~/project/backend
npm install --legacy-peer-deps
npm run migrations:revert
fi
jobs:
build-frontend:
docker:
- image: circleci/node:13.8.0
steps:
- checkout
- restore_cache:
keys: [frontend-build]
- run:
name: Build front-end
command: |
cd frontend
npm install
npm run build
- save_cache:
paths: [frontend/node_modules]
key: frontend-build
build-backend:
docker:
- image: circleci/node:13.8.0
steps:
- checkout
- restore_cache:
keys: [backend-build]
- run:
name: Back-end build
command: |
cd backend
npm install
npm run build
- save_cache:
paths: [backend/node_modules]
key: backend-build
test-frontend:
docker:
- image: circleci/node:13.8.0
steps:
- checkout
- restore_cache:
keys: [frontend-build]
- run:
name: test front-end
command: |
cd frontend
npm install
npm run test
test-backend:
docker:
- image: circleci/node:13.8.0
steps:
- checkout
- restore_cache:
keys: [backend-build]
- run:
name: test back-end
command: |
cd backend
npm install
npm run test
scan-frontend:
docker:
- image: circleci/node:13.8.0
steps:
- checkout
- restore_cache:
keys: [frontend-build]
- run:
name: scan front-end
command: |
cd frontend
npm install
npm audit fix
npm audit fix --audit-level=critical --force
npm audit --audit-level=critical
scan-backend:
docker:
- image: circleci/node:13.8.0
steps:
- checkout
- restore_cache:
keys: [backend-build]
- run:
name: scan back-end
command: |
cd backend
npm install
npm audit fix
npm audit fix --audit-level=critical --force
npm audit --audit-level=critical
deploy-initial-cloudfront-infrastructure:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- run:
name: Ensure initial infrastructure exists
command: |
aws cloudformation deploy --template-file .circleci/cloudformation/cloudfront.yml \
--stack-name InitialStack \
--parameter-overrides WorkflowID=mide1994
deploy-infrastructure:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- run:
name: Ensure back-end infrastructure exists
command: |
aws cloudformation deploy \
--template-file .circleci/cloudformation/backend.yml \
--stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \
--parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" \
--tags project=udapeople
- run:
name: Ensure front-end infrastructure exist
command: |
aws cloudformation deploy \
--template-file .circleci/cloudformation/frontend.yml \
--stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" \
--parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}" \
--tags project=udapeople
- run:
name: Add back-end ip to ansible inventory
command: |
echo Y | sudo apt-get install jq
sudo cat .circleci/ansible/inventory.txt
echo $(aws cloudformation describe-stacks --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} | sudo jq .Stacks[0].Outputs[1].OutputValue | tr -d '"') >> .circleci/ansible/inventory.txt
sudo cat .circleci/ansible/inventory.txt
- persist_to_workspace:
root: ~/
paths:
- project/.circleci/ansible/inventory.txt
- run:
name: destroy-environment on fail
when: on_fail
command: |
aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name InitialStack
configure-infrastructure:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- add_ssh_keys:
fingerprints: ["37:15:79:e4:1b:ca:d8:53:70:11:2d:75:48:76:f6:4c"]
- attach_workspace:
at: ~/
- run:
name: Install dependencies
command: |
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
- run:
name: Configure server
command: |
export ANSIBLE_HOST_KEY_CHECKING=False
export RECORD_HOST_KEYS=False
export ANSIBLE_PARAMIKO_BANNER_TIMEOUT=30
echo ENVIROMENT=production > "backend/.env"
echo TYPEORM_CONNECTION=postgres >> "backend/.env"
echo TYPEORM_MIGRATIONS_DIR=./src/migrations >> "backend/.env"
echo TYPEORM_ENTITIES=./src/modules/domain/**/*.entity.ts >> "backend/.env"
echo TYPEORM_PORT=$TYPEORM_PORT >> "backend/.env"
echo TYPEORM_USERNAME=$TYPEORM_USERNAME >> "backend/.env"
echo TYPEORM_PASSWORD=$TYPEORM_PASSWORD >> "backend/.env"
echo TYPEORM_DATABASE=$TYPEORM_DATABASE >> "backend/.env"
echo "CONTENT OF backend/.env"
cat backend/.env
sudo cat ~/project/.circleci/ansible/inventory.txt
ansible-playbook -i ~/project/.circleci/ansible/inventory.txt ~/project/.circleci/ansible/configure-server.yml
- run:
name: destroy-environment on fail
when: on_fail
command: |
aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name InitialStack
run-migrations:
docker:
- image: cimg/node:13.8.0
steps:
- checkout
- run:
name: Run migrations
command: |
cd backend
npm install
npm run migrations > migrations_dump.txt
- run:
name: Send migration results to memstash
command: |
cat ~/project/backend/migrations_dump.txt
if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt
then
curl https://kvdb.io/LPxKGYgg6GmHRqNc9vNwNC/migration_${CIRCLE_WORKFLOW_ID:0:7} -d '1'
fi
- revert-migrations
deploy-frontend:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- attach_workspace:
at: ~/
- run:
name: Install dependencies
command: |
sudo apt install curl
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
nodejs --version
- run:
name: Get backend url
command: |
echo Y | sudo apt-get install jq
export BACKEND_IP=$(aws cloudformation describe-stacks --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} | sudo jq .Stacks[0].Outputs[1].OutputValue | tr -d '"')
export API_URL="http://${BACKEND_IP}:3030"
echo "API_URL = ${API_URL}"
echo API_URL="http://${BACKEND_IP}:3030" >> frontend/.env
echo "NODE_ENV=production" >> frontend/.env
sudo cat frontend/.env
- persist_to_workspace:
root: ~/
paths:
- project/frontend/.env
- run:
name: Deploy frontend objects
command: |
export NODE_OPTIONS='--openssl-legacy-provider'
cd frontend
npm install
npm run build
tar -czvf artifact-"${CIRCLE_WORKFLOW_ID:0:7}".tar.gz dist
aws s3 cp dist s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7} --recursive
- run:
name: destroy-environment on fail
when: on_fail
command: |
aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name InitialStack
- revert-migrations
deploy-backend:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- add_ssh_keys:
fingerprints: ["37:15:79:e4:1b:ca:d8:53:70:11:2d:75:48:76:f6:4c"]
- attach_workspace:
at: ~/
- run:
name: Install dependencies
command: |
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
- run:
name: Deploy backend
command: |
cd backend
npm i --legacy-peer-deps
npm run build
cd ..
tar -C backend -czvf artifact.tar.gz .
mkdir -p ~/project/.circleci/ansible/roles/deploy/files/
mv artifact.tar.gz .circleci/ansible/roles/deploy/files/artifact.tar.gz
cd .circleci/ansible
echo "Contents of the inventory.txt file is -------"
cat inventory.txt
ansible-playbook -i inventory.txt deploy-backend.yml
- run:
name: destroy-environment on fail
when: on_fail
command: |
aws --region us-west-2 s3 rm "s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7}" --recursive
aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name InitialStack
- revert-migrations
smoke-test:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- run:
name: Backend smoke test.
command: |
export BACKEND_url=$(aws cloudformation describe-stacks --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7} | sudo jq .Stacks[0].Outputs[0].OutputValue | tr -d '"')
export API_URL="http://${BACKEND_url}:3030"
echo "API_URL = ${API_URL}"
if curl -s ${API_URL}/api/status | grep "ok"
then
exit 0
else
exit 1
fi
- run:
name: Frontend smoke test.
command: |
URL="http://udapeople-${CIRCLE_WORKFLOW_ID:0:7}.s3-website-us-east-1.amazonaws.com/#/employees"
echo ${URL}
if curl -s ${URL} | grep "Welcome"
then
exit 0
else
exit 1
fi
- run:
name: destroy-environment on fail
when: on_fail
command: |
aws --region us-east-1 s3 rm "s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7}" --recursive
aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name InitialStack
- revert-migrations
cloudfront-update:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- run:
name: Save Old Workflow ID to kvdb.io
command: |
export OldWorkflowID=c9bb430
echo "Old Wokflow ID: $OldWorkflowID"
curl https://kvdb.io/LPxKGYgg6GmHRqNc9vNwNC/old_workflow_id -d "${OldWorkflowID}"
- run:
name: Update cloudfront distribution
command: |
aws cloudformation deploy \
--template-file .circleci/cloudformation/cloudfront.yml \
--stack-name InitialStack \
--parameter-overrides WorkflowID="${CIRCLE_WORKFLOW_ID:0:7}" \
--region us-east-1\
- revert-migrations
- run:
name: destroy-environment on fail
when: on_fail
command: |
aws --region us-east-1 s3 rm "s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7}" --recursive
aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name InitialStack
cleanup:
executor: aws-cli/default
steps:
- checkout
- aws-cli/setup:
profile-name: udacity
- run:
name: Get old stack workflow id and Cleanup
command: |
export OldWorkflowID=$(curl --insecure https://kvdb.io/LPxKGYgg6GmHRqNc9vNwNC/old_workflow_id)
export STACKS=($(aws --region us-east-1 cloudformation list-stacks --query "StackSummaries[*].StackName" --stack-status-filter CREATE_COMPLETE --no-paginate --output text))
echo OldWorkflowID: "${OldWorkflowID}"
echo CIRCLE_WORKFLOW_ID "$CIRCLE_WORKFLOW_ID"
echo Stack names: "${STACKS[@]}"
if [[ ${STACKS[@]} =~ "${OldWorkflowID}" ]]
then
aws --region us-east-1 s3 rm "s3://udapeople-${OldWorkflowID}" --recursive
aws --region us-east-1 cloudformation delete-stack --stack-name "udapeople-backend-${OldWorkflowID}"
aws --region us-east-1 cloudformation delete-stack --stack-name "udapeople-frontend-${OldWorkflowID}"
echo "=======================Delete Confirmed========================"
else
echo "=======================Didn't delete========================"
fi
workflows:
default:
jobs:
- build-frontend
- build-backend
- test-frontend:
requires: [build-frontend]
- test-backend:
requires: [build-backend]
- scan-backend:
requires: [build-backend]
- scan-frontend:
requires: [build-frontend]
- deploy-initial-cloudfront-infrastructure:
filters:
branches:
only: [main]
- deploy-infrastructure:
requires: [test-frontend, test-backend, scan-frontend, scan-backend]
filters:
branches:
only: [main]
- configure-infrastructure:
requires: [deploy-infrastructure]
- run-migrations:
requires: [configure-infrastructure]
- deploy-frontend:
requires: [run-migrations]
- deploy-backend:
requires: [run-migrations]
- smoke-test:
requires: [deploy-backend, deploy-frontend]
- cloudfront-update:
requires: [smoke-test]
- cleanup:
requires: [cloudfront-update]