#!/usr/bin/env bash set -euo pipefail COMPOSE="docker compose -f docker-compose.prod.yml" LIVE_FILE=".live-slot" ROUTER_CONF="router/default.conf" # ── Determine slots ───────────────────────────────────────── CURRENT=$(cat "$LIVE_FILE" 2>/dev/null || echo "none") if [ "$CURRENT" = "blue" ]; then TARGET="green" else TARGET="blue" fi echo "==> Current: $CURRENT → Deploying: $TARGET" # ── Build ─────────────────────────────────────────────────── echo "==> Building $TARGET..." $COMPOSE --profile "$TARGET" build # ── Migrate ───────────────────────────────────────────────── echo "==> Running migrations..." $COMPOSE --profile "$TARGET" run --rm "${TARGET}-app" \ python -m beanflows.migrations.migrate # ── Start & health check ─────────────────────────────────── echo "==> Starting $TARGET (waiting for health check)..." if ! $COMPOSE --profile "$TARGET" up -d --wait; then echo "!!! Health check failed — rolling back" $COMPOSE stop "${TARGET}-app" "${TARGET}-worker" "${TARGET}-scheduler" exit 1 fi # ── Switch router ─────────────────────────────────────────── echo "==> Switching router to $TARGET..." mkdir -p "$(dirname "$ROUTER_CONF")" cat > "$ROUTER_CONF" < Stopping $CURRENT..." $COMPOSE stop "${CURRENT}-app" "${CURRENT}-worker" "${CURRENT}-scheduler" fi # ── Record live slot ──────────────────────────────────────── echo "$TARGET" > "$LIVE_FILE" echo "==> Deployed $TARGET successfully!"