bash
#!/bin/bash
# Simple deploy script with health check

set -euo pipefail

APP_NAME="${1:-myapp}"
DEPLOY_DIR="/opt/deploy/$APP_NAME"
HEALTH_URL="http://localhost:8080/health"

echo "Deploying $APP_NAME..."

# Pull latest
cd "$DEPLOY_DIR"
git pull origin main

# Install dependencies
npm ci --production

# Build
npm run build

# Restart service
sudo systemctl restart "$APP_NAME"

# Wait for health check
for i in $(seq 1 30); do
  if curl -sf "$HEALTH_URL" > /dev/null 2>&1; then
    echo "Health check passed after ${i}s"
    exit 0
  fi
  sleep 1
done

echo "ERROR: Health check failed after 30s"
sudo systemctl status "$APP_NAME"
exit 1