103 lines
3.2 KiB
Bash
Executable File
103 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Run on the Linux production host (CI SSHs here after pushing :v* images).
|
|
# Usage: ./prod-remote-deploy.sh v1.0.1
|
|
set -euo pipefail
|
|
|
|
TAG="${1:?usage: prod-remote-deploy.sh v1.0.1}"
|
|
INFRA="${PROD_INFRA_DIR:-/opt/dyolink/infrastructure}"
|
|
cd "$INFRA"
|
|
|
|
if [ ! -f .env ]; then
|
|
echo "Missing $INFRA/.env — copy deploy.prod.env.example and set DOMAIN=nudentic.ir REGISTRY_PREFIX=wixur.ir:3000/<owner>"
|
|
exit 1
|
|
fi
|
|
|
|
if ! grep -q '^REGISTRY_PREFIX=.\+' .env; then
|
|
echo "Set REGISTRY_PREFIX in .env (Gitea registry, e.g. wixur.ir:3000/admin). Do not use Docker Hub :latest for tag deploys."
|
|
exit 1
|
|
fi
|
|
|
|
export TAG
|
|
|
|
# Default runtime bases to Gitea copies when .env has REGISTRY_PREFIX but no *IMAGE lines.
|
|
DEPLOY_TAG="$TAG"
|
|
set -a
|
|
# shellcheck disable=SC1091
|
|
. ./.env
|
|
set +a
|
|
export TAG="$DEPLOY_TAG"
|
|
export POSTGRES_IMAGE="${POSTGRES_IMAGE:-${REGISTRY_PREFIX}/postgres:15-alpine}"
|
|
export NGINX_IMAGE="${NGINX_IMAGE:-${REGISTRY_PREFIX}/nginx:alpine}"
|
|
export CERTBOT_IMAGE="${CERTBOT_IMAGE:-${REGISTRY_PREFIX}/certbot:latest}"
|
|
|
|
# Windows Gitea often RSTs concurrent or long pulls (connection reset by peer).
|
|
# Pull one image at a time with backoff so a flake does not fail the whole tag.
|
|
pull_one() {
|
|
local service="$1"
|
|
local attempt=1
|
|
local max=5
|
|
local delay=8
|
|
while [ "$attempt" -le "$max" ]; do
|
|
echo "Pulling $service :$TAG (attempt $attempt/$max)"
|
|
if docker compose -f docker-compose.prod.yml --env-file .env pull "$service"; then
|
|
return 0
|
|
fi
|
|
if [ "$attempt" -eq "$max" ]; then
|
|
echo "Failed to pull $service after $max attempts"
|
|
return 1
|
|
fi
|
|
echo "Pull of $service failed; retrying in ${delay}s..."
|
|
sleep "$delay"
|
|
delay=$((delay * 2))
|
|
attempt=$((attempt + 1))
|
|
done
|
|
}
|
|
|
|
echo "Pulling backend/frontend :$TAG and runtime bases from Gitea (REGISTRY_PREFIX in .env)"
|
|
pull_one backend
|
|
pull_one frontend
|
|
pull_one postgres
|
|
pull_one nginx
|
|
pull_one certbot
|
|
|
|
if [ -f nginx/nginx.ssl.conf.template ]; then
|
|
echo "Rendering nginx SSL config (includes /admin → backend)"
|
|
chmod +x scripts/render-nginx-ssl.sh
|
|
./scripts/render-nginx-ssl.sh
|
|
fi
|
|
|
|
COMPOSE=(docker compose -f docker-compose.prod.yml --env-file .env)
|
|
|
|
wait_healthy() {
|
|
local name="$1"
|
|
local seconds="${2:-300}"
|
|
local elapsed=0
|
|
local status="missing"
|
|
echo "Waiting for $name healthy (up to ${seconds}s)..."
|
|
while [ "$elapsed" -lt "$seconds" ]; do
|
|
status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$name" 2>/dev/null || echo missing)"
|
|
if [ "$status" = "healthy" ]; then
|
|
echo "$name is healthy"
|
|
return 0
|
|
fi
|
|
sleep 3
|
|
elapsed=$((elapsed + 3))
|
|
done
|
|
echo "$name not healthy after ${seconds}s (last status=${status})"
|
|
docker logs "$name" --tail 50 || true
|
|
return 1
|
|
}
|
|
|
|
# Backend/frontend first so nginx never caches a dead upstream IP (502 on /api and /admin).
|
|
echo "Starting postgres, backend, frontend..."
|
|
"${COMPOSE[@]}" up -d postgres backend frontend
|
|
wait_healthy dyolink_backend_prod 300
|
|
wait_healthy dyolink_frontend_prod 180
|
|
echo "Recreating nginx after backends are reachable"
|
|
"${COMPOSE[@]}" up -d --force-recreate nginx
|
|
"${COMPOSE[@]}" up -d
|
|
echo "=== Status ==="
|
|
"${COMPOSE[@]}" ps
|
|
echo "Health: https://nudentic.ir/api/health"
|
|
echo "AdminJS: https://nudentic.ir/admin"
|