-
Notifications
You must be signed in to change notification settings - Fork 0
225 lines (206 loc) · 7.5 KB
/
cd-dev.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
name: Continuous Deployment to Dev
on:
workflow_dispatch:
inputs:
version:
description: 'Version to deploy (e.g., v1.2.3)'
required: true
type: string
default: 'latest'
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: threedays-app
EB_APPLICATION_NAME: threedays-dev
EB_ENVIRONMENT_NAME: threedays-dev-env
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
jobs:
deploy:
runs-on: ubuntu-latest
environment: dev
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Determine version to deploy
id: determine-version
env:
GITHUB_EVENT_NAME: ${{ github.event_name }}
REPOSITORY_DISPATCH_VERSION: ${{ github.event.client_payload.version }}
WORKFLOW_DISPATCH_VERSION: ${{ github.event.inputs.version }}
run: |
if [ "${GITHUB_EVENT_NAME}" = "repository_dispatch" ]; then
echo "VERSION=${REPOSITORY_DISPATCH_VERSION}" >> $GITHUB_OUTPUT
elif [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
echo "VERSION=${WORKFLOW_DISPATCH_VERSION}" >> $GITHUB_OUTPUT
else
echo "Error: Unexpected event type"
exit 1
fi
- name: Send Deployment Start Webhook
env:
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
VERSION: ${{ steps.determine-version.outputs.VERSION }}
run: |
curl -H "Content-Type: application/json" -X POST -d '{
"embeds": [{
"title": "배포 시작",
"color": 16776960,
"fields": [
{
"name": "버전",
"value": "'"${VERSION}"'",
"inline": true
},
{
"name": "환경",
"value": "'"${EB_ENVIRONMENT_NAME}"'",
"inline": true
},
{
"name": "배포자",
"value": "'"${GITHUB_ACTOR}"'",
"inline": true
}
],
"url": "'"${WORKFLOW_URL}"'"
}]
}' ${{ env.DISCORD_WEBHOOK }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
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@v2
- name: Check if image exists in ECR
id: check-image
env:
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
VERSION: ${{ steps.determine-version.outputs.VERSION }}
run: |
set +e
aws ecr describe-images --repository-name $ECR_REPOSITORY --image-ids imageTag=$VERSION
EXIT_CODE=$?
set -e
if [ $EXIT_CODE -ne 0 ]; then
echo "::error::ECR 리포지토리 $ECR_REPOSITORY 에 태그 $VERSION 의 이미지가 존재하지 않습니다."
exit 1
fi
echo "ECR에서 이미지를 찾았습니다."
- name: Generate Dockerrun.aws.json with environment variables
env:
VERSION: ${{ steps.determine-version.outputs.VERSION }}
REGISTRY_URL: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
VARS_CONTEXT: ${{ toJson(vars) }}
SECRETS_CONTEXT: ${{ toJson(secrets) }}
run: |
cat > Dockerrun.aws.json << EOF
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "${REGISTRY_URL}/${ECR_REPOSITORY}:${VERSION}",
"Update": "true"
},
"Ports": [
{
"ContainerPort": 8080,
"HostPort": 80
}
],
"Volumes": [],
"Logging": "/var/log/nginx"
}
EOF
echo '{}' | jq --argjson secrets "$SECRETS_CONTEXT" \
--argjson vars "$VARS_CONTEXT" \
'$secrets + $vars | to_entries | map(select(.key | test("^AWS_") | not) | {key: .key, value: .value})' \
> env_vars.json
- name: Create .ebextensions directory
run: mkdir -p .ebextensions
- name: Create env.config file
run: |
cat > .ebextensions/env.config << EOF
option_settings:
aws:elasticbeanstalk:application:environment:
EOF
jq -r '.[] | " \(.key): \"\(.value)\""' env_vars.json >> .ebextensions/env.config
- name: Create deployment package
run: |
zip -r deploy.zip Dockerrun.aws.json .ebextensions
- name: Get current time
id: current-time
run: echo "::set-output name=time::$(date +'%Y%m%d%H%M%S')"
- name: Deploy to Elastic Beanstalk
id: deploy
uses: einaregilsson/beanstalk-deploy@v22
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: ${{ env.EB_APPLICATION_NAME }}
environment_name: ${{ env.EB_ENVIRONMENT_NAME }}
version_label: ${{ steps.determine-version.outputs.VERSION }}-${{ steps.current-time.outputs.time }}
region: ${{ env.AWS_REGION }}
deployment_package: deploy.zip
use_existing_version_if_available: false
- name: Send Deployment Success Webhook
if: success()
env:
VERSION: ${{ steps.determine-version.outputs.VERSION }}
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
curl -H "Content-Type: application/json" -X POST -d '{
"embeds": [{
"title": "배포 성공",
"color": 65280,
"fields": [
{
"name": "버전",
"value": "'"${VERSION}"'",
"inline": true
},
{
"name": "환경",
"value": "'"${EB_ENVIRONMENT_NAME}"'",
"inline": true
},
{
"name": "배포자",
"value": "'"${GITHUB_ACTOR}"'",
"inline": true
}
],
"url": "'"${WORKFLOW_URL}"'"
}]
}' ${{ env.DISCORD_WEBHOOK }}
- name: Send Deployment Failure Webhook
if: failure()
env:
VERSION: ${{ steps.determine-version.outputs.VERSION }}
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
curl -H "Content-Type: application/json" -X POST -d '{
"embeds": [{
"title": "배포 실패",
"color": 16711680,
"fields": [
{
"name": "버전",
"value": "'"${VERSION}"'",
"inline": true
},
{
"name": "환경",
"value": "'"${EB_ENVIRONMENT_NAME}"'",
"inline": true
},
{
"name": "배포자",
"value": "'"${GITHUB_ACTOR}"'",
"inline": true
}
],
"url": "'"${WORKFLOW_URL}"'"
}]
}' ${{ env.DISCORD_WEBHOOK }}