-
Notifications
You must be signed in to change notification settings - Fork 1
167 lines (144 loc) · 5.68 KB
/
production.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
on:
push:
branches:
- production
name: Production
env:
ECS_CONTAINER_MAIN : discord-bot-production
ECS_SERVICE_MAIN : discord-bot-production-service
ECS_CLUSTER : karma-production
ECR_REPOSITORY_MAIN : production-karma-discord-bot
ECS_CONTAINER_DELEGATE : delegate-discord-message-update-stat-production
ECS_SERVICE_DELEGATE : delegate-discord-message-update-stat-production-service
jobs:
build_image:
name: Build docker image
if: |
!failure() && !cancelled()
runs-on: ubuntu-latest
outputs:
image: ${{ steps.build-image.outputs.image }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@master
with:
install: true
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.PRODUCTION_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.PRODUCTION_AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Get .env and certs from Secret Manager
run: |
aws secretsmanager get-secret-value --secret-id production/env --query SecretString --output text >> .env
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
# Key is named differently to avoid collision
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx
- name: Build image
uses: docker/build-push-action@v3
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY_MAIN }}
IMAGE_TAG: ${{ github.sha }}
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
file: Dockerfile
push: true
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY_MAIN }}:${{ github.sha }}
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY_MAIN }}:latest
cache-from: type=local,src=/tmp/.buildx-cache
# Note the mode=max here
# More: https://github.com/moby/buildkit#--export-cache-options
# And: https://github.com/docker/buildx#--cache-tonametypetypekeyvalue
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
- name: Move cache
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Discord notification
uses: sarisia/actions-status-discord@v1
if: failure()
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
deploy-ecs-services:
name: Deploy ECS services
needs: [build_image]
if: |
!failure() && !cancelled()
runs-on: ubuntu-latest
strategy:
matrix:
ECS_CONTAINER: ["discord-bot-production", "delegate-discord-message-update-stat-production" ]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.PRODUCTION_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.PRODUCTION_AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Download task definition for ${{ matrix.ECS_CONTAINER }}
id: download-task
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY_MAIN }}
IMAGE_TAG: ${{ github.sha }}
run: |
aws ecs describe-task-definition --task-definition ${{ matrix.ECS_CONTAINER }} --query taskDefinition > task-definition.json
echo "::set-output name=revision::$(cat task-definition.json | jq .revision)"
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- 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
with:
task-definition: task-definition.json
container-name: ${{ matrix.ECS_CONTAINER }}
image: ${{ steps.download-task.outputs.image }}
- name: Deploy Amazon ECS task definition for ${{ matrix.ECS_CONTAINER }}
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ matrix.ECS_CONTAINER }}-service
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
- name: Check deployed revision
run: |
bash deploy/check_service_revision.sh `expr ${{ steps.download-task.outputs.revision }} + 1` ${{ matrix.ECS_CONTAINER }}-service ${{ env.ECS_CLUSTER }}
- name: Deregister previous revision
run: |
aws ecs deregister-task-definition --task-definition ${{ matrix.ECS_CONTAINER }}:${{ steps.download-task.outputs.revision }} >> /dev/null
- name: Discord notification
uses: sarisia/actions-status-discord@v1
if: failure()
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
notification:
runs-on: ubuntu-latest
needs: [build_image, deploy-ecs-services]
if: needs.deploy-ecs-services.result == 'success'
steps:
- name: Discord notification
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}