Skip to content

Commit d62d1ab

Browse files
committed
cd: blue/green 배포 기반 CD 파이프라인 구축
1 parent 90d0ddb commit d62d1ab

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

.github/workflows/cd.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: CD - Blue Green Deploy
2+
3+
on:
4+
push:
5+
branches: [ release ]
6+
7+
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
# 1. 소스 체크아웃
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
# 2. Docker Hub 로그인
17+
- name: Login to DockerHub
18+
uses: docker/login-action@v3
19+
with:
20+
username: ${{ secrets.DOCKERHUB_USERNAME }}
21+
password: ${{ secrets.DOCKERHUB_TOKEN }}
22+
23+
# 3. Docker 이미지 빌드 & 푸시
24+
- name: Build & Push Docker Image
25+
run: |
26+
docker build -t heygeeji/ncb-backend:latest .
27+
docker push heygeeji/ncb-backend:latest
28+
29+
# 4. EC2 배포 (Blue/Green + Rollback)
30+
- name: Deploy to EC2
31+
uses: appleboy/ssh-action@v1.0.3
32+
with:
33+
host: ${{ secrets.EC2_HOST }}
34+
username: ec2-user
35+
key: ${{ secrets.EC2_SSH_KEY }}
36+
script: |
37+
set -e
38+
cd ~/deploy
39+
40+
echo "🔍 Detecting active container..."
41+
42+
if docker ps --filter "name=concert-spring-blue" --format "{{.Status}}" | grep -q healthy; then
43+
ACTIVE=blue
44+
TARGET=green
45+
PORT=8082
46+
else
47+
ACTIVE=green
48+
TARGET=blue
49+
PORT=8081
50+
fi
51+
52+
echo "🟦 Active: $ACTIVE"
53+
echo "🟩 Deploy target: $TARGET"
54+
55+
echo "📥 Pull latest image"
56+
docker compose -f docker-compose-prod.yml pull spring-$TARGET
57+
58+
echo "🚀 Start spring-$TARGET"
59+
docker compose -f docker-compose-prod.yml up -d spring-$TARGET
60+
61+
echo "⏳ Waiting for health check on $PORT..."
62+
for i in {1..20}; do
63+
if curl -sf http://localhost:$PORT/actuator/health > /dev/null; then
64+
echo "✅ spring-$TARGET is healthy"
65+
exit 0
66+
fi
67+
echo "⏳ still waiting..."
68+
sleep 5
69+
done
70+
71+
echo "❌ Health check failed for spring-$TARGET - rolling back"
72+
docker compose -f docker-compose-prod.yml stop spring-$TARGET
73+
exit 1

0 commit comments

Comments
 (0)