-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeploy.sh
More file actions
86 lines (67 loc) · 3.49 KB
/
deploy.sh
File metadata and controls
86 lines (67 loc) · 3.49 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
#!/usr/bin/env bash
###############################################################################
# Blue/Green 배포 스크립트 (EC2 내부 실행, 단일 인스턴스용)
###############################################################################
set -eEuo pipefail # -E: ERR 트랩 포함
############################ 0. 공통 변수 #####################################
PROJECT_NAME="aibe1_finalproject_lastdance_be"
APP_DIR="/home/ubuntu/${PROJECT_NAME}"
DOCKER_APP_DIR="${APP_DIR}/docker"
MONITORING_DIR="${APP_DIR}/monitoring"
COMPOSE_FILE="${DOCKER_APP_DIR}/docker-compose.yml"
NGINX_CONF="/etc/nginx/sites-available/lastdance-app.conf"
BLUE_PORT=8080; GREEN_PORT=8081
BLUE_SERVICE="blue-app"; GREEN_SERVICE="green-app"
COMPOSE="docker-compose"
command -v docker compose &>/dev/null && COMPOSE="docker compose"
############################ 0-1. 고아 컨테이너 처리 ##########################
DEPLOY_OK=false
cleanup() {
if [[ "$DEPLOY_OK" == false ]]; then
echo "[CLEANUP] 롤백 – $NEW_APP_SERVICE 제거"
$COMPOSE -f "$COMPOSE_FILE" rm -fs "$NEW_APP_SERVICE" 2>/dev/null || true
fi
}
trap cleanup EXIT ERR INT # 어떤 종료라도 cleanup 실행
############################ 0-2. 이미지 풀링 ################################
echo "── GHCR 로그인 & 최신 이미지 Pull ──"
FULL_IMAGE_NAME="${GHCR_IMAGE_NAME}:${IMAGE_TAG}"
echo "$GHCR_TOKEN" | docker login --username "$GHCR_USER" --password-stdin ghcr.io
docker pull "$FULL_IMAGE_NAME"
############################ 0-3. 네트워크 ###################################
docker network create app-monitor-network || true
############################ 1. 현재 활성 포트 ################################
CURRENT_APP_PORT=$(grep -A1 'upstream current_app' "$NGINX_CONF" \
| tail -n1 | grep -oP '127\.0\.0\.1:\K[0-9]+')
[[ -z "$CURRENT_APP_PORT" ]] && CURRENT_APP_PORT=$BLUE_PORT
if [[ "$CURRENT_APP_PORT" == "$BLUE_PORT" ]]; then
NEW_APP_PORT=$GREEN_PORT; NEW_APP_SERVICE=$GREEN_SERVICE; OLD_APP_SERVICE=$BLUE_SERVICE
else
NEW_APP_PORT=$BLUE_PORT; NEW_APP_SERVICE=$BLUE_SERVICE; OLD_APP_SERVICE=$GREEN_SERVICE
fi
echo "새 서비스: $NEW_APP_SERVICE (포트 $NEW_APP_PORT)"
############################ 2. 새 컨테이너 기동 ##############################
export IMAGE_URI="$FULL_IMAGE_NAME" # docker-compose.yml 에서 사용
$COMPOSE -f "$COMPOSE_FILE" rm -fs "$NEW_APP_SERVICE" || true
echo "컨테이너 기동…"
# (compose v2.21+ 사용 시 주석 해제해 헬스체크 통합)
$COMPOSE -f "$COMPOSE_FILE" up -d --wait -t 180 "$NEW_APP_SERVICE"
############################ 3. Nginx 스위치 ##################################
sudo sed -E -i '/upstream current_app/,+1 s/127\.0\.0\.1:[0-9]+/127.0.0.1:'"$NEW_APP_PORT"'/' \
"$NGINX_CONF"
sudo nginx -t
sudo systemctl reload nginx
echo "Nginx가 $NEW_APP_SERVICE 로 전환됨"
$COMPOSE -f "$COMPOSE_FILE" stop "$OLD_APP_SERVICE" || true
$COMPOSE -f "$COMPOSE_FILE" rm -fs "$OLD_APP_SERVICE" || true
DEPLOY_OK=true # ★ 여기서 성공 플래그 ON
echo "🎉 Blue/Green 전환 완료"
############################ 4. 모니터링 스택 재배포 ##########################
cd "$MONITORING_DIR"
echo "→ Rendering Alertmanager config with SLACK_WEBHOOK_URL"
envsubst < prometheus/alertmanager.yml \
> prometheus/alertmanager.rendered.yml
# 2) 치환된 파일로 compose 실행
$COMPOSE -f monitoring-compose.yml down || true
$COMPOSE -f monitoring-compose.yml up -d
echo "✅ 모니터링 스택 기동 완료"