-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
81 lines (66 loc) · 2.09 KB
/
Copy pathdeploy.sh
File metadata and controls
81 lines (66 loc) · 2.09 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
#!/bin/bash
set -e
APP_DIR="/home/ubuntu/app"
NGINX_CONF="$APP_DIR/nginx.conf"
COMPOSE_FILE="$APP_DIR/docker-compose.prod.yaml"
run_compose() {
cd "$APP_DIR" && docker compose -f "$COMPOSE_FILE" "$@"
}
echo "======================================"
echo " Blue-Green 무중단 배포 시작"
echo "======================================"
# head -n1 추가 → 여러 줄 매치 방지
CURRENT=$(grep "server app-" "$NGINX_CONF" 2>/dev/null \
| grep -oE "app-(blue|green)" \
| head -n1 \
|| echo "")
if [ "$CURRENT" = "app-blue" ]; then
NEXT="app-green"
NEXT_PORT="8081"
INACTIVE="app-blue"
else
NEXT="app-blue"
NEXT_PORT="8080"
INACTIVE="app-green"
fi
echo "▶ 현재 활성: ${CURRENT:-없음(최초 배포)}"
echo "▶ 배포 대상: $NEXT"
echo "[1/5] 최신 이미지 pull..."
run_compose pull "$NEXT"
echo "[2/5] $NEXT 컨테이너 실행..."
run_compose up -d --no-deps "$NEXT"
echo "[3/5] 헬스체크 대기..."
echo " 앱 초기 기동 대기 (20초)..."
sleep 20
HEALTH_URL="http://localhost:$NEXT_PORT/actuator/health"
STATUS="000"
for i in {1..20}; do
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$HEALTH_URL" || true)
if [ "$STATUS" = "200" ]; then
echo "✅ 헬스체크 통과 (${i}회 시도)"
break
fi
echo " 대기 중... ($i/20) status=$STATUS"
sleep 3
done
if [ "$STATUS" != "200" ] && [ "$STATUS" != "403" ]; then
echo "❌ 헬스체크 실패 — 롤백"
run_compose stop "$NEXT"
exit 1
fi
echo "[4/5] Nginx를 $NEXT 로 전환..."
# sed 치환 후 실제로 변경됐는지 검증
sed -i "s/server $INACTIVE:8080/server $NEXT:8080/g" "$NGINX_CONF"
if ! grep -q "server $NEXT:8080" "$NGINX_CONF"; then
echo "❌ nginx.conf 치환 실패 — 롤백"
run_compose stop "$NEXT"
exit 1
fi
docker compose -f "$COMPOSE_FILE" restart nginx
echo "✅ Nginx restart 완료"
echo "[5/5] $INACTIVE 컨테이너 종료..."
run_compose stop "$INACTIVE"
docker image prune -f
echo "======================================"
echo "✅ 배포 완료: $NEXT 활성화"
echo "======================================"