Skip to content

Commit 17f4b72

Browse files
authored
Merge pull request #12 from next-engineer/feature/container
CICD Test
2 parents e7597b2 + 6b53826 commit 17f4b72

2 files changed

Lines changed: 163 additions & 0 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: Deploy to Amazon ECR & EC2
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
7+
env:
8+
AWS_REGION: ap-northeast-2
9+
ECR_REPOSITORY: intune-chat-repo
10+
CONTAINER_NAME: intune-chat
11+
EC2_INSTANCE_IDS: "i-08100c71ecc821618,i-07ee03b6f1f176e41"
12+
DOCKER_RUN_ARGS: >-
13+
-p 8081:8081
14+
--restart unless-stopped
15+
16+
jobs:
17+
deploy:
18+
name: Build → Push(ECR) → Deploy(EC2 Private)
19+
runs-on: ubuntu-latest
20+
environment: production
21+
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
26+
- name: Configure AWS credentials
27+
uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
28+
with:
29+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
30+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
31+
aws-region: ${{ env.AWS_REGION }}
32+
33+
- name: Login to Amazon ECR
34+
id: login-ecr
35+
uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a
36+
37+
- name: Build, tag, and push image to Amazon ECR
38+
id: build-image
39+
env:
40+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
41+
IMAGE_TAG: ${{ github.sha }}
42+
run: |
43+
set -euo pipefail
44+
docker build -t "$ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG" .
45+
docker push "$ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG"
46+
echo "image=$ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG" >> "$GITHUB_OUTPUT"
47+
48+
# === 여기부터 Private EC2로 배포(SSM RunCommand) ===
49+
- name: Build SSM command script
50+
id: ssm-script
51+
env:
52+
AWS_REGION: ${{ env.AWS_REGION }}
53+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
54+
IMAGE_URI: ${{ steps.build-image.outputs.image }}
55+
CONTAINER_NAME: ${{ env.CONTAINER_NAME }}
56+
DOCKER_RUN_ARGS: ${{ env.DOCKER_RUN_ARGS }}
57+
run: |
58+
set -euo pipefail
59+
cat > deploy.sh <<'EOF'
60+
#!/usr/bin/env bash
61+
set -euo pipefail
62+
63+
echo "[1/5] ECR 로그인"
64+
aws ecr get-login-password --region "${AWS_REGION}" \
65+
| docker login --username AWS --password-stdin "${ECR_REGISTRY}"
66+
67+
echo "[2/5] 새 이미지 Pull: ${IMAGE_URI}"
68+
docker pull "${IMAGE_URI}"
69+
70+
echo "[3/5] 기존 컨테이너 정리: ${CONTAINER_NAME}"
71+
# 동일 이름 컨테이너가 있으면 중지/삭제
72+
if docker ps -a --format '{{.Names}}' | grep -wq "${CONTAINER_NAME}"; then
73+
docker rm -f "${CONTAINER_NAME}" || true
74+
fi
75+
76+
echo "[4/5] 오래된 이미지 가비지 정리(선택)"
77+
docker image prune -f || true
78+
79+
echo "[5/5] 새 컨테이너 기동"
80+
docker run -d --name "${CONTAINER_NAME}" ${DOCKER_RUN_ARGS} "${IMAGE_URI}"
81+
82+
echo "DONE"
83+
EOF
84+
85+
python3 - <<'PY'
86+
import json
87+
script = open('deploy.sh','r',encoding='utf-8').read()
88+
89+
cmd = ["bash","-lc", script]
90+
print(f"commands={json.dumps(cmd)}")
91+
PY >> $GITHUB_OUTPUT
92+
93+
- name: Send SSM RunCommand to EC2 (by InstanceIds or by Tag)
94+
id: ssm-deploy
95+
run: |
96+
set -euo pipefail
97+
98+
TARGETS_JSON="[]"
99+
if [ -n "${{ env.EC2_INSTANCE_IDS }}" ]; then
100+
101+
# InstanceId로 지정
102+
IFS=',' read -ra IDS <<< "${{ env.EC2_INSTANCE_IDS }}"
103+
ARR=""
104+
for id in "${IDS[@]}"; do
105+
ARR="${ARR}{\"Key\":\"InstanceIds\",\"Values\":[\"$id\"]},"
106+
done
107+
TARGETS_JSON="["${ARR%,}"]"
108+
elif [ -n "${{ env.EC2_TAG_KEY || '' }}" ] && [ -n "${{ env.EC2_TAG_VALUE || '' }}" ]; then
109+
# 태그로 지정
110+
TARGETS_JSON=$(cat <<JSON
111+
[
112+
{"Key":"tag:${{ env.EC2_TAG_KEY }}","Values":["${{ env.EC2_TAG_VALUE }}"]}
113+
]
114+
JSON
115+
)
116+
else
117+
echo "EC2 타깃(EC2_INSTANCE_IDS 또는 EC2_TAG_KEY/EC2_TAG_VALUE)을 설정하세요." >&2
118+
exit 1
119+
fi
120+
121+
aws ssm send-command \
122+
--document-name "AWS-RunShellScript" \
123+
--comment "Pull from ECR and restart container" \
124+
--targets "${TARGETS_JSON}" \
125+
--parameters '{"commands": '${{ steps.ssm-script.outputs.commands }}'}' \
126+
--timeout-seconds 1800 \
127+
--max-concurrency "1" \
128+
--max-errors "0" \
129+
--region "${{ env.AWS_REGION }}" \
130+
--output json > send-command.json
131+
132+
COMMAND_ID=$(jq -r '.Command.CommandId' send-command.json)
133+
echo "command-id=${COMMAND_ID}" >> "$GITHUB_OUTPUT"
134+
135+
- name: Wait for SSM command to finish
136+
run: |
137+
set -euo pipefail
138+
COMMAND_ID="${{ steps.ssm-deploy.outputs.command-id }}"
139+
echo "Waiting for SSM Command $COMMAND_ID ..."
140+
# 간단 폴링
141+
for i in $(seq 1 120); do
142+
STATUS=$(aws ssm list-commands --command-id "$COMMAND_ID" --query "Commands[0].Status" --output text --region "${{ env.AWS_REGION }}")
143+
echo "SSM Status: $STATUS"
144+
case "$STATUS" in
145+
Success) exit 0 ;;
146+
Failed|Cancelled|TimedOut) echo "SSM failed: $STATUS"; exit 1 ;;
147+
esac
148+
sleep 5
149+
done
150+
echo "Timeout waiting SSM command"
151+
exit 1

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Use an official OpenJDK runtime as a parent image
2+
FROM openjdk:17-jdk-slim
3+
4+
# Set the working directory inside the container
5+
WORKDIR /app
6+
7+
COPY build/libs/*.jar app.jar
8+
9+
EXPOSE 8081
10+
11+
# Run the application
12+
ENTRYPOINT ["java", "-jar", "app.jar"]

0 commit comments

Comments
 (0)