-
Notifications
You must be signed in to change notification settings - Fork 2
131 lines (110 loc) Β· 4.11 KB
/
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
name: Deploy to AWS ECS on Fargate
on:
push:
branches:
[ "develop" ]
workflow_dispatch:
jobs:
boot-jar:
if: ${{ secrets.ENABLE_CD == 'true' }}
runs-on: ubuntu-latest
outputs:
cache-hit: ${{ steps.cache.outputs.cache-hit }}
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: gradle
- name: BootJar With Gradle Wrapper
run: ./gradlew bootJar
- name: Look up cached jar
uses: actions/cache/restore@v4
id: cache
with:
path: app/external-api/build/libs
key: ${{ runner.os }}-cached-jar-${{ hashFiles('app/external-api/build/libs/*.jar') }}
lookup-only: true
- name: Log cache step
env:
CACHE_OUTPUT: ${{ toJSON(steps.cache.outputs) }}
run: |
echo $CACHE_OUTPUT
- name: Cache jar
if: steps.cache.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: app/external-api/build/libs
key: ${{ runner.os }}-cached-jar-${{ hashFiles('app/external-api/build/libs/*.jar') }}
deploy:
# jar νμΌμ΄ κ°±μ λκ²½μ°μλ§ deploy μ§ν
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: devs-spring-boot
ECS_SERVICE: devs-spring-server-service
ECS_CLUSTER: devs-cluster-be-01
ECS_TASK_DEFINITION: ./devs-spring-server-task.json
CONTAINER_NAME: spring-boot
needs: boot-jar
if: ${{ secrets.ENABLE_CD == 'true' && needs.boot-jar.outputs.cache-hit != 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Restore jar
uses: actions/cache/restore@v4
with:
path: app/external-api/build/libs
key: ${{ runner.os }}-cached-jar-
restore-keys: ${{ runner.os }}-cached-jar-
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
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: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build and Push Image to Amazon ECR
uses: docker/build-push-action@v3
id: build-and-push
env:
ECR: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}
with:
context: .
push: true
provenance: false
tags: ${{ env.ECR }}:${{ github.sha }}
platforms: linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
- 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: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ fromJSON(steps.build-and-push.outputs.metadata)['image.name'] }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
id: ecs-deploy
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
- name: Check if deployment was successful
id: check-deployment
run: |
CURRENT_TASK_DEF_ARN=$(aws ecs describe-services --cluster ${{ env.ECS_CLUSTER }} --services ${{ env.ECS_SERVICE }} --query services[0].deployments[0].taskDefinition | jq -r ".")
NEW_TASK_DEF_ARN=${{ steps.ecs-deploy.outputs.task-definition-arn }}
echo "Current task arn: $CURRENT_TASK_DEF_ARN"
echo "New task arn: $NEW_TASK_DEF_ARN"
if [ "$CURRENT_TASK_DEF_ARN" != "$NEW_TASK_DEF_ARN" ]; then
echo "Deployment failed."
exit 1
fi