/
var
/
www
/
html
/
pesentiket.com
/
scripts
/
Upload FileeE
HOME
#!/bin/bash # ============================================================ # PesenTiket — Deploy Frontend # Usage: ./deploy-frontend.sh # ============================================================ set -e RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m' BLUE='\033[0;34m'; CYAN='\033[0;36m'; NC='\033[0m' FE_DIR="/var/www/html/pesentiket.com" LOG="/var/log/pesentiket/deploy-frontend.log" log() { echo -e "${CYAN}[$(date '+%H:%M:%S')]${NC} $1" | tee -a "$LOG"; } success() { echo -e "${GREEN}✅ $1${NC}" | tee -a "$LOG"; } warn() { echo -e "${YELLOW}⚠️ $1${NC}" | tee -a "$LOG"; } error() { echo -e "${RED}❌ $1${NC}" | tee -a "$LOG"; exit 1; } step() { echo -e "\n${BLUE}━━━ STEP $1: $2 ━━━${NC}" | tee -a "$LOG"; } mkdir -p /var/log/pesentiket echo "" >> "$LOG" echo "============================================================" >> "$LOG" log "🚀 Deploy Frontend — $(date '+%Y-%m-%d %H:%M:%S')" echo "============================================================" >> "$LOG" step 1 "Validasi" cd "$FE_DIR" || error "Direktori tidak ditemukan: $FE_DIR" [[ -f "package.json" ]] || error "package.json tidak ada — upload kode dulu" [[ -f ".env.local" ]] || error ".env.local tidak ada" success "OK" step 2 "Cek Node.js" which node > /dev/null 2>&1 || error "Node.js tidak terinstall" log "Node: $(node --version) | npm: $(npm --version)" success "OK" step 3 "Backup Build Lama" if [[ -f ".next/BUILD_ID" ]]; then OLD_BUILD=$(cat .next/BUILD_ID) cp -r .next .next.bak 2>/dev/null && log "Backup build $OLD_BUILD OK" || warn "Gagal backup" else warn "Tidak ada build lama" fi step 4 "Install Dependencies" log "npm install..." npm install --prefer-offline 2>/dev/null || npm install || error "npm install gagal" success "OK" step 5 "Build" log "Building Next.js (estimasi 3-7 menit)..." AVAILABLE_MEM=$(free -m | awk 'NR==2{print $7}') [[ "$AVAILABLE_MEM" -lt 512 ]] && warn "Memory rendah: ${AVAILABLE_MEM}MB" if npm run build; then if [[ -f ".next/BUILD_ID" ]]; then NEW_BUILD=$(cat .next/BUILD_ID) success "Build OK — BUILD_ID: $NEW_BUILD" rm -rf .next.bak 2>/dev/null else error "Build selesai tapi BUILD_ID tidak ada" fi else if [[ -d ".next.bak" ]]; then warn "Build gagal — rollback..." rm -rf .next && cp -r .next.bak .next warn "Rollback OK — periksa kode sebelum deploy ulang" else error "Build gagal dan tidak ada backup" fi exit 1 fi step 6 "Reload PM2 (zero-downtime)" if pm2 list | grep -q "pesentiket-web"; then pm2 reload pesentiket-web else pm2 start "$FE_DIR/ecosystem.config.js" --only pesentiket-web fi sleep 5 step 7 "Verifikasi" HTTP=$(curl -s -o /dev/null -w "%{http_code}" --max-time 15 http://localhost:3000 2>/dev/null) if [[ "$HTTP" == "200" ]] || [[ "$HTTP" == "307" ]] || [[ "$HTTP" == "308" ]]; then success "Frontend respond HTTP $HTTP ✓" else warn "Frontend respond HTTP $HTTP — cek: pm2 logs pesentiket-web" if [[ -d ".next.bak" ]]; then warn "Rollback build..." rm -rf .next && cp -r .next.bak .next pm2 reload pesentiket-web sleep 3 HTTP2=$(curl -s -o /dev/null -w "%{http_code}" --max-time 15 http://localhost:3000 2>/dev/null) [[ "$HTTP2" == "200" ]] \ && warn "Rollback berhasil" \ || error "Rollback gagal — cek manual" fi fi echo "" echo -e "${GREEN}============================================================${NC}" success "Frontend deploy selesai — $(date '+%Y-%m-%d %H:%M:%S')" echo -e "${GREEN}============================================================${NC}" echo "" pm2 status