← back to Dear Bubbe Nextjs

scripts/purge-cache.sh

162 lines

#!/bin/bash

# Purge Cloudflare Cache and Restart Service
# Usage: ./purge-cache.sh

set -e  # Exit on error

PROJECT_DIR="/root/Projects/dear-bubbe-nextjs"
PM2_APP="bubbe-nextjs"

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  Cloudflare Cache Purge & Service Restart"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

cd "$PROJECT_DIR"

# Check if .env.local has Cloudflare credentials
if grep -q "CLOUDFLARE_EMAIL" .env.local && grep -q "CLOUDFLARE_PASSWORD" .env.local; then
    echo "✓ Using Puppeteer automation (from .env.local)"
    echo ""

    # Run Puppeteer script
    node purge-cache-puppeteer.js

    if [ $? -ne 0 ]; then
        echo "✗ Puppeteer automation failed"
        echo "  Falling back to API method..."
        echo ""
    else
        echo ""
        echo "✓ Cloudflare cache purged via Puppeteer"
    fi
else
    # Fallback to API method
    CLOUDFLARE_EMAIL="Stev@designerwallcoverings.com"
    DOMAIN="bubbe.ai"

    # Check for API credentials
    if [ -f /root/.cloudflare-token ]; then
        echo "✓ Using API Token authentication"
        CLOUDFLARE_TOKEN=$(cat /root/.cloudflare-token)
        AUTH_HEADER="Authorization: Bearer $CLOUDFLARE_TOKEN"
    elif [ -f /root/.cloudflare-key ]; then
        echo "✓ Using Global API Key authentication"
        CLOUDFLARE_KEY=$(cat /root/.cloudflare-key)
        AUTH_HEADER="X-Auth-Key: $CLOUDFLARE_KEY"
    else
        echo "✗ ERROR: No Cloudflare credentials found!"
        echo ""
        echo "Please set up credentials in .env.local or use API method:"
        echo ""
        echo "Option 1 (Recommended): Add to .env.local:"
        echo "  CLOUDFLARE_EMAIL=your@email.com"
        echo "  CLOUDFLARE_PASSWORD=yourpassword"
        echo "  CLOUDFLARE_ACCOUNT_ID=your_account_id"
        echo "  CLOUDFLARE_DOMAIN=bubbe.ai"
        echo ""
        echo "Option 2: Use API token:"
        echo "  echo 'YOUR_TOKEN' > /root/.cloudflare-token"
        echo ""
        exit 1
    fi

    # Get Zone ID
    echo ""
    echo "→ Fetching Zone ID for $DOMAIN..."
    ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN" \
      -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
      -H "$AUTH_HEADER" \
      -H "Content-Type: application/json" | jq -r '.result[0].id')

    if [ "$ZONE_ID" == "null" ] || [ -z "$ZONE_ID" ]; then
        echo "✗ ERROR: Could not fetch Zone ID"
        echo "  Check your API credentials and try again"
        exit 1
    fi

    echo "✓ Zone ID: $ZONE_ID"

    # Purge cache
    echo ""
    echo "→ Purging Cloudflare cache..."
    PURGE_RESULT=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
      -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
      -H "$AUTH_HEADER" \
      -H "Content-Type: application/json" \
      --data '{"purge_everything":true}')

    if echo "$PURGE_RESULT" | jq -e '.success' > /dev/null; then
        echo "✓ Cache purged successfully"
    else
        echo "✗ ERROR: Failed to purge cache"
        echo "$PURGE_RESULT" | jq .
        exit 1
    fi
fi

# Rebuild Next.js
echo ""
echo "→ Rebuilding Next.js application..."
cd "$PROJECT_DIR"
npm run build > /tmp/bubbe-build.log 2>&1

if [ $? -eq 0 ]; then
    echo "✓ Build completed successfully"
else
    echo "✗ ERROR: Build failed"
    echo "  Check logs: tail -50 /tmp/bubbe-build.log"
    exit 1
fi

# Restart PM2 service
echo ""
echo "→ Restarting PM2 service..."
pm2 restart $PM2_APP > /dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "✓ Service restarted successfully"
else
    echo "✗ ERROR: Failed to restart service"
    exit 1
fi

# Wait for service to be ready
echo ""
echo "→ Waiting for service to be ready..."
sleep 3

# Verify service is running
echo ""
echo "→ Verifying service status..."
PM2_STATUS=$(pm2 jlist | jq -r ".[] | select(.name==\"$PM2_APP\") | .pm2_env.status")

if [ "$PM2_STATUS" == "online" ]; then
    echo "✓ Service is online"
else
    echo "✗ WARNING: Service status: $PM2_STATUS"
fi

# Test the website
echo ""
echo "→ Testing website..."
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://www.$DOMAIN)

if [ "$HTTP_STATUS" == "200" ]; then
    echo "✓ Website is responding (HTTP $HTTP_STATUS)"
else
    echo "⚠ WARNING: Website returned HTTP $HTTP_STATUS"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  ✓ Cache purge and restart completed!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Next steps:"
echo "  • Visit https://www.$DOMAIN to verify changes"
echo "  • Cache propagation may take 30-60 seconds"
echo "  • Use hard refresh (Ctrl+Shift+R) if needed"
echo ""