-
Notifications
You must be signed in to change notification settings - Fork 17
215 lines (202 loc) · 8.02 KB
/
deploy-eb.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
name: Deploy backend to AWS Elastic Beanstalk
on:
workflow_call:
outputs:
revert_command_backend:
description: "Command to revert backend deployment"
value: ${{ jobs.deploy_backend.outputs.revert_cmd }}
revert_command_callback:
description: "Command to revert callback server deployment"
value: ${{ jobs.deploy_callback.outputs.revert_cmd }}
env:
# Update this common config
DIRECTORY: backend
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-test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
redis:
image: redis:alpine
ports:
- "6379:6379"
options: --entrypoint redis-server
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 and test app code
env:
NODE_OPTIONS: "--max-old-space-size=4096"
run: |
cd shared
npm ci
cd ..
cd $DIRECTORY
npm ci
npm run build
npm run lint-no-fix
npm test
build_application:
needs: [set_environment, lint-test]
environment:
name: ${{ needs.set_environment.outputs.current_env }}
env:
IMAGE_TAG: "github-actions-backend-${{ github.sha }}-${{ github.run_id }}-${{github.run_attempt}}"
BRANCH: ${{ needs.set_environment.outputs.current_env }}
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.set-image-tag.outputs.image_tag }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
- name: Before deploy
env:
SECRET_ID: ${{ vars.SECRET_ID }}
DD_API_KEY: ${{ secrets.DD_API_KEY }}
run: |
cd $DIRECTORY
cp -R ../shared ./
sed -i -e "s#@SECRET_ID#$SECRET_ID#g" docker-entrypoint.sh
sed -i -e "s#@DD_API_KEY#$DD_API_KEY#g" .ebextensions/99datadog.config
- 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: ${{ vars.AWS_DEFAULT_REGION }}
- 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 }}-backend
cache-from: type=registry,ref=${{ vars.ECR_REPOSITORY }}:${{ env.BRANCH }}-backend
cache-to: type=inline
- name: Create and push EB config files
run: |
sed -i -e "s|@TAG|$IMAGE_TAG|g" Dockerrun.aws.json
sed -i -e "s|@REPO|$ECR_REPOSITORY|g" Dockerrun.aws.json
zip -r "$IMAGE_TAG.zip" .ebextensions .platform Dockerrun.aws.json
working-directory: ${{ env.DIRECTORY }}
env:
ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }}
- name: Copy to S3
env:
BUCKET_NAME: postmangovsg-elasticbeanstalk-appversion
run: |
cd backend
aws s3 cp $IMAGE_TAG.zip s3://$BUCKET_NAME/$IMAGE_TAG.zip
- name: Create application version
env:
BUCKET_NAME: postmangovsg-elasticbeanstalk-appversion
APP_NAME: postmangovsg
run: |
cd backend
TRUNCATED_DESC=$(echo "${{github.event.head_commit.message}}" | head -n 1 | cut -b1-180)
aws elasticbeanstalk create-application-version --application-name $APP_NAME \
--version-label $IMAGE_TAG \
--source-bundle S3Bucket=$BUCKET_NAME,S3Key=$IMAGE_TAG.zip \
--description "$TRUNCATED_DESC"
- name: Set image tag
id: set-image-tag
run: echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
deploy_backend:
needs: [set_environment, build_application]
environment:
name: ${{ needs.set_environment.outputs.current_env }}
outputs:
revert_cmd: ${{ steps.generate-revert-command.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: ${{ vars.AWS_DEFAULT_REGION }}
- name: Generate revert commands
id: generate-revert-command
env:
APP_NAME: postmangovsg
EB_ENV_NAME: ${{ vars.EB_BACKEND_ENV_NAME }}
run: |
CURRENT_VERSION_LABEL=$(aws elasticbeanstalk describe-environments --application-name $APP_NAME --environment-names $EB_ENV_NAME --max-items 1 | jq .Environments[0].VersionLabel)
echo "revert_cmd=aws elasticbeanstalk update-environment --application-name $APP_NAME --environment-name $EB_ENV_NAME --version-label $CURRENT_VERSION_LABEL" >> $GITHUB_OUTPUT
- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v21
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: postmangovsg
environment_name: ${{ vars.EB_BACKEND_ENV_NAME }}
version_label: ${{ needs.build_application.outputs.image_tag }}
region: ap-southeast-1
wait_for_environment_recovery: 480 # 8 minutes
deploy_callback:
needs: [set_environment, build_application]
environment:
name: ${{ needs.set_environment.outputs.current_env }}
outputs:
revert_cmd: ${{ steps.generate-revert-command.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: ${{ vars.AWS_DEFAULT_REGION }}
- name: Generate revert commands
id: generate-revert-command
env:
APP_NAME: postmangovsg
EB_ENV_NAME: ${{ vars.EB_CALLBACK_ENV_NAME }}
run: |
CURRENT_VERSION_LABEL=$(aws elasticbeanstalk describe-environments --application-name $APP_NAME --environment-names $EB_ENV_NAME --max-items 1 | jq .Environments[0].VersionLabel)
echo "revert_cmd=aws elasticbeanstalk update-environment --application-name $APP_NAME --environment-name $EB_ENV_NAME --version-label $CURRENT_VERSION_LABEL" >> $GITHUB_OUTPUT
- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v21
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: postmangovsg
environment_name: ${{ vars.EB_CALLBACK_ENV_NAME }}
version_label: ${{ needs.build_application.outputs.image_tag }}
region: ap-southeast-1
wait_for_environment_recovery: 480 # 8 minutes