Files
web-page-backend/scripts/deploy.sh
gahusb a9f38e1248 fix(deploy): bring up infra services (redis) via separate up -d step
Previous deploy.sh only started services listed in BUILD_TARGETS, so the
newly-added redis service never came up after the SP-1 commit pushed to
NAS. Split image-based infra (redis) into INFRA_SERVICES and call
'docker compose up -d $INFRA_SERVICES' after the BUILD_TARGETS rebuild.

stop/rm is intentionally skipped for INFRA_SERVICES so AOF data
(/runtime/redis-data) survives each deploy cycle. Future infra services
(prometheus, grafana, ...) can join the same list.

Also add redis to HEALTH_ENDPOINTS so deployer's docker-inspect health
check waits for redis to report healthy before declaring DEPLOY_OK.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 23:47:51 +09:00

141 lines
5.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -euo pipefail
# ── docker / compose / buildkit timeout 늘리기 ──
# NAS Celeron J4025에서 pip install·chromium 다운로드 등 무거운 RUN step이
# 기본 timeout(2분)에 걸려 webhook 자동 배포가 "DeadlineExceeded"로 끝나는 일이
# 있어 10분으로 상향. 호스트 셸 + deployer 컨테이너 둘 다에 적용됨.
export COMPOSE_HTTP_TIMEOUT=600
export DOCKER_CLIENT_TIMEOUT=600
export BUILDKIT_STEP_LOG_MAX_SIZE=-1
# ── 동시 배포 방지 (flock) ──
exec 200>/tmp/deploy.lock
flock -n 200 || { echo "Deploy already running, skipping"; exit 0; }
# ── 서비스 목록 (한 곳에서만 관리) ──
# docker compose 서비스명 (deployer 제외 — 자기 자신을 재빌드하면 스크립트 중단)
BUILD_TARGETS="lotto travel-proxy stock music-lab insta-lab realestate-lab agent-office personal packs-lab frontend"
# 컨테이너 이름 (고아 정리용 — blog-lab은 폐기 대상으로 정리 리스트에 유지)
CONTAINER_NAMES="lotto stock music-lab insta-lab blog-lab realestate-lab agent-office personal packs-lab travel-proxy frontend"
# Infra 서비스 (image-based, 영속 데이터 보존을 위해 stop/rm 없이 up만)
INFRA_SERVICES="redis"
# 헬스체크 대상
HEALTH_ENDPOINTS="lotto stock travel-proxy music-lab insta-lab realestate-lab agent-office personal packs-lab redis"
# data 디렉토리 (packs-lab은 별도 media/packs 사용)
DATA_DIRS="music stock insta realestate agent-office personal"
# 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
# ── git pull 후 파일 소유권 복원 (deployer가 root로 실행되므로) ──
DEPLOY_UID="${PUID:-1026}"
DEPLOY_GID="${PGID:-100}"
chown -R "${DEPLOY_UID}:${DEPLOY_GID}" "$SRC"
# ── 릴리즈 백업 (롤백용) ──
TAG="$(date +%Y%m%d-%H%M%S)"
BACKUP_DIR="$DST/.releases/$TAG"
mkdir -p "$BACKUP_DIR.tmp"
rsync -a --delete \
--exclude ".releases" \
--exclude "data" \
"$DST/" "$BACKUP_DIR.tmp/"
mv "$BACKUP_DIR.tmp" "$BACKUP_DIR"
# 오래된 릴리즈 정리 (최근 5개만 유지, 권한 문제 우회)
ls -dt "$DST/.releases"/*/ 2>/dev/null | tail -n +6 | while read -r old_dir; do
chmod -R u+rwX "$old_dir" 2>/dev/null || true
rm -rf "$old_dir" 2>/dev/null || true
done
# ── 소스 → 운영 반영 ──
bash "$SRC/scripts/deploy-nas.sh"
# ── data 디렉토리 보장 (볼륨 마운트 실패 방지) ──
mkdir -p "$DST/data"
for d in $DATA_DIRS; do
mkdir -p "$DST/data/$d"
done
# packs-lab media 디렉토리 (DSM 공유 + admin upload target)
mkdir -p "$DST/media/packs"
chown "${DEPLOY_UID}:${DEPLOY_GID}" "$DST/media/packs" 2>/dev/null || true
# ── 서비스 재빌드 (deployer 제외) ──
cd "$DST"
# 1) compose가 관리하는 컨테이너 정리
docker compose stop $BUILD_TARGETS 2>/dev/null || true
docker compose rm -f $BUILD_TARGETS 2>/dev/null || true
# 2) Synology NAS 고아 컨테이너(해시 prefix 포함) 추가 정리
for cname in $CONTAINER_NAMES; do
docker rm -f "$cname" 2>/dev/null || true
done
# 3) 재빌드 및 시작
docker compose up -d --build $BUILD_TARGETS
docker exec frontend nginx -s reload 2>/dev/null || true
# 4) Infra 서비스 보장 (이미 떠 있으면 no-op, 없으면 시작 — 영속 데이터 보존)
docker compose up -d $INFRA_SERVICES
# ── 배포 후 헬스체크 ──
# Docker compose의 healthcheck 블록이 이미 모든 컨테이너에 정의되어 있으므로
# `docker inspect`로 컨테이너 health state를 직접 조회. 이 방식은
# (a) deployer 컨테이너 내부에서도 호스트에서도 동일하게 동작
# (b) 호스트네임 DNS 해석에 의존하지 않음 (호스트 셸에서는 'lotto' 등 미해석)
echo "Waiting for services to become healthy..."
HEALTH_OK=true
for svc in $HEALTH_ENDPOINTS; do
health="starting"
# 최대 60초 (5초×12) 동안 starting → healthy 전이 대기
for _ in $(seq 1 12); do
health=$(docker inspect --format='{{.State.Health.Status}}' "$svc" 2>/dev/null || echo "missing")
if [ "$health" = "healthy" ] || [ "$health" = "unhealthy" ] || [ "$health" = "missing" ]; then
break
fi
sleep 5
done
if [ "$health" != "healthy" ]; then
echo "HEALTH_FAIL: $svc (state=$health)"
HEALTH_OK=false
fi
done
if [ "$HEALTH_OK" = false ]; then
echo "DEPLOY_FAIL: Some services failed health check. Backup available: $TAG"
exit 1
else
echo "DEPLOY_OK $TAG"
fi