#!/usr/bin/env bash set -euo pipefail APP_DIR="${APP_DIR:-/opt/drivers_bot/}" BRANCH="${DEPLOY_BRANCH:-main}" COMPOSE="${COMPOSE:-docker compose}" HEALTH_URL="${HEALTH_URL:-http://127.0.0.1:8000/ready}" BACKUP_BEFORE_DEPLOY="${BACKUP_BEFORE_DEPLOY:-false}" if [[ ! -d "$APP_DIR/.git" ]]; then echo "Deploy directory is not a git repository: $APP_DIR" >&2 exit 1 fi cd "$APP_DIR" if [[ ! -f ".env" ]]; then echo ".env is missing in $APP_DIR" >&2 exit 1 fi echo "Fetching $BRANCH..." git fetch origin "$BRANCH" git checkout "$BRANCH" git pull --ff-only origin "$BRANCH" if [[ "$BACKUP_BEFORE_DEPLOY" == "true" ]]; then echo "Creating pre-deploy database backup..." ./scripts/backup_db.sh fi echo "Building and starting containers..." $COMPOSE build $COMPOSE up -d echo "Applying migrations..." $COMPOSE exec -T api alembic upgrade head echo "Running smoke checks..." $COMPOSE exec -T api python -m compileall -q app bot echo "Running health check: $HEALTH_URL" for attempt in {1..30}; do if curl -fsS "$HEALTH_URL" >/tmp/carpass-ready.json; then cat /tmp/carpass-ready.json echo BASE_URL="${BASE_URL:-${HEALTH_URL%/ready}}" ./scripts/smoke_test.sh $COMPOSE ps exit 0 fi sleep 2 done echo "API readiness check failed" >&2 $COMPOSE logs --tail=120 api >&2 exit 1