-
Notifications
You must be signed in to change notification settings - Fork 1
264 lines (246 loc) · 9.63 KB
/
aws_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
name: AWS Deploy
on:
workflow_call:
inputs:
environment:
description: "Deployment environment"
required: true
type: string
ecs-container-port:
description: "Port that ecs listens on for traffic"
required: true
type: number
shortEnv:
description: "Deployment environment, the short version (one of [dev, prod, stg, test, uat, vapt])"
required: true
type: string
aws-account-id:
description: "AWS account ID to use"
required: true
type: string
aws-region:
description: "AWS region to use"
required: true
default: "ap-southeast-1"
type: string
cicd-role:
description: "AWS IAM role to assume by GitHub action runner"
required: true
type: string
ecr-repository:
description: "ECR repository to push image to"
required: true
type: string
ecs-cluster-name:
description: "ECS cluster to deploy to"
required: true
type: string
ecs-task-definition-path:
description: "ECS task definition path"
default: "ecs-task-definition.json"
type: string
ecs-service-name:
description: "ECS service to deploy to"
required: true
type: string
ecs-container-name:
description: "Name of container in ECS task definition"
required: true
type: string
codedeploy-application:
description: "CodeDeploy application to use"
required: true
type: string
codedeploy-appspec-path:
description: "CodeDeploy appspec.json/yml file path"
default: "appspec.json"
type: string
codedeploy-deployment-group:
description: "CodeDeploy deployment group to use"
required: true
type: string
ecs-task-role:
description: "Task role that ecs uses to run our app"
required: true
type: string
ecs-task-exec-role:
description: "Task role that ecs uses to deploy our app"
required: true
type: string
app-url:
description: "URL of the app"
required: false
type: string
app-name:
description: "Name of the app"
required: false
default: "Isomer Studio"
type: string
app-version:
description: "Version of the app"
required: false
default: "0.0.0"
type: string
app-enable-sgid:
description: "Enable SGID login"
required: false
default: false
type: boolean
app-s3-region:
description: "S3 region for the assets bucket"
required: false
default: "ap-southeast-1"
type: string
app-s3-assets-domain-name:
description: "Public domain name for the assets bucket"
required: false
default: "user-content.example.com"
type: string
app-s3-assets-bucket-name:
description: "Name of the assets S3 bucket"
required: false
default: "example-assets-bucket"
type: string
app-growthbook-client-key:
description: "Growthbook client key"
required: false
type: string
app-intercom-app-id:
description: "Intercom app ID"
required: false
type: string
secrets:
DD_API_KEY:
description: "Datadog API key for uploading sourcemaps"
required: false
RDS_READER_ENDPOINT:
description: "RDS reader endpoint for database connection"
required: false
permissions:
id-token: write
contents: read
jobs:
build:
name: Build and push image to ECR
environment: ${{ inputs.environment }}
runs-on: ubuntu-latest
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: network=host
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ inputs.cicd-role }}
role-session-name: github-action-application-deploy
aws-region: ${{ inputs.aws-region }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
mask-password: "true"
- name: Checkout source code
uses: actions/checkout@v3
- name: Build and push image to ECR
uses: docker/build-push-action@v4
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ inputs.ecr-repository }}
ENV: ${{ inputs.environment }}
IMAGE_TAG: ${{ github.sha }}
with:
context: .
file: "./apps/studio/Dockerfile"
push: true
tags: |
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}
build-args: |
NEXT_PUBLIC_APP_ENV=${{ inputs.environment }}
NEXT_PUBLIC_APP_URL=${{ inputs.app-url }}
NEXT_PUBLIC_APP_NAME=${{ inputs.app-name }}
NEXT_PUBLIC_APP_VERSION=${{ inputs.app-version }}
NEXT_PUBLIC_ENABLE_SGID=${{ inputs.app-enable-sgid }}
NEXT_PUBLIC_S3_REGION=${{ inputs.app-s3-region }}
NEXT_PUBLIC_S3_ASSETS_DOMAIN_NAME=${{ inputs.app-s3-assets-domain-name }}
NEXT_PUBLIC_S3_ASSETS_BUCKET_NAME=${{ inputs.app-s3-assets-bucket-name }}
NEXT_PUBLIC_GROWTHBOOK_CLIENT_KEY=${{ inputs.app-growthbook-client-key }}
NEXT_PUBLIC_INTERCOM_APP_ID=${{ inputs.app-intercom-app-id }}
deploy:
name: Deploy image to ECS
runs-on: ubuntu-latest
needs: build
environment: ${{ inputs.environment }}
env:
DD_API_KEY: ${{ secrets.DD_API_KEY }}
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ inputs.cicd-role }}
role-session-name: github-action-application-deploy
aws-region: ${{ inputs.aws-region }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
mask-password: "true"
- name: Checkout source code
uses: actions/checkout@v3
- name: Replace variables in task definition file
id: replace-variables
run: |
sed -i 's/<AWS_ACCOUNT_ID>/${{ inputs.aws-account-id }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<ENV>/${{ inputs.environment }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<SHORT_ENV>/${{ inputs.shortEnv }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<CPU>/${{ inputs.environment == 'production' && 1024 || 512 }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<MEMORY>/${{ inputs.environment == 'production' && 2048 || 1024 }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<RDS_READER_ENDPOINT>/${{ secrets.RDS_READER_ENDPOINT }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<RDS_DATADOG_PASSWORD>/${{ secrets.RDS_DATADOG_PASSWORD}}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<DD_COMMIT_SHA>/${{ github.sha }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<ECS_TASK_ROLE>/${{ inputs.ecs-task-role }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<ECS_TASK_EXEC_ROLE>/${{ inputs.ecs-task-exec-role }}/g' ${{ inputs.ecs-task-definition-path }}
sed -i 's/<DD_API_KEY>/${{ secrets.DD_API_KEY }}/g' ${{ inputs.ecs-task-definition-path }}
- name: Replace variables in appspec
run: |
sed -i 's/<AWS_ACCOUNT_ID>/${{ inputs.aws-account-id }}/g' .aws/deploy/appspec.json
sed -i 's/<ENV>/${{ inputs.environment}}/g' .aws/deploy/appspec.json
sed -i 's/<CONTAINER_NAME>/${{ inputs.ecs-container-name }}/g' .aws/deploy/appspec.json
sed -i 's/<CONTAINER_PORT>/${{ inputs.ecs-container-port }}/g' .aws/deploy/appspec.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ inputs.ecr-repository }}
ENV: ${{ inputs.environment }}
IMAGE_TAG: ${{ github.sha }}
with:
task-definition: ${{ inputs.ecs-task-definition-path }}
container-name: ${{ inputs.ecs-container-name }}
image: ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
cluster: ${{ inputs.ecs-cluster-name }}
service: ${{ inputs.ecs-service-name }}
wait-for-service-stability: true
codedeploy-appspec: ${{ inputs.codedeploy-appspec-path }}
codedeploy-application: ${{ inputs.codedeploy-application }}
codedeploy-deployment-group: ${{ inputs.codedeploy-deployment-group }}
# - name: Upload sourcemaps to Datadog
# if: ${{ env.DD_API_KEY != '' }}
# env:
# IMAGE_TAG: ${{ env.LOCAL_IMAGE_TAG }}
# shell: bash
# run: |
# npm install -g @datadog/datadog-ci
# rm -rf assets
# docker cp $(docker create ${IMAGE_TAG}):/opt/nemsw/services/frontend/build/assets assets
# datadog-ci sourcemaps upload assets \
# --service=bright --release-version=${{ github.sha }} --minified-path-prefix=/assets/