#!/bin/bash # # Shahi Kitchen Production Deploy Script # Run as: sudo -u deploy /var/www/shahikitchen.se/scripts/deploy.sh # # This script supports two modes: # 1. Tarball mode (current primary method): Place new shahi.tar.gz in /tmp or /root and run # 2. Git mode (if you initialize git in the future) # set -euo pipefail APP_DIR="/var/www/shahikitchen.se" PM2_APP_NAME="shahikitchen" PORT=3001 echo "==========================================" echo " Shahi Kitchen - Production Deploy" echo " $(date)" echo "==========================================" cd "$APP_DIR" # --- Detect mode --- if [ -f "/tmp/shahi.tar.gz" ] || [ -f "/root/shahi.tar.gz" ]; then echo "[1/6] Tarball update detected" TARBALL="" if [ -f "/tmp/shahi.tar.gz" ]; then TARBALL="/tmp/shahi.tar.gz"; fi if [ -f "/root/shahi.tar.gz" ]; then TARBALL="/root/shahi.tar.gz"; fi echo "Using tarball: $TARBALL" echo "Stopping PM2 app (graceful)..." pm2 stop "$PM2_APP_NAME" || true echo "Backing up current .next (quick safety)..." rm -rf .next.bak 2>/dev/null || true cp -a .next .next.bak 2>/dev/null || true echo "Extracting new tarball..." tar --strip-components=1 -xzf "$TARBALL" echo "Cleaning shipped node_modules + cache..." rm -rf node_modules .next/cache 2>/dev/null || true echo "Running npm ci..." npm ci echo "Building..." npm run build elif git rev-parse --git-dir > /dev/null 2>&1; then echo "[1/6] Git update mode" pm2 stop "$PM2_APP_NAME" || true git fetch --all git reset --hard origin/main || git reset --hard origin/master npm ci npm run build else echo "ERROR: No tarball found in /tmp or /root, and no git repository." echo "Please either:" echo " - scp your new shahi.tar.gz to the server, or" echo " - Run: cp /path/to/shahi.tar.gz /tmp/shahi.tar.gz" exit 1 fi echo "[2/6] Dependencies and build complete" echo "[3/6] Starting / restarting PM2..." pm2 start ecosystem.config.cjs --only "$PM2_APP_NAME" || pm2 reload "$PM2_APP_NAME" --update-env || true pm2 save echo "[4/6] Reloading Nginx..." sudo nginx -t && sudo systemctl reload nginx echo "[5/6] Post-deploy health checks..." echo "PM2 status:" pm2 list | grep -E "shahikitchen|App name" || true echo "" echo "Testing local Next.js process..." curl -s --max-time 5 "http://127.0.0.1:${PORT}" | head -c 300 || echo "(first request may be slow)" echo "" echo "[6/6] Deploy finished successfully at $(date)" echo "==========================================" echo "Website should be live at: http://76.13.210.183" echo "=========================================="