pahvalentines/deploy.sh
2026-02-10 14:58:03 +05:30

174 lines
4.6 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPOSE_FILE="$ROOT_DIR/backend/docker-compose.yml"
ENV_FILE="$ROOT_DIR/backend/.env"
HTDOCS_DIR="/var/vhosts/valentinesong.oliver.digital/htdocs"
usage() {
echo ""
echo "Usage: $0 [frontend] [backend]"
echo ""
echo "Parameters:"
echo " frontend Deploys PHP files and assets to htdocs"
echo " backend Rebuilds Docker containers and runs migrations"
echo ""
echo "Examples:"
echo " $0 frontend # Deploy only frontend"
echo " $0 backend # Deploy only backend"
echo " $0 frontend backend # Deploy everything"
exit 1
}
# --- NEW: ARGUMENT PARSING ---
if [ $# -eq 0 ]; then
usage
fi
DEPLOY_FRONTEND=false
DEPLOY_BACKEND=false
for arg in "$@"; do
case "$arg" in
frontend) DEPLOY_FRONTEND=true ;;
backend) DEPLOY_BACKEND=true ;;
*) echo "Unknown parameter: $arg"; usage ;;
esac
done
# ##################################################################################################
if [ ! -d "$ROOT_DIR/.git" ]; then
echo "Error: run this script from the repo root (expected .git here)." >&2
exit 1
fi
if ! command -v git >/dev/null 2>&1; then
echo "Error: git is required but not found in PATH." >&2
exit 1
fi
echo ""
echo "Pulling latest code..."
git -C "$ROOT_DIR" pull
# ##################################################################################################
if [ "$DEPLOY_BACKEND" = true ]; then
echo ""
echo "Checking BACKEND dependancies..."
if ! command -v docker >/dev/null 2>&1; then
echo "Error: docker is required but not found in PATH." >&2
exit 1
fi
if ! docker compose version >/dev/null 2>&1; then
echo "Error: docker compose is required but not available (docker compose)." >&2
exit 1
fi
if [ ! -f "$ENV_FILE" ]; then
echo "Error: environment variables not found at $ENV_FILE" >&2
exit 1
fi
if [ ! -f "$COMPOSE_FILE" ]; then
echo "Error: docker compose file not found at $COMPOSE_FILE" >&2
exit 1
fi
echo ""
echo "Stopping containers (including orphans from old compose definitions)..."
docker compose -f "$COMPOSE_FILE" down --remove-orphans
echo ""
echo "Building containers (no cache to ensure fresh code)..."
docker compose -f "$COMPOSE_FILE" build --no-cache
echo ""
echo "Starting containers..."
docker compose -f "$COMPOSE_FILE" up -d --force-recreate
echo ""
echo "Waiting for database to be healthy..."
MAX_ATTEMPTS=30
ATTEMPT=0
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
if docker compose -f "$COMPOSE_FILE" exec -T db pg_isready -U pah -d pah >/dev/null 2>&1; then
echo "Database is ready."
break
fi
ATTEMPT=$((ATTEMPT + 1))
if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then
echo "Error: database did not become healthy after $MAX_ATTEMPTS attempts." >&2
exit 1
fi
echo "Waiting for database... (attempt $ATTEMPT/$MAX_ATTEMPTS)"
sleep 2
done
echo ""
echo "Running database migrations..."
docker compose -f "$COMPOSE_FILE" exec -T web alembic upgrade head
echo ""
echo "BACKEND Deploy complete."
fi
# ##################################################################################################
if [ "$DEPLOY_FRONTEND" = true ]; then
echo ""
echo "Checking FRONTEND dependancies..."
if [ ! -d "$HTDOCS_DIR" ]; then
echo "Error: htdocs directory not found at $HTDOCS_DIR" >&2
exit 1
fi
FRONTEND_ITEMS=(
"index.php"
"waiting.php"
"result.php"
"header.php"
"footer.php"
"opengraph.php"
"analytics.php"
"admin.html"
"assets"
"favicon.ico"
"favicon-16x16.png"
"favicon-32x32.png"
"apple-touch-icon.png"
"pets-icon.svg"
)
echo ""
echo "Removing previous frontend files..."
for item in "${FRONTEND_ITEMS[@]}"; do
if [ -e "$HTDOCS_DIR/$item" ]; then
rm -rf "$HTDOCS_DIR/$item";
fi
done
echo ""
echo "Copying frontend files to htdocs..."
for item in "${FRONTEND_ITEMS[@]}"; do
if [ -e "$ROOT_DIR/frontend/$item" ]; then
echo "Copying => $item"
cp -a "$ROOT_DIR/frontend/$item" "$HTDOCS_DIR/"
else
echo "Warning: missing 'frontend/$item' in repo, skipping." >&2
fi
done
echo ""
echo "FRONTEND Deploy complete."
fi