-
Notifications
You must be signed in to change notification settings - Fork 16
190 lines (175 loc) · 7.34 KB
/
deploy-worker.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
name: Deploy worker to AWS Elastic Container Service
on:
workflow_call:
outputs:
sending_revert_command:
description: "Command to revert sending worker"
value: ${{ jobs.deploy_sending.outputs.revert_cmd }}
logging_revert_command:
description: "Command to revert logging worker"
value: ${{ jobs.deploy_logging.outputs.revert_cmd }}
env:
# Update this common config
DIRECTORY: worker
jobs:
set_environment:
outputs:
current_env: ${{ steps.set-environment.outputs.current_env }}
runs-on: ubuntu-latest
steps:
- id: set-environment
run: |
echo "Running on branch ${{ github.ref }}"
if [ "${{ github.ref }}" = "refs/heads/master" ]; then
echo "current_env=production" >> $GITHUB_OUTPUT
else
echo "current_env=staging" >> $GITHUB_OUTPUT
fi
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- name: Lint lock file
run: cd $DIRECTORY && npx lockfile-lint --type npm --path package-lock.json -o "https:" -o "file:" --allowed-hosts npm
- name: Lint app code
run: |
cd shared
npm ci
cd ..
cd $DIRECTORY
npm ci
npm run build
npm run lint-no-fix
build_application:
needs: [set_environment, lint]
environment:
name: ${{ needs.set_environment.outputs.current_env }}
env:
IMAGE_TAG: "github-actions-worker-${{ github.sha }}-${{ github.run_id }}-${{github.run_attempt}}"
BRANCH: ${{ needs.set_environment.outputs.current_env }}
outputs:
image: ${{ steps.set-image-tag.outputs.image_tag }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- name: Build shared package
env:
NODE_OPTIONS: "--max-old-space-size=4096"
run: cd shared && npm ci
- name: Replace id of secret to be used for retrieving env vars from secrets manager
env:
NODE_OPTIONS: "--max-old-space-size=4096"
SECRET_ID: ${{ vars.SECRET_ID }}
run: |
cd $DIRECTORY
cp -R ../shared ./
sed -i -e "s#@SECRET_ID#$SECRET_ID#g" docker-entrypoint.sh
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push
uses: docker/build-push-action@v4
with:
context: ${{ env.DIRECTORY }}
push: true
tags: ${{ vars.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }},${{ vars.ECR_REPOSITORY }}:${{ env.BRANCH }}-worker
cache-from: type=registry,ref=${{ vars.ECR_REPOSITORY }}:${{ env.BRANCH }}-worker
cache-to: type=inline
- name: Set image tag
id: set-image-tag
run: echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
deploy_sending:
needs: [set_environment, build_application]
environment:
name: ${{ needs.set_environment.outputs.current_env }}
outputs:
revert_cmd: ${{ steps.get-task-definition-sending.outputs.revert_cmd }}
test_value: ${{ steps.get-task-definition-sending.outputs.test_value }}
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-1
- name: (Sending) Download task definition
id: get-task-definition-sending
env:
TASK_DEFINITION: ${{ vars.WORKER_TASK_DEFINITION_SENDING }}
OUT: task-definition-sending.json
SERVICE: ${{ vars.WORKER_SERVICE_SENDING }}
CLUSTER: ${{ vars.WORKER_CLUSTER }}
run: |
aws ecs describe-task-definition --task-definition $TASK_DEFINITION --query taskDefinition > $OUT
echo "task-definition=$OUT" >> $GITHUB_OUTPUT
CMD="aws ecs update-service --cluster $CLUSTER --service $SERVICE --task-definition $TASK_DEFINITION:$(jq .revision $OUT)"
echo "revert_cmd=$CMD" >> $GITHUB_OUTPUT
- name: (Sending) Fill in the new image ID in the Amazon ECS task definition
id: task-def-sending
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.get-task-definition-sending.outputs.task-definition }}
image: "454894717801.dkr.ecr.ap-southeast-1.amazonaws.com/postmangovsg:${{ needs.build_application.outputs.image }}"
container-name: sending
- name: (Sending) Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def-sending.outputs.task-definition }}
service: ${{ vars.WORKER_SERVICE_SENDING }}
cluster: ${{ vars.WORKER_CLUSTER }}
wait-for-service-stability: true
deploy_logging:
needs: [set_environment, build_application]
environment:
name: ${{ needs.set_environment.outputs.current_env }}
outputs:
revert_cmd: ${{ steps.get-task-definition-logging.outputs.revert_cmd }}
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-1
- name: (Logging) Download task definition
id: get-task-definition-logging
env:
TASK_DEFINITION: ${{ vars.WORKER_TASK_DEFINITION_LOGGING }}
OUT: task-definition-logging.json
SERVICE: ${{ vars.WORKER_SERVICE_LOGGING }}
CLUSTER: ${{ vars.WORKER_CLUSTER }}
run: |
aws ecs describe-task-definition --task-definition $TASK_DEFINITION --query taskDefinition > $OUT
echo "task-definition=$OUT" >> $GITHUB_OUTPUT
CMD="aws ecs update-service --cluster $CLUSTER --service $SERVICE --task-definition $TASK_DEFINITION:$(jq .revision $OUT)"
echo "revert_cmd=$CMD" >> $GITHUB_OUTPUT
- name: (Logging) Fill in the new image ID in the Amazon ECS task definition
id: task-def-logging
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.get-task-definition-logging.outputs.task-definition }}
image: "454894717801.dkr.ecr.ap-southeast-1.amazonaws.com/postmangovsg:${{ needs.build_application.outputs.image }}"
container-name: logging
- name: (Logging) Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def-logging.outputs.task-definition }}
service: ${{ vars.WORKER_SERVICE_LOGGING }}
cluster: ${{ vars.WORKER_CLUSTER }}
wait-for-service-stability: true