Files
2026-06-08 16:27:17 +07:00

86 lines
3.2 KiB
Bash

#!/bin/bash
# ═══════════════════════════════════════════════════════════
# Harat's Booking — Server-side deploy script
# ═══════════════════════════════════════════════════════════
#
# Usage:
# bash deploy-remote.sh <archive> <tag>
# bash deploy-remote.sh harats-1.0.0+20260608.tar.gz 1.0.0+20260608
# ═══════════════════════════════════════════════════════════
set -euo pipefail
ARCHIVE="${1:?Missing archive name}"
TAG="${2:?Missing version tag}"
DEPLOY_DIR="/apps/harats"
RELEASES_DIR="$DEPLOY_DIR/releases"
COMPOSE_FILE="docker-compose.prod.yml"
PROJECT_NAME="harats"
echo ""
echo "═══ Harat's Deploy: $TAG ═══"
echo ""
# ── 1. Extract archive ──
echo "[1/5] Extracting $ARCHIVE..."
cd "$DEPLOY_DIR"
tar -xzf "$RELEASES_DIR/$ARCHIVE" --overwrite
chmod +x deploy/deploy-remote.sh 2>/dev/null || true
# ── 2. Ensure directories ──
echo "[2/5] Ensuring directories..."
mkdir -p "$DEPLOY_DIR/envs/prod"
mkdir -p /apps/harats-storage/logs
# Create .env from example if missing
if [ ! -f "$DEPLOY_DIR/envs/prod/.env" ] && [ -f "$DEPLOY_DIR/envs/prod/.env.example" ]; then
cp "$DEPLOY_DIR/envs/prod/.env.example" "$DEPLOY_DIR/envs/prod/.env"
echo " ⚠️ Created envs/prod/.env from example — EDIT SECRETS before use!"
fi
# ── 3. Build Docker image ──
echo "[3/5] Building Docker image..."
DOCKER_TAG="${TAG//+/-}"
export BUILD_VERSION="$DOCKER_TAG"
export BUILD_DATE="$(date -Iseconds)"
echo " → harats-server:$DOCKER_TAG"
docker compose -f "$DEPLOY_DIR/$COMPOSE_FILE" build --no-cache \
--build-arg BUILD_VERSION="$TAG" \
--build-arg BUILD_DATE="$BUILD_DATE" \
server
docker tag "harats-server:$DOCKER_TAG" harats-server:latest 2>/dev/null || true
# ── 4. Start services ──
echo "[4/5] Starting services..."
BUILD_VERSION="$DOCKER_TAG" docker compose -p "$PROJECT_NAME" -f "$DEPLOY_DIR/$COMPOSE_FILE" up -d --remove-orphans --no-build 2>&1
# ── 5. Run migrations + Health check ──
echo "[5/5] Running migrations & health check..."
sleep 5
docker exec harats-server-1 npx -y prisma migrate deploy --schema /app/prisma/schema.prisma 2>&1 || echo " ⚠️ Migration warning (may be first run)"
sleep 5
HEALTH=$(curl -sf "http://localhost:3001/api/health" 2>/dev/null || echo '{"status":"FAIL"}')
if echo "$HEALTH" | grep -q '"status":"ok"'; then
echo " ✅ Server healthy"
else
echo " ❌ Health check FAILED"
docker compose -p "$PROJECT_NAME" -f "$DEPLOY_DIR/$COMPOSE_FILE" logs --tail=20 server 2>/dev/null || true
fi
# ── Finalize ──
echo "$TAG" > "$DEPLOY_DIR/.current-version"
date -Iseconds > "$DEPLOY_DIR/.last-deploy"
# ── Cleanup old releases ──
cd "$RELEASES_DIR"
ls -t harats-*.tar.gz 2>/dev/null | tail -n +6 | xargs -r rm -v
docker image prune -f --filter "until=168h" 2>/dev/null || true
echo ""
echo "✅ Deployed: Harat's v$TAG"
echo "═══ Deploy complete ═══"