-
Notifications
You must be signed in to change notification settings - Fork 0
447 lines (401 loc) · 15.5 KB
/
build.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
name: Application Build
on:
push:
branches:
- main
pull_request:
branches:
- main
env:
APPLICATION_VERSION: 0.1.${{ github.run_number }}
TF_IN_AUTOMATION: true
TF_VAR_application_name: ${{ github.event.repository.name }}
TF_VAR_operations_assume_role: ${{ secrets.OPERATIONS_ROLE_TO_ASSUME }}
jobs:
setup-ecr:
name: Setup ECR Repository
runs-on: ubuntu-latest
defaults:
run:
working-directory: deployment/ecr
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/[email protected]
- name: Setup Terraform
uses: hashicorp/[email protected]
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: Terraform Format
id: fmt
run: terraform fmt -check
- name: Terraform Init
id: init
run: terraform init -input=false
- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: terraform plan -no-color -input=false
continue-on-error: true
- uses: actions/github-script@v5
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
script: |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outputs.stdout }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve -input=false
- name: Get ECR Repository URL
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
id: ecr-repository-url
run: terraform output -no-color -raw repository_url
- name: Write ECR repository URL to artifact
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
id: ecr-artifact
run: echo "${{ steps.ecr-repository-url.outputs.stdout }}" > ${{ github.workspace }}/ecr-url.txt
- name: Upload ecr repository url
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
uses: actions/[email protected]
with:
name: ecr
path: ${{ github.workspace }}/ecr-url.txt
if-no-files-found: error
test:
name: Run Tests and Checks
runs-on: ubuntu-latest
env:
NODE_ENV: test
steps:
- uses: actions/[email protected]
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '16.x'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run Tests
run: npm test
- name: Upload file report files as an artifact
if: success()
uses: actions/[email protected]
with:
name: reports
path: reports/
retention-days: 1
build:
needs:
- setup-ecr
- test
name: Build artefacts and container
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: Download all artifacts
uses: actions/[email protected]
- name: Read ECR file
id: ecr
run: echo ::set-output name=url::$(cat ${{ github.workspace }}/ecr/ecr-url.txt)
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '16.x'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run build
- name: Build docker container
run: docker build . -t ${{ steps.ecr.outputs.url }}:${{ env.APPLICATION_VERSION }}
- name: Configure AWS credentials
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
uses: aws-actions/[email protected]
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
role-to-assume: ${{ secrets.OPERATIONS_ROLE_TO_ASSUME }}
role-skip-session-tagging: true
role-duration-seconds: 900
- name: Login to Amazon ECR
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
id: login-ecr
uses: aws-actions/[email protected]
- name: Push docker image
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: docker push ${{ steps.ecr.outputs.url }}:${{ env.APPLICATION_VERSION }}
sonarcloud:
needs:
- test
name: Quality Gate
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
with:
fetch-depth: 0
- name: Download a reports artifact
uses: actions/[email protected]
with:
name: reports
path: reports
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
deploy-development:
needs:
- setup-ecr
- build
name: "Deploy development infrastructure and ECS service"
runs-on: ubuntu-latest
env:
TF_WORKSPACE: ${{ github.event.repository.name }}-development
defaults:
run:
working-directory: deployment/service
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Download all artifacts
uses: actions/[email protected]
- name: Read ECR file
id: ecr
run: echo ::set-output name=url::$(cat ${{ github.workspace }}/ecr/ecr-url.txt)
working-directory: ${{ github.workspace }}
- name: Checkout
uses: actions/[email protected]
- name: Setup Terraform
uses: hashicorp/[email protected]
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: Terraform Format
id: fmt
run: terraform fmt -check
- name: Terraform Init
id: init
run: terraform init -input=false
- name: Setup Terraform variables
id: vars
run: |-
echo "TF_VAR_container_image=${{ steps.ecr.outputs.url }}:${{ env.APPLICATION_VERSION }}" >> $GITHUB_ENV
- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: terraform plan -no-color -input=false
continue-on-error: true
- uses: actions/github-script@v5
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
script: |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outputs.stdout }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve -input=false
- name: Get terraform outputs
id: terraform
run: |
echo "::set-output name=service-name::$(terraform-bin output -no-color -raw service_name)"
echo "::set-output name=ecs-cluster-name::$(terraform-bin output -no-color -raw ecs_cluster_name)"
echo "::set-output name=codedeploy-application-name::$(terraform-bin output -no-color -raw codedeploy_application_name)"
echo "::set-output name=codedeploy-group-name::$(terraform-bin output -no-color -raw codedeploy_deployment_group)"
echo "::set-output name=task-definition::$(terraform-bin output -no-color -raw task_definition_file)"
echo "::set-output name=appspec-file::$(terraform-bin output -no-color -raw appspec_file)"
- name: Write CodeDeploy files
run: |
echo '${{ steps.terraform.outputs.task-definition }}' > ${{ github.workspace }}/task-definition.json
echo '${{ steps.terraform.outputs.appspec-file }}' > ${{ github.workspace }}/appspec.json
- name: Configure AWS credentials
uses: aws-actions/[email protected]
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
role-to-assume: ${{ secrets.DEVELOPMENT_ROLE_TO_ASSUME }}
role-skip-session-tagging: true
role-duration-seconds: 1200
- name: Deploy to Development ECS
uses: aws-actions/[email protected]
with:
task-definition: task-definition.json
service: ${{ steps.terraform.outputs.service-name }}
cluster: ${{ steps.terraform.outputs.ecs-cluster-name }}
wait-for-service-stability: true
codedeploy-appspec: appspec.json
codedeploy-application: ${{ steps.terraform.outputs.codedeploy-application-name }}
codedeploy-deployment-group: ${{ steps.terraform.outputs.codedeploy-group-name }}
# deploy-production:
# needs:
# - deploy-development
# name: "Deploy production infrastructure and ECS service"
# runs-on: ubuntu-latest
# env:
# TF_WORKSPACE: ${{ github.event.repository.name }}-production
# defaults:
# run:
# working-directory: deployment/service
# if: ${{ false }}
#
# steps:
# - name: Download all artifacts
# uses: actions/[email protected]
#
# - name: Read ECR file
# id: ecr
# run: echo ::set-output name=url::$(cat ${{ github.workspace }}/ecr/ecr-url.txt)
# working-directory: ${{ github.workspace }}
#
# - name: Checkout
# uses: actions/[email protected]
#
# - name: Setup Terraform
# uses: hashicorp/[email protected]
# with:
# cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
#
# - name: Terraform Format
# id: fmt
# run: terraform fmt -check
#
# - name: Terraform Init
# id: init
# run: terraform init -input=false
#
# - name: Setup Terraform variables
# id: vars
# run: |-
# echo "TF_VAR_container_image=${{ steps.ecr.outputs.url }}:${{ env.APPLICATION_VERSION }}" >> $GITHUB_ENV
#
# - name: Terraform Plan
# id: plan
# if: github.event_name == 'pull_request'
# run: terraform plan -no-color -input=false
# continue-on-error: true
#
# - uses: actions/github-script@v5
# if: github.event_name == 'pull_request'
# env:
# PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
# with:
# script: |
# const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
# #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
# #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
# #### Terraform Validation 🤖\`${{ steps.validate.outputs.stdout }}\`
# <details><summary>Show Plan</summary>
# \`\`\`\n
# ${process.env.PLAN}
# \`\`\`
# </details>
# *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
# github.issues.createComment({
# issue_number: context.issue.number,
# owner: context.repo.owner,
# repo: context.repo.repo,
# body: output
# })
# - name: Terraform Plan Status
# if: steps.plan.outcome == 'failure'
# run: exit 1
#
# - name: Terraform Apply
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
# run: terraform apply -auto-approve -input=false
#
# - name: Get terraform outputs
# id: terraform
# run: |
# echo "::set-output name=service-name::$(terraform-bin output -no-color -raw service_name)"
# echo "::set-output name=ecs-cluster-name::$(terraform-bin output -no-color -raw ecs_cluster_name)"
# echo "::set-output name=codedeploy-application-name::$(terraform-bin output -no-color -raw codedeploy_application_name)"
# echo "::set-output name=codedeploy-group-name::$(terraform-bin output -no-color -raw codedeploy_deployment_group)"
# echo "::set-output name=task-definition::$(terraform-bin output -no-color -raw task_definition_file)"
# echo "::set-output name=appspec-file::$(terraform-bin output -no-color -raw appspec_file)"
#
# - name: Write CodeDeploy files
# run: |
# echo '${{ steps.terraform.outputs.task-definition }}' > ${{ github.workspace }}/task-definition.json
# echo '${{ steps.terraform.outputs.appspec-file }}' > ${{ github.workspace }}/appspec.json
# - name: Configure AWS credentials
# uses: aws-actions/[email protected]
# with:
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# aws-region: us-east-1
# role-to-assume: ${{ secrets.DEVELOPMENT_ROLE_TO_ASSUME }}
# role-skip-session-tagging: true
# role-duration-seconds: 1200
#
# - name: Deploy to Development ECS
# uses: aws-actions/[email protected]
# with:
# task-definition: task-definition.json
# service: ${{ steps.terraform.outputs.service-name }}
# cluster: ${{ steps.terraform.outputs.ecs-cluster-name }}
# wait-for-service-stability: true
# codedeploy-appspec: appspec.json
# codedeploy-application: ${{ steps.terraform.outputs.codedeploy-application-name }}
# codedeploy-deployment-group: ${{ steps.terraform.outputs.codedeploy-group-name }}
#
# slack-workflow-status:
# if: always()
# name: Post Workflow Status To Slack
# runs-on: ubuntu-latest
# needs:
# - setup-ecr
# - test
# - build
# - sonarcloud
# - deploy-development
# steps:
# - name: Workflow Conclusion Action
# uses: technote-space/[email protected]
#
# - name: Slack Notification
# uses: 8398a7/[email protected]
# with:
# status: ${{ env.WORKFLOW_CONCLUSION }}
# author_name: GitHub Action
# fields: repo,commit,message,author,action,workflow
# mention: channel
# if_mention: failure,cancelled
# env:
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}