Files
startover/deploy/deploy.sh
T
Johngreen 0c705d00e2 fix: 프로덕션 DB 스키마 미적용 및 PrismaClient 싱글톤 수정
- deploy.sh에 prisma db push 단계 추가 (배포 시 스키마 자동 적용)
- Dockerfile에 Prisma schema 런타임 이미지 복사 추가
- PrismaClient 프로덕션 싱글톤 캐시 활성화 (커넥션 풀 소진 방지)
2026-03-08 23:26:29 +09:00

82 lines
2.2 KiB
Bash

#!/bin/bash
set -euo pipefail
# ===========================================
# Startover Auto Deploy Script
# Triggered by Gitea webhook on push to main
# ===========================================
APP_DIR="$HOME/startover"
LOG_FILE="$APP_DIR/deploy.log"
LOCK_FILE="$APP_DIR/deploy.lock"
GITEA_REPO="http://39.117.244.52:3000/geonhee/startover.git"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# Prevent concurrent deploys
if [ -f "$LOCK_FILE" ]; then
LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null)
if kill -0 "$LOCK_PID" 2>/dev/null; then
log "ERROR: Deploy already running (PID: $LOCK_PID). Skipping."
exit 1
fi
rm -f "$LOCK_FILE"
fi
echo $$ > "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT
log "=== Deploy started ==="
# Navigate to app directory
cd "$APP_DIR"
# Clone or pull
if [ ! -d "$APP_DIR/repo/.git" ]; then
log "Cloning repository..."
git clone "$GITEA_REPO" "$APP_DIR/repo"
cd "$APP_DIR/repo"
else
cd "$APP_DIR/repo"
log "Pulling latest changes..."
git fetch origin
git reset --hard origin/main
fi
log "Current commit: $(git log --oneline -1)"
# Copy env file
if [ -f "$APP_DIR/.env.production" ]; then
cp "$APP_DIR/.env.production" "$APP_DIR/repo/.env.production"
fi
# Build and deploy
log "Building and deploying with Docker Compose..."
docker compose -f docker-compose.prod.yml --env-file .env.production up -d --build --remove-orphans 2>&1 | tee -a "$LOG_FILE"
# Wait for postgres to be healthy
log "Waiting for postgres to be healthy..."
sleep 10
# Apply database schema
log "Applying database schema (prisma db push)..."
docker compose -f docker-compose.prod.yml --env-file .env.production exec -T web npx prisma db push --schema=./prisma/schema.prisma --skip-generate --accept-data-loss 2>&1 | tee -a "$LOG_FILE"
log "Database schema applied."
# Wait for services to stabilize
log "Waiting for services to stabilize..."
sleep 5
# Health check
if curl -sf http://localhost/health > /dev/null 2>&1; then
log "Health check PASSED"
else
log "WARNING: Health check failed - services may still be starting"
fi
# Cleanup old images
docker image prune -f >> "$LOG_FILE" 2>&1
log "=== Deploy completed ==="