social-reporting-tool/v2/deploy/deploy-v2.sh
DJP d2271f9cf3 Deploy scripts: resolve repo root from script location
The previous BACKEND_DIR_V2=/opt/social-reporting-v2 hardcode failed on
the production host because we did an in-place cutover: V1 and V2 share
a single checkout at /opt/social-reporting. ./v2/deploy/deploy-v2.sh
exited immediately with "V2 dir not found".

Both deploy-v2.sh and rollback-to-v1.sh now derive their repo root from
the script's own location, so they work whatever the parent directory
is named. setup-v2.sh is left alone — it's the first-time provisioner
and creating /opt/social-reporting-v2 from scratch is still a fine
default for greenfield installs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 21:15:22 -04:00

39 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
set -euo pipefail
# Routine V2 redeploy (after `git pull`). Resolves the repo from the script's
# own location, so it works regardless of whether the checkout lives at
# /opt/social-reporting-v2 or /opt/social-reporting (V1's old path, reused).
GREEN='\033[0;32m'; RED='\033[0;31m'; NC='\033[0m'
log() { echo -e "${GREEN}[+]${NC} $1"; }
err() { echo -e "${RED}[x]${NC} $1"; exit 1; }
# Repo root = two dirs up from this script (v2/deploy/deploy-v2.sh).
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$REPO_ROOT" || err "Repo root not found at $REPO_ROOT"
log "Repo root: $REPO_ROOT"
log "Pulling latest..."
git pull origin main
# Fix briefs/ ownership in case sudo/root touched it. The container runs as uid 1000.
log "Refreshing briefs/ ownership..."
sudo mkdir -p "$REPO_ROOT/briefs"
sudo chown -R 1000:1000 "$REPO_ROOT/briefs"
log "Rebuilding V2 stack..."
docker compose -f v2/docker-compose.v2.yml -f v2/docker-compose.v2.prod.yml --env-file v2/.env up -d --build
log "Waiting for backend..."
for i in {1..30}; do
curl -sf http://127.0.0.1:3457/api/health >/dev/null 2>&1 && { log "Healthy"; break; }
[ "$i" -eq 30 ] && err "Backend not responding — docker compose -p social-reporting-v2 logs app-v2"
sleep 2
done
log "Reloading Apache..."
sudo systemctl reload apache2
echo -e "${GREEN}Deploy complete.${NC}"