#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" COMPOSE_FILE="$ROOT_DIR/backend/docker-compose.yml" HTDOCS_DIR="/var/vhosts/valentinesong.oliver.digital/htdocs" usage() { 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 "Pulling latest code..." git -C "$ROOT_DIR" pull # ################################################################################################## if [ "$DEPLOY_BACKEND" = true ]; then 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 "$COMPOSE_FILE" ]; then echo "Error: docker compose file not found at $COMPOSE_FILE" >&2 exit 1 fi echo "Stopping containers..." docker compose -f "$COMPOSE_FILE" down echo "Building containers (no cache to ensure fresh code)..." docker compose -f "$COMPOSE_FILE" build --no-cache echo "Starting containers..." docker compose -f "$COMPOSE_FILE" up -d --force-recreate 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)) sleep 2 done echo "Running database migrations..." docker compose -f "$COMPOSE_FILE" exec -T web alembic upgrade head echo "BACKEND Deploy complete." fi # ################################################################################################## if [ "$DEPLOY_FRONTEND" = true ]; then 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" "admin.html" "assets" ) 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 "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 "FRONTEND Deploy complete." fi