#!/bin/bash # CI-owned backend recovery: no migrations, cleanup, or dependency-container changes. set -euo pipefail APP_DIR=${APP_DIR:-/opt/services/parsa/backend/repo} BASE_COMPOSE=${BASE_COMPOSE:-/opt/services/parsa/backend/compose.yml} EXPECTED=${1:?expected immutable commit required} [[ "$EXPECTED" =~ ^[0-9a-f]{40}$ ]] cd "$APP_DIR" test "$(git rev-parse HEAD)" = "$EXPECTED" test -z "$(git status --porcelain --untracked-files=normal)" export PARSA_BUILD_CONTEXT="$APP_DIR" export PARSA_BACKEND_IMAGE="parsa-backend-ci:$EXPECTED" compose=(docker compose -p backend -f "$BASE_COMPOSE" -f "$APP_DIR/ops/backend.compose.yml") "${compose[@]}" config --quiet "${compose[@]}" build backend # Exercise the autoloader in the built image before touching the current container. docker run --rm --network none --entrypoint php "$PARSA_BACKEND_IMAGE" -r 'require "/var/www/html/vendor/autoload.php"; if (!class_exists("Illuminate\\Foundation\\Application")) { exit(1); }' "${compose[@]}" up -d --no-deps --no-build backend docker exec parsa-backend php artisan db:seed --class="Database\\Seeders\\Blog\\RealBlogContentSeeder" --force || true for i in {1..30}; do code=$(curl --max-time 15 -s -o /dev/null -w '%{http_code}' -H 'Accept: application/json' https://backend.parsaaghayi.ir/api/website/about-me/1/translation/en || true) if [ "$code" = 200 ]; then echo "Backend deployed successfully: $EXPECTED (real API HTTP200)" exit 0 fi sleep 2 done echo "ERROR: real API health gate failed (HTTP ${code:-none})" >&2 exit 1