-
Notifications
You must be signed in to change notification settings - Fork 0
206 lines (177 loc) · 6.85 KB
/
Copy pathdeploy.yml
File metadata and controls
206 lines (177 loc) · 6.85 KB
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
name: Deploy (prod)
on:
push:
branches: [main]
workflow_dispatch:
concurrency:
group: deploy-prod
cancel-in-progress: false
permissions:
id-token: write # OIDC AssumeRole용
contents: read
env:
AWS_REGION: ap-northeast-2
ECR_REGISTRY: 092504162781.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: groute-ai-ecr
DEPLOY_ROLE_ARN: arn:aws:iam::092504162781:role/gh-actions-deploy-ai
EC2_TAG_KEY: Role
EC2_TAG_VALUE: ai
CONTAINER_NAME: groute-ai
HOST_PORT: "8000"
CONTAINER_PORT: "8000"
SSM_PREFIX: /groute/ai
jobs:
build-push:
name: Build & Push → ECR
runs-on: ubuntu-latest
outputs:
image_uri: ${{ steps.meta.outputs.image_uri }}
steps:
- uses: actions/checkout@v4
- name: Resolve image tag
id: meta
run: |
echo "image_uri=${ECR_REGISTRY}/${ECR_REPOSITORY}:${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.DEPLOY_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2
- uses: docker/setup-buildx-action@v3
- name: Build & push image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.image_uri }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
deploy:
name: Deploy to EC2 via SSM
needs: build-push
runs-on: ubuntu-latest
env:
IMAGE_URI: ${{ needs.build-push.outputs.image_uri }}
steps:
- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.DEPLOY_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Resolve target instance IDs (tag Role=ai)
id: targets
run: |
set -euo pipefail
IDS=$(aws ec2 describe-instances \
--filters "Name=tag:${EC2_TAG_KEY},Values=${EC2_TAG_VALUE}" \
"Name=instance-state-name,Values=running" \
--query 'Reservations[].Instances[].InstanceId' --output text)
if [ -z "${IDS}" ]; then
echo "::error::no running instances matched tag ${EC2_TAG_KEY}=${EC2_TAG_VALUE}"
exit 1
fi
echo "ids=${IDS}" >> "$GITHUB_OUTPUT"
echo "targets: ${IDS}"
- name: Render remote deploy script
run: |
set -euo pipefail
# EC2 위에서 실행될 스크립트. GitHub Actions 시점에 env 값이 전개됨.
cat > /tmp/remote-deploy.sh <<REMOTE
#!/usr/bin/env bash
set -euo pipefail
export AWS_DEFAULT_REGION="${AWS_REGION}"
IMAGE_URI="${IMAGE_URI}"
CONTAINER_NAME="${CONTAINER_NAME}"
SSM_PREFIX="${SSM_PREFIX}"
ECR_REGISTRY="${ECR_REGISTRY}"
HOST_PORT="${HOST_PORT}"
CONTAINER_PORT="${CONTAINER_PORT}"
echo "[1/5] ECR login"
aws ecr get-login-password --region "\$AWS_DEFAULT_REGION" \\
| docker login --username AWS --password-stdin "\$ECR_REGISTRY"
echo "[2/5] Pull image \$IMAGE_URI"
docker pull "\$IMAGE_URI"
echo "[3/5] Load env from SSM (\$SSM_PREFIX)"
ENV_FILE=\$(mktemp)
chmod 600 "\$ENV_FILE"
# SSM Parameter Store: /groute/ai/* → KEY=VALUE 형태로 env-file 생성
aws ssm get-parameters-by-path \\
--path "\$SSM_PREFIX/" --recursive --with-decryption \\
--query 'Parameters[].[Name,Value]' --output text \\
| while IFS=\$'\t' read -r name value; do
printf '%s=%s\n' "\${name##*/}" "\$value"
done > "\$ENV_FILE"
echo "[4/5] Replace container (stop & run)"
docker rm -f "\$CONTAINER_NAME" 2>/dev/null || true
docker run -d \\
--name "\$CONTAINER_NAME" \\
--restart unless-stopped \\
--log-driver json-file --log-opt max-size=10m --log-opt max-file=3 \\
-p "\${HOST_PORT}:\${CONTAINER_PORT}" \\
--env-file "\$ENV_FILE" \\
"\$IMAGE_URI"
rm -f "\$ENV_FILE"
echo "[5/5] Health check (/readyz)"
for i in \$(seq 1 30); do
if curl -fsS "http://127.0.0.1:\${HOST_PORT}/readyz" >/dev/null; then
echo "ready after \${i} tries"
break
fi
if [ "\$i" -eq 30 ]; then
echo "::error::health check failed"
docker logs --tail 200 "\$CONTAINER_NAME" || true
exit 1
fi
sleep 2
done
docker image prune -f || true
echo "deploy done: \$IMAGE_URI"
REMOTE
echo "--- rendered script ---"
cat /tmp/remote-deploy.sh
- name: Send SSM command
id: send
run: |
set -euo pipefail
# jq로 멀티라인 스크립트를 JSON 파라미터로 안전하게 인코딩
PARAMS=$(jq -Rs '{commands: [.]}' < /tmp/remote-deploy.sh)
CMD_ID=$(aws ssm send-command \
--document-name AWS-RunShellScript \
--targets "Key=tag:${EC2_TAG_KEY},Values=${EC2_TAG_VALUE}" \
--comment "deploy ${ECR_REPOSITORY}@${GITHUB_SHA::12}" \
--timeout-seconds 600 \
--cloud-watch-output-config CloudWatchOutputEnabled=true \
--parameters "$PARAMS" \
--query 'Command.CommandId' --output text)
echo "command_id=$CMD_ID" >> "$GITHUB_OUTPUT"
echo "CommandId: $CMD_ID"
- name: Wait & collect logs
env:
CMD_ID: ${{ steps.send.outputs.command_id }}
IDS: ${{ steps.targets.outputs.ids }}
run: |
set -euo pipefail
FAIL=0
for IID in $IDS; do
echo "::group::instance ${IID}"
# wait는 Success가 아닌 경우 non-zero로 끝나므로 || true
aws ssm wait command-executed --command-id "$CMD_ID" --instance-id "$IID" || true
STATUS=$(aws ssm get-command-invocation \
--command-id "$CMD_ID" --instance-id "$IID" \
--query 'Status' --output text)
echo "--- stdout ---"
aws ssm get-command-invocation \
--command-id "$CMD_ID" --instance-id "$IID" \
--query 'StandardOutputContent' --output text || true
echo "--- stderr ---"
aws ssm get-command-invocation \
--command-id "$CMD_ID" --instance-id "$IID" \
--query 'StandardErrorContent' --output text || true
echo "status=${STATUS}"
[ "$STATUS" = "Success" ] || FAIL=1
echo "::endgroup::"
done
exit $FAIL