new-site/scripts/deploy.sh
justin aa8ad6d106 Use --no-cache in deploy to prevent stale Docker builds
Docker layer caching was serving old site content after git pull
because file timestamps didn't change enough to bust the cache.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-27 07:57:14 -05:00

78 lines
2.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# deploy.sh — Deploy Performance West via git pull
#
# Usage:
# ./scripts/deploy.sh # deploy to prod
# ./scripts/deploy.sh prod # deploy to prod
# ./scripts/deploy.sh dev # deploy to dev
#
# Prerequisites:
# - SSH key configured for deploy@207.174.124.71 port 22022
# - Server directories are git clones of the Forgejo repo
# - .env configured on server
set -euo pipefail
SERVER="deploy@207.174.124.71"
SSH_PORT=22022
ENV="${1:-prod}"
if [ "$ENV" = "dev" ]; then
REMOTE_DIR="/opt/performancewest-dev"
SITE_URL="https://dev.performancewest.net"
API_URL="https://api.dev.performancewest.net"
API_PORT=3002
else
REMOTE_DIR="/opt/performancewest"
SITE_URL="https://performancewest.net"
API_URL="https://api.performancewest.net"
API_PORT=3001
fi
# Colors
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
log() { echo -e "${GREEN}[deploy:${ENV}]${NC} $*"; }
warn() { echo -e "${YELLOW}[deploy:${ENV}]${NC} $*"; }
die() { echo -e "${RED}[deploy:${ENV}] ERROR:${NC} $*" >&2; exit 1; }
# ── 1. Ensure local changes are committed ────────────────────────────────────
if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
warn "You have uncommitted changes. Commit them first:"
git status --short
die "Commit your changes before deploying."
fi
# ── 2. Push to Forgejo ───────────────────────────────────────────────────────
log "Pushing to Forgejo..."
git push origin main || die "Git push failed"
# ── 3. Pull + build + restart on server ──────────────────────────────────────
log "Deploying to ${ENV} (${REMOTE_DIR})..."
ssh -p "${SSH_PORT}" "${SERVER}" bash <<REMOTE
set -euo pipefail
cd ${REMOTE_DIR}
echo "[remote] Pulling latest..."
git pull origin main
echo "[remote] Building Docker images..."
docker compose build --parallel --no-cache
echo "[remote] Restarting containers..."
docker compose up -d --remove-orphans
echo "[remote] Waiting for API health check..."
for i in \$(seq 1 20); do
if curl -sf http://localhost:${API_PORT}/health > /dev/null 2>&1; then
echo "[remote] API is healthy."
break
fi
sleep 3
done
echo "[remote] Container status:"
docker compose ps --format "table {{.Name}}\t{{.Status}}"
REMOTE
log "Deploy complete."
log "Site: ${SITE_URL} | API: ${API_URL}"