#!/bin/bash set -euo pipefail # =========================================== # Re:Link Auto Deploy Script # Triggered by Gitea webhook on push to main # =========================================== APP_DIR="$HOME/relink" LOG_FILE="$APP_DIR/deploy.log" LOCK_FILE="$APP_DIR/deploy.lock" GITEA_REPO="http://39.117.244.52:3000/geonhee/Re_Link.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 services log "Waiting for services to be healthy..." sleep 15 # 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 ==="