-
Notifications
You must be signed in to change notification settings - Fork 0
360 lines (314 loc) · 12.5 KB
/
ci-cd-main.yaml
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
name: 'CI/CD MAIN'
on:
push:
branches:
- 'main'
env:
AWS_REGION: ap-northeast-2
jobs:
build-api:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/[email protected]
- name: AWS IAM
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.ECR_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ECR_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
- name: Cache Gradle packages
uses: actions/[email protected]
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: make application.yaml
run: |
cd ./api/src/main/resources/
touch ./application.yaml
echo "${{ secrets.YAML_PROD_API }}" > ./application.yaml
shell: bash
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew :api:build -x test
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Delete existing image with same tag
run: |
if aws ecr list-images \
--repository-name backgu-repository \
--region ${{env.AWS_REGION}} \
--query 'imageIds[?imageTag==`api`]' \
--output text | grep -q 'api'; then
aws ecr batch-delete-image \
--repository-name backgu-repository \
--image-ids imageTag=api \
--region ap-northeast-2
echo "Image with tag 'api' deleted successfully."
else
echo "Image with tag 'api' not found. No action taken."
fi
- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: backgu-repository
IMAGE_TAG: api
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -f Dockerfile-api .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
- name: Deploy Images with Docker compose
uses: appleboy/[email protected]
env:
APP: "backgu-api"
COMPOSE: "/home/ec2-user/compose/docker-compose-api.yaml"
with:
host: ${{secrets.EC2_PROD_BASTION}}
username: ec2-user
key: ${{secrets.EC2_PROD_PRIVATE_KEY}}
port: 22
envs: APP, COMPOSE
script_stop: true
script: |
ssh api01 "aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REGISTRY }}"
ssh api01 docker-compose -f $COMPOSE down
ssh api01 docker pull ${{secrets.IMAGE_API}}
ssh api01 docker-compose -p api -f $COMPOSE up -d
build-notification:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/[email protected]
- name: AWS IAM
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.ECR_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ECR_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
- name: Cache Gradle packages
uses: actions/[email protected]
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: make application.yaml
run: |
cd ./notification/src/main/resources/
touch ./application.yaml
echo "${{ secrets.YAML_PROD_NOTIFICATION }}" > ./application.yaml
mkdir -p firebase
echo "${{ secrets.FCM_ACCOUNT }}" > ./firebase/fcm.json
shell: bash
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew :notification:build -x test
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Delete existing image with same tag
run: |
if aws ecr list-images \
--repository-name backgu-repository \
--region ${{env.AWS_REGION}} \
--query 'imageIds[?imageTag==`notification`]' \
--output text | grep -q 'notification'; then
aws ecr batch-delete-image \
--repository-name backgu-repository \
--image-ids imageTag=notification \
--region ap-northeast-2
echo "Image with tag 'notification' deleted successfully."
else
echo "Image with tag 'notification' not found. No action taken."
fi
- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: backgu-repository
IMAGE_TAG: notification
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -f Dockerfile-notification .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
- name: Deploy Images with Docker compose
uses: appleboy/[email protected]
env:
APP: "backgu-notification"
COMPOSE: "/home/ec2-user/compose/docker-compose-notification.yaml"
with:
host: ${{secrets.EC2_PROD_BASTION}}
username: ec2-user
key: ${{secrets.EC2_PROD_PRIVATE_KEY}}
port: 22
envs: APP, COMPOSE
script_stop: true
script: |
ssh notification01 "aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REGISTRY }}"
ssh notification01 docker-compose -f $COMPOSE down
ssh notification01 docker pull ${{secrets.IMAGE_NOTIFICATION}}
ssh notification01 docker-compose -p notification -f $COMPOSE up -d
build-batch:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/[email protected]
- name: AWS IAM
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.ECR_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ECR_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
- name: Cache Gradle packages
uses: actions/[email protected]
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: make application.yaml
run: |
cd ./batch/src/main/resources/
touch ./application.yaml
echo "${{ secrets.YAML_PROD_BATCH }}" > ./application.yaml
shell: bash
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew :batch:build -x test
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Delete existing image with same tag
run: |
if aws ecr list-images \
--repository-name backgu-repository \
--region ${{env.AWS_REGION}} \
--query 'imageIds[?imageTag==`batch`]' \
--output text | grep -q 'batch'; then
aws ecr batch-delete-image \
--repository-name backgu-repository \
--image-ids imageTag=batch \
--region ap-northeast-2
echo "Image with tag 'batch' deleted successfully."
else
echo "Image with tag 'batch' not found. No action taken."
fi
- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: backgu-repository
IMAGE_TAG: batch
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -f Dockerfile-batch .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
- name: Deploy Images with Docker compose
uses: appleboy/[email protected]
env:
APP: "backgu-batch"
COMPOSE: "/home/ec2-user/compose/docker-compose-batch.yaml"
with:
host: ${{secrets.EC2_PROD_BASTION}}
username: ec2-user
key: ${{secrets.EC2_PROD_PRIVATE_KEY}}
port: 22
envs: APP, COMPOSE
script_stop: true
script: |
ssh batch01 "aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REGISTRY }}"
ssh batch01 docker-compose -f $COMPOSE down
ssh batch01 docker pull ${{secrets.IMAGE_BATCH}}
ssh batch01 docker-compose -p batch -f $COMPOSE up -d
build-realtime:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/[email protected]
- name: AWS IAM
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.ECR_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ECR_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
- name: Cache Gradle packages
uses: actions/[email protected]
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: make application.yaml
run: |
cd ./realtime/src/main/resources/
touch ./application.yaml
echo "${{ secrets.YAML_PROD_REALTIME }}" > ./application.yaml
shell: bash
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew :realtime:build -x test
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Delete existing image with same tag
run: |
if aws ecr list-images \
--repository-name backgu-repository \
--region ${{env.AWS_REGION}} \
--query 'imageIds[?imageTag==`realtime`]' \
--output text | grep -q 'realtime'; then
aws ecr batch-delete-image \
--repository-name backgu-repository \
--image-ids imageTag=realtime \
--region ap-northeast-2
echo "Image with tag 'realtime' deleted successfully."
else
echo "Image with tag 'realtime' not found. No action taken."
fi
- name: Build, tag, and push docker image to Amazon ECR
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: backgu-repository
IMAGE_TAG: realtime
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -f Dockerfile-realtime .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
- name: Deploy Images with Docker compose
uses: appleboy/[email protected]
env:
APP: "backgu-realtime"
COMPOSE: "/home/ec2-user/compose/docker-compose-realtime.yaml"
with:
host: ${{secrets.EC2_PROD_BASTION}}
username: ec2-user
key: ${{secrets.EC2_PROD_PRIVATE_KEY}}
port: 22
envs: APP, COMPOSE
script_stop: true
script: |
ssh realtime01 "aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REGISTRY }}"
ssh realtime01 docker-compose -f $COMPOSE down
ssh realtime01 docker pull ${{secrets.IMAGE_REALTIME}}
ssh realtime01 docker-compose -p realtime -f $COMPOSE up -d