-
Notifications
You must be signed in to change notification settings - Fork 2
192 lines (169 loc) · 8 KB
/
build-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
name: Build Deploy
on:
push:
branches:
- main
- staging
permissions:
id-token: write
contents: read # This is required for actions/checkout
jobs:
get-environment:
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.get_environment.outputs.environment }}
steps:
- id: get_environment
name: Get Environment
run: |
if [[ "$GITHUB_REF" = "refs/heads/staging" ]]
then
echo "environment=staging" >> $GITHUB_OUTPUT
elif [[ "$GITHUB_REF" = "refs/heads/main" ]]
then
echo "environment=production" >> $GITHUB_OUTPUT
else
echo "Fatal error. Could not determine environment"
exit 1
fi
build-deploy-store:
runs-on: ubuntu-latest
needs: ['get-environment']
environment:
name: ${{ needs.get-environment.outputs.environment }}
steps:
- name: Git clone the repository
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@885d1462b80bc1c1c7f0b00334ad271f09369c55 # v2
with:
install: true
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: github-actions-store-public-ecr
aws-region: us-east-1
- name: Login to public Amazon ECR
id: login-ecr-public
uses: aws-actions/amazon-ecr-login@2fc7aceee09e9e4a7105c0d060c656fad0b4f63d # v1
with:
registry-type: public
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: github-actions-store-build-push
aws-region: us-west-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@2fc7aceee09e9e4a7105c0d060c656fad0b4f63d # v1
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: store-ecr
IMAGE_TAG: ${{ github.sha }}
PUBLIC_ASSETS_PATH: ${{ secrets.PUBLIC_ASSETS_PATH }}
run: |
docker buildx build --push --build-arg PUBLIC_ASSETS_PATH=$PUBLIC_ASSETS_PATH \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from=type=registry,ref=$ECR_REGISTRY/$ECR_REPOSITORY:latest \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest .
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
# Cannot use `--cache-to` due to https://github.com/docker/buildx/issues/271
# Caching to `latest` with inline caching which seems to work
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition store --query taskDefinition > task-definition.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@3c975f1cb22919a28755c6541b4ca2656a690f49 # v1
with:
task-definition: task-definition.json
container-name: store
image: ${{ steps.build-image.outputs.image }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@69e7aed9b8acdd75a6c585ac669c33831ab1b9a3 # v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: store
cluster: store
wait-for-service-stability: true
build-deploy-store-backend:
runs-on: ubuntu-latest
needs: ['get-environment']
environment:
name: ${{ needs.get-environment.outputs.environment }}
steps:
- name: Git clone the repository
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@885d1462b80bc1c1c7f0b00334ad271f09369c55 # v2
with:
install: true
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: github-actions-store-public-ecr
aws-region: us-east-1
- name: Login to public Amazon ECR
id: login-ecr-public
uses: aws-actions/amazon-ecr-login@2fc7aceee09e9e4a7105c0d060c656fad0b4f63d # v1
with:
registry-type: public
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: github-actions-store-build-push
aws-region: us-west-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@2fc7aceee09e9e4a7105c0d060c656fad0b4f63d # v1
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: store-backend-ecr
IMAGE_TAG: ${{ github.sha }}
run: |
SESSION_SECRET=$( xxd -u -l 16 -p /dev/urandom )
docker buildx build --push --build-arg SESSION_SECRET=$SESSION_SECRET --build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from=type=registry,ref=$ECR_REGISTRY/$ECR_REPOSITORY:latest \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest api/
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
# Cannot use `--cache-to` due to https://github.com/docker/buildx/issues/271
# Caching to `latest` with inline caching which seems to work
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition store-backend --query taskDefinition > task-definition.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@3c975f1cb22919a28755c6541b4ca2656a690f49 # v1
with:
task-definition: task-definition.json
container-name: store-backend
image: ${{ steps.build-image.outputs.image }}
- name: Register Amazon ECS task definition for DB migration
uses: aws-actions/amazon-ecs-deploy-task-definition@69e7aed9b8acdd75a6c585ac669c33831ab1b9a3 # v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
- name: DB migration
run: |
run_result=$(aws ecs run-task --overrides '{ "containerOverrides": [{ "name": "store-backend", "command": [ "npm", "run", "migrate" ] }]}' --task-definition store-backend --cluster store --launch-type="FARGATE" --network-configuration '{ "awsvpcConfiguration": { "subnets": ["${{ secrets.ECS_DB_MIGRATION_SUBNET }}"],"securityGroups": ["${{ secrets.ECS_DB_MIGRATION_SG }}"] }}')
container_arn=$(echo $run_result | jq '.tasks[0].taskArn' | sed -e 's/^"//' -e 's/"$//')
echo "container arn: ${container_arn}"
aws ecs wait tasks-stopped --cluster store --tasks "${container_arn}"
describe_result=$(aws ecs describe-tasks --cluster store --tasks "${container_arn}")
terminated_status=$(echo $describe_result | jq '.tasks[0].containers[0].exitCode')
echo "exit status:" $terminated_status
exit $terminated_status
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@69e7aed9b8acdd75a6c585ac669c33831ab1b9a3 # v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: store-backend
cluster: store
wait-for-service-stability: true