-
Notifications
You must be signed in to change notification settings - Fork 0
310 lines (272 loc) · 10.4 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
name: Deploy
on:
workflow_dispatch:
push:
branches: [dev, stg, prd]
paths-ignore:
- 'README.md'
- 'LICENSE'
- '.gitignore'
env:
node_version: "18.x"
tf_version: "1.7.0" # must match value in terraform-iac/*/app/main.tf
FORCE_COLOR: 3
concurrency: ${{ github.ref }}
jobs:
env:
name: Set Env Vars
timeout-minutes: 1
runs-on: ubuntu-latest
steps:
- name: Set up DEV Environment Variables
if: github.ref == 'refs/heads/dev'
run: |
matrix='{
"env":[
{
"environment_name":"dev",
"ecr_repo_name":"hw-fargate-api-dev",
"tf_working_dir":"./terraform-iac/dev/app",
"aws_account":"977306314792",
"aws_gha_role":"hw-fargate-api-dev-gha",
"rfc_key_name":"standard_change_sandbox_client_key",
"rfc_secret_name":"standard_change_sandbox_client_secret",
"rfc_template_id":"Codepipeline-Standard-Change"
}
]
}'
echo matrix=`echo $matrix | jq -c .` >> $GITHUB_ENV
- name: Set up STG Environment Variables
if: github.ref == 'refs/heads/stg'
run: |
matrix='{
"env":[
{
"environment_name":"stg",
"ecr_repo_name":"hw-fargate-api-stg",
"tf_working_dir":"./terraform-iac/stg/app",
"aws_account":"977306314792",
"aws_gha_role":"hw-fargate-api-stg-gha",
"rfc_key_name":"standard_change_sandbox_client_key",
"rfc_secret_name":"standard_change_sandbox_client_secret",
"rfc_template_id":"Codepipeline-Standard-Change"
}
]
}'
echo matrix=`echo $matrix | jq -c .` >> $GITHUB_ENV
- name: Set up PRD/CPY Environment Variables
if: github.ref == 'refs/heads/prd'
# TODO: When prd really is production, in prd environment (first block below):
# * Change standard_change_sandbox_client_key to standard_change_production_client_key
# * Change standard_change_sandbox_client_secret to standard_change_production_client_secret
# You probably don't want to do this in cpy (second block), or you will get two RFCs everytime you push to prd
run: |
matrix='{
"env":[
{
"environment_name":"prd",
"ecr_repo_name":"hw-fargate-api-prd",
"tf_working_dir":"./terraform-iac/prd/app",
"aws_account":"539738229445",
"aws_gha_role":"hw-fargate-api-prd-gha",
"rfc_key_name":"standard_change_sandbox_client_key",
"rfc_secret_name":"standard_change_sandbox_client_secret",
"rfc_template_id":"Codepipeline-Standard-Change"
},
{
"environment_name":"cpy",
"ecr_repo_name":"hw-fargate-api-cpy",
"tf_working_dir":"./terraform-iac/cpy/app",
"aws_account":"539738229445",
"aws_gha_role":"hw-fargate-api-cpy-gha",
"rfc_key_name":"standard_change_sandbox_client_key",
"rfc_secret_name":"standard_change_sandbox_client_secret",
"rfc_template_id":"Codepipeline-Standard-Change"
}
]
}'
echo matrix=`echo $matrix | jq -c .` >> $GITHUB_ENV
outputs:
matrix: ${{ env.matrix }}
test:
name: Test
timeout-minutes: 3
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.node_version }}
cache: npm
cache-dependency-path: '**/package-lock.json'
- name: npm ci
working-directory: src
run: npm ci --prefer-offline
- name: npm test
working-directory: src
run: npm test
- name: Report test coverage to Codecov
uses: codecov/codecov-action@v4
if: env.CODECOV_TOKEN
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
audit:
name: Audit
timeout-minutes: 3
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.node_version }}
# We don't need to install deps to audit them
- name: npm audit
working-directory: src
run: npm audit --audit-level=critical
lint:
name: Lint
timeout-minutes: 3
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.node_version }}
cache: npm
cache-dependency-path: '**/package-lock.json'
- name: npm ci
working-directory: src
run: npm ci --prefer-offline
- name: npm lint
working-directory: src
run: npm run lint
hadolint:
name: Lint Docker
timeout-minutes: 3
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Hadolint follows semantic versioning, but doesn't have a @v2 release
- name: Lint Dockerfile
uses: hadolint/hadolint-action@v3.1.0
with:
dockerfile: src/Dockerfile
format:
name: Terraform Format
timeout-minutes: 3
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Terraform Setup
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.tf_version }}
- name: Terraform Format
working-directory: terraform-iac
run: terraform fmt -check -recursive
build_and_deploy:
name: Build and Deploy / ${{ matrix.env.environment_name }}
timeout-minutes: 90
runs-on: ubuntu-latest
needs: [env, test, audit, lint, hadolint, format]
strategy:
matrix: ${{ fromJson(needs.env.outputs.matrix) }}
fail-fast: false
environment:
name: ${{ matrix.env.environment_name }}
url: https://${{ steps.terraform-outputs.outputs.url }}
permissions:
id-token: write
contents: read
steps:
- name: Check out
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: "arn:aws:iam::${{ matrix.env.aws_account }}:role/${{ matrix.env.aws_gha_role }}"
role-session-name: ${{ github.sha }}
aws-region: us-west-2
- name: Log into Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Get Current Timestamp
id: date
run: echo "timestamp=$(date +'%Y-%m-%d_%H-%M-%S')" >> $GITHUB_OUTPUT
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push the Docker image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPO: ${{ matrix.env.ecr_repo_name }}
IMAGE_TAG: ${{ steps.date.outputs.timestamp }}
uses: docker/build-push-action@v5
with:
context: src
push: true
tags: ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPO}}:${{ env.IMAGE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Terraform Setup
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.tf_version }}
terraform_wrapper: false
- name: Terraform Init
working-directory: ${{ matrix.env.tf_working_dir }}
run: terraform init
- name: Terraform Plan
working-directory: ${{ matrix.env.tf_working_dir }}
run: terraform plan -var 'image_tag=${{ steps.date.outputs.timestamp }}' -input=false -out=plan
- name: Analyze Terraform Plan
uses: byu-oit/github-action-tf-plan-analyzer@v2
if: github.repository_owner == 'byu-oit'
# If you're at BYU, but outside the byu-oit GitHub org, you may be able to obtain credentials by contacting cloudoffice@byu.edu
with:
working-directory: ${{ matrix.env.tf_working_dir }}
terraform-plan-file: plan
divvycloud-username: ${{ secrets.DIVVYCLOUD_USERNAME }}
divvycloud-password: ${{ secrets.DIVVYCLOUD_PASSWORD }}
- name: Start Standard Change
uses: byu-oit/github-action-start-standard-change@v1
id: start-standard-change
with:
client-key: ${{ secrets[matrix.env.rfc_key_name] }}
client-secret: ${{ secrets[matrix.env.rfc_secret_name] }}
template-id: ${{ matrix.env.rfc_template_id }}
- name: Terraform Apply
working-directory: ${{ matrix.env.tf_working_dir }}
run: terraform apply plan
- name: Get Terraform Outputs
id: terraform-outputs
working-directory: ${{ matrix.env.tf_working_dir }}
run: |
echo "codedeploy_app_name=$(terraform output -raw codedeploy_app_name)" >> $GITHUB_OUTPUT
echo "codedeploy_deployment_group_name=$(terraform output -raw codedeploy_deployment_group_name)" >> $GITHUB_OUTPUT
echo "codedeploy_appspec_json_file=$(terraform output -raw codedeploy_appspec_json_file)" >> $GITHUB_OUTPUT
echo "url=$(terraform output -raw url)" >> $GITHUB_OUTPUT
- name: CodeDeploy
id: deploy
uses: byu-oit/github-action-codedeploy@v2
with:
application-name: ${{ steps.terraform-outputs.outputs.codedeploy_app_name }}
deployment-group-name: ${{ steps.terraform-outputs.outputs.codedeploy_deployment_group_name }}
appspec-file: ${{ steps.terraform-outputs.outputs.codedeploy_appspec_json_file }}
- name: End Standard Change
uses: byu-oit/github-action-end-standard-change@v1
if: always() && steps.start-standard-change.outcome == 'success' # Run if RFC started, even if the deploy failed
with:
client-key: ${{ secrets[matrix.env.rfc_key_name] }}
client-secret: ${{ secrets[matrix.env.rfc_secret_name] }}
change-sys-id: ${{ steps.start-standard-change.outputs.change-sys-id }}
work-start: ${{ steps.start-standard-change.outputs.work-start }}
success: ${{ job.status == 'success' }}
- name: Teams Notification
uses: byu-oit/github-action-teams@v3
if: always()
with:
status: ${{ job.status }}
webhook-url: ${{ secrets.MS_TEAMS_WEBHOOK_URL }}