#!/bin/bash # Cinny dev branch nightly build and deploy set -e REPO_DIR="/opt/cinny-dev" WEB_ROOT="/var/www/html" CONFIG_SAVED="/opt/cinny-dev/.cinny-config.json" BUILD_DIR="/opt/cinny-dev/dist" LOG="/var/log/cinny-dev-update.log" exec >> "$LOG" 2>&1 echo "=== $(date) === Starting Cinny dev update ===" # Save config from live site (skip if web root is already broken) if [ -f "$WEB_ROOT/config.json" ]; then cp "$WEB_ROOT/config.json" "$CONFIG_SAVED" fi # Pull latest dev cd "$REPO_DIR" git fetch --depth=1 origin dev PREV=$(git rev-parse HEAD) git reset --hard origin/dev NEW=$(git rev-parse HEAD) if [ "$PREV" = "$NEW" ]; then echo "No changes since last build ($NEW), skipping." exit 0 fi echo "New commits since $PREV, building $NEW..." # Clean previous dist so stale files don't get deployed on partial build rm -rf "$BUILD_DIR" # Build — cap at 896MB to stay within 1GB RAM + swap headroom export NODE_OPTIONS='--max_old_space_size=896' npm ci 2>&1 | tail -5 npm run build 2>&1 | tail -10 # Verify build actually produced output before touching web root if [ ! -f "$BUILD_DIR/index.html" ]; then echo "ERROR: Build failed — dist/index.html missing. Web root untouched." exit 1 fi # Deploy only now that we know the build succeeded rm -rf "$WEB_ROOT"/* cp -r "$BUILD_DIR"/* "$WEB_ROOT/" # Restore config if [ -f "$CONFIG_SAVED" ]; then cp "$CONFIG_SAVED" "$WEB_ROOT/config.json" else echo "WARNING: No saved config found — default config from build will be used." fi nginx -s reload echo "=== Deploy complete: $NEW ==="