-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy.sh
More file actions
77 lines (64 loc) · 2.23 KB
/
deploy.sh
File metadata and controls
77 lines (64 loc) · 2.23 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
#!/bin/bash
BLUE_ENV=".env.blue"
GREEN_ENV=".env.green"
BASE_COMPOSE="docker-compose.base.yml"
BLUE_COMPOSE="docker-compose.blue.yml"
GREEN_COMPOSE="docker-compose.green.yml"
if sudo docker ps --filter "name=mosu-server-blue" --format '{{.Names}}' | grep -q "mosu-server-blue"; then
CURRENT_ENV=$BLUE_ENV
CURRENT_CONTAINER="mosu-server-blue"
elif sudo docker ps --filter "name=mosu-server-green" --format '{{.Names}}' | grep -q "mosu-server-green"; then
CURRENT_ENV=$GREEN_ENV
CURRENT_CONTAINER="mosu-server-green"
else
echo "실행 중인 컨테이너가 없습니다."
exit 1
fi
CURRENT_PORT=$(grep SPRING_PORT $CURRENT_ENV | cut -d '=' -f2)
echo "현재 포트 : $CURRENT_PORT"
if [ "$CURRENT_PORT" == "8081" ]; then
TARGET_PORT=8082
TARGET_ENV=$GREEN_ENV
TARGET_COMPOSE=$GREEN_COMPOSE
PREV_CONTAINER="mosu-server-blue"
else
TARGET_PORT=8081
TARGET_ENV=$BLUE_ENV
TARGET_COMPOSE=$BLUE_COMPOSE
PREV_CONTAINER="mosu-server-green"
fi
echo "=== 배포 대상 ==="
echo "포트: $TARGET_PORT"
echo "env 파일: $TARGET_ENV"
echo "compose 파일: $TARGET_COMPOSE"
echo "이전 컨테이너: $PREV_CONTAINER"
sudo docker compose -f $BASE_COMPOSE -f $TARGET_COMPOSE --env-file $TARGET_ENV up -d --build
check_heartbeat() {
local url=$1
local retries=100
local wait=3
local count=0
while [ $count -lt $retries ]; do
if curl --silent --fail "$url" > /dev/null; then
return 0
fi
echo "헬스 체크 실패, 재시도 중... ($((count+1))/$retries)"
sleep $wait
((count++))
done
return 1
}
HEALTH_URL="http://localhost:${TARGET_PORT}/api/v1/actuator/health"
echo "새 컨테이너 헬스 체크 시작: $HEALTH_URL"
if ! check_heartbeat "$HEALTH_URL"; then
echo "❌ 새 컨테이너 헬스 체크 실패. 배포 중단."
exit 1
fi
echo "✅ 새 컨테이너 헬스 체크 성공."
sudo sed -i -E "s|(reverse_proxy /api/v1/\* localhost:)[0-9]+|\1${TARGET_PORT}|" /etc/caddy/Caddyfile
sudo systemctl reload caddy
echo "Caddy 라우팅 ${TARGET_PORT} 로 변경 완료."
echo "이전 컨테이너 $PREV_CONTAINER 중지 및 제거"
sudo docker stop $PREV_CONTAINER || true
sudo docker rm $PREV_CONTAINER || true
echo "✅ 배포 완료! 현재 운영 포트: $TARGET_PORT"