Files
web-page-backend/scripts/deploy.sh
gahusb 80ccb20f99 fix(deploy): docker rm -f로 컨테이너 강제 제거 후 빌드
docker compose down은 다른 프로젝트명으로 생성된 컨테이너를 인식 못함.
개별 docker rm -f로 확실하게 이름 충돌 제거.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-06 21:56:46 +09:00

81 lines
2.5 KiB
Bash

#!/bin/bash
set -euo pipefail
# ── 동시 배포 방지 (flock) ──
exec 200>/tmp/deploy.lock
flock -n 200 || { echo "Deploy already running, skipping"; exit 0; }
# 1. 자동 감지: Docker 컨테이너 내부인가?
if [ -d "/repo" ] && [ -d "/runtime" ]; then
echo "Detected Docker Container environment."
SRC="/repo"
DST="/runtime"
else
# 2. Host 환경: .env 로드 시도
if [ -f ".env" ]; then
echo "Loading .env file..."
set -a; source .env; set +a
fi
SRC="${REPO_PATH:-$(pwd)}"
DST="${RUNTIME_PATH:-/volume1/docker/webpage}"
if [ -z "$DST" ]; then
echo "Error: RUNTIME_PATH is not set."
exit 1
fi
fi
echo "Source: $SRC"
echo "Target: $DST"
git config --global --add safe.directory "$SRC"
cd "$SRC"
git fetch --all --prune
git pull --ff-only
# ── 릴리즈 백업 (롤백용) ──
TAG="$(date +%Y%m%d-%H%M%S)"
BACKUP_DIR="$DST/.releases/$TAG"
mkdir -p "$BACKUP_DIR.tmp"
rsync -a --delete \
--exclude ".releases" \
"$DST/" "$BACKUP_DIR.tmp/"
mv "$BACKUP_DIR.tmp" "$BACKUP_DIR"
# 오래된 릴리즈 정리 (최근 5개만 유지)
ls -dt "$DST/.releases"/*/ 2>/dev/null | tail -n +6 | xargs -r rm -rf
# ── 소스 → 운영 반영 ──
bash "$SRC/scripts/deploy-nas.sh"
# ── 변경된 서비스만 재빌드 (deployer 제외 — 자기 자신을 재빌드하면 스크립트 중단됨) ──
cd "$DST"
# 이름 충돌 방지: 기존 컨테이너 강제 제거 후 빌드
for cname in lotto-backend stock-lab music-lab blog-lab realestate-lab travel-proxy lotto-frontend; do
docker rm -f "$cname" 2>/dev/null || true
done
docker compose up -d --build backend travel-proxy stock-lab music-lab blog-lab realestate-lab frontend
docker exec lotto-frontend nginx -s reload 2>/dev/null || true
# ── 배포 후 헬스체크 ──
echo "Waiting for services to start..."
sleep 5
HEALTH_OK=true
# 컨테이너 내부에서는 서비스명 + 내부포트(8000)로 접근
for endpoint in "http://backend:8000/health" "http://stock-lab:8000/health" "http://travel-proxy:8000/health" "http://music-lab:8000/health" "http://blog-lab:8000/health" "http://realestate-lab:8000/health"; do
if ! curl -sf --max-time 10 --retry 2 --retry-delay 3 "$endpoint" > /dev/null 2>&1; then
echo "HEALTH_FAIL: $endpoint"
HEALTH_OK=false
fi
done
if [ "$HEALTH_OK" = false ]; then
echo "DEPLOY_WARN: Some services failed health check. Consider rollback: $TAG"
else
echo "DEPLOY_OK $TAG"
fi