-
Notifications
You must be signed in to change notification settings - Fork 0
211 lines (195 loc) · 7.67 KB
/
deploy-ecs.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
name: Deploy to Amazon ECS
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true
on:
push:
branches: [ "staging" ]
env:
AWS_REGION: ap-northeast-2 # AWS region 설정
ECR_REPOSITORY: gyeongdan-fastapi # Amazon ECR repository 이름
ECS_SERVICE: GyeongdanFastAPI # Amazon ECS 서비스 이름
ECS_CLUSTER: Gyeongdan # Amazon ECS 클러스터 이름
ECS_TASK_DEFINITION: tf-staging.json # Amazon ECS task definition 파일 경로
CONTAINER_NAME: FastAPI # 컨테이너 이름
PROGRESS_SLACK_CHANNEL: C07BRCDNBMF # Slack 채널 ID
permissions:
contents: read
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Post Slack Channel that Build Start
id: slack-build-start
uses: slackapi/[email protected]
with:
channel-id: ${{ env.PROGRESS_SLACK_CHANNEL }}
payload: |
{
"text": ":small_airplane: *Gyeongdan FastAPI ${{ github.ref_name }}* 배포가 시작되었습니다.",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":mega: *Gyeongdan FastAPI ${{ github.ref_name }}* 배포가 시작되었습니다."
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "깃허브액션에서 확인하기."
},
"value": "click_me_123",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}
]
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.PROGRESS_SLACK_CHANNEL_TOKEN }}
- name: Checkout
uses: actions/checkout@v4
- 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: ${{ env.AWS_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@v3
- name: Docker build & Push Docker image to Amazon ECR
id: build-image
uses: docker/build-push-action@v5
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
with:
context: .
push: true
tags: ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
build-args: |
DB_HOST=${{ secrets.DB_HOST }}
DB_USER=${{ secrets.DB_USER }}
DB_PASSWORD=${{ secrets.DB_PASSWORD }}
DB_NAME=${{ secrets.DB_NAME }}
DB_PORT=${{ secrets.DB_PORT }}
OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}
GOOGLE_CSE_ID=${{ secrets.GOOGLE_CSE_ID }}
GOOGLE_API_KEY=${{ secrets.GOOGLE_API_KEY }}
USER_AGENT=${{ secrets.USER_AGENT }}
cache-from: type=gha
cache-to: type=gha,mode=min,ignore-error=true
- 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
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
- name: Deploy Amazon ECS task definition
id: ecs-deploy
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
wait-for-minutes: 10
- name: Verify New Task Definition is Deployed
run: |
CURRENT_TASK_DEF_ARN=$(aws ecs describe-services --cluster ${{ env.ECS_CLUSTER }} --service ${{ 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
- name: Post Slack Channel that Build Success
if: success()
id: slack-build-success
uses: slackapi/[email protected]
with:
channel-id: ${{ env.PROGRESS_SLACK_CHANNEL }}
payload: |
{
"text": ":white_check_mark: *Gyeongdan FastAPI ${{ github.ref_name }}* 배포가 성공했습니다.",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":pedro: *Gyeongdan FastAPI ${{ github.ref_name }}* 배포가 성공했습니다."
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "깃허브액션에서 확인하기."
},
"style": "primary",
"value": "click_me_123",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}
]
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.PROGRESS_SLACK_CHANNEL_TOKEN }}
- name: Post Slack Channel that Build Fail
if: failure()
id: slack-build-fail
uses: slackapi/[email protected]
with:
channel-id: ${{ env.PROGRESS_SLACK_CHANNEL }}
payload: |
{
"text": ":x: *Gyeongdan FastAPI ${{ github.ref_name }}* 배포가 실패했습니다.",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":typingcat: *Gyeongdan FastAPI ${{ github.ref_name }}* 배포가 실패했습니다."
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": true,
"text": "깃허브액션에서 확인하기."
},
"style": "danger",
"value": "click_me_123",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}
]
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.PROGRESS_SLACK_CHANNEL_TOKEN }}