Files
gamercomp/deploy.sh
T
2026-04-30 02:14:25 +00:00

113 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# Deploy script for GamerComp.com
# Usage: ./deploy.sh [--clear-cloudflare]
#
# Cache clearing order (correct sequence):
# 1. Cloudflare CDN (if --clear-cloudflare) - Clear edge servers first
# 2. Build new frontend assets (with new hashed filenames)
# 3. Restart backend API
# 4. Reload nginx (picks up new files)
# 5. Clean old artifacts
# 6. Users clear browser cache via /clear-cache.html
set -e
echo "=========================================="
echo " GamerComp.com Deploy Script"
echo "=========================================="
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Navigate to project root
cd /home/gamearc/game-arcade
# Load environment variables (for Cloudflare credentials)
if [[ -f backend/.env ]]; then
export $(grep -E '^CLOUDFLARE_' backend/.env | xargs)
fi
# Step 1: Clear Cloudflare cache FIRST (if requested and configured)
# This ensures edge servers don't serve stale content during deploy
if [[ "$1" == "--clear-cloudflare" ]]; then
if [[ -n "$CLOUDFLARE_ZONE_ID" && -n "$CLOUDFLARE_API_TOKEN" ]]; then
echo -e "${YELLOW}Step 1: Clearing Cloudflare cache (before deploy)...${NC}"
RESULT=$(curl -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/purge_cache" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}' \
--silent)
if echo "$RESULT" | jq -e '.success' > /dev/null 2>&1; then
echo -e "${GREEN}Cloudflare cache cleared!${NC}"
else
echo -e "${YELLOW}Warning: Cloudflare purge may have failed${NC}"
echo "$RESULT" | jq .
fi
else
echo -e "${YELLOW}Step 1: Skipping Cloudflare - credentials not set${NC}"
echo "To enable, add to ~/.bashrc:"
echo " export CLOUDFLARE_ZONE_ID='your_zone_id'"
echo " export CLOUDFLARE_API_TOKEN='your_api_token'"
fi
else
echo -e "${YELLOW}Step 1: Skipping Cloudflare (use --clear-cloudflare to purge CDN)${NC}"
fi
# Step 2: Build frontend (creates new hashed filenames)
echo -e "${YELLOW}Step 2: Building frontend...${NC}"
cd frontend
npm run build
cd ..
echo -e "${GREEN}Frontend built successfully!${NC}"
# Step 3: Restart backend
echo -e "${YELLOW}Step 3: Restarting backend...${NC}"
pm2 restart gamercomp-api
echo -e "${GREEN}Backend restarted!${NC}"
# Step 4: Reload nginx (serves new files)
echo -e "${YELLOW}Step 4: Reloading nginx...${NC}"
sudo nginx -t && sudo systemctl reload nginx
echo -e "${GREEN}Nginx reloaded!${NC}"
# Step 5: Clear old cached files
echo -e "${YELLOW}Step 5: Cleaning up old build artifacts...${NC}"
find /home/gamearc/game-arcade/frontend/dist/assets -name "*.js" -mtime +7 -delete 2>/dev/null || true
find /home/gamearc/game-arcade/frontend/dist/assets -name "*.css" -mtime +7 -delete 2>/dev/null || true
echo -e "${GREEN}Cleanup complete!${NC}"
# Step 6: Purge Cloudflare AGAIN after new files are live
# This ensures CDN fetches the new hashed files
if [[ "$1" == "--clear-cloudflare" && -n "$CLOUDFLARE_ZONE_ID" && -n "$CLOUDFLARE_API_TOKEN" ]]; then
echo -e "${YELLOW}Step 6: Final Cloudflare purge (new files now live)...${NC}"
curl -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/purge_cache" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}' \
--silent > /dev/null
echo -e "${GREEN}Cloudflare cache purged!${NC}"
fi
echo ""
echo "=========================================="
echo -e "${GREEN} Deploy Complete!${NC}"
echo "=========================================="
echo ""
echo "Cache clearing order completed:"
echo " 1. Cloudflare CDN purged (if --clear-cloudflare)"
echo " 2. New frontend built with fresh hashed filenames"
echo " 3. Backend restarted"
echo " 4. Nginx reloaded"
echo " 5. Old artifacts cleaned"
if [[ "$1" == "--clear-cloudflare" ]]; then
echo " 6. Final Cloudflare purge (ensures CDN has new files)"
fi
echo ""
echo "For users to clear browser cache:"
echo " • Visit /clear-cache.html"
echo " • Or hard refresh: Ctrl+Shift+R (Win) / Cmd+Shift+R (Mac)"
echo ""
echo "Service worker auto-updates within seconds."