#!/bin/bash set -euo pipefail # ============================================================================= # Deploy script for hm-o2e-tool # Repo lives at /var/www/html/hm-o2e-tool — git pull IS the deployment # Usage: ./deploy.sh (run from anywhere, script detects its own location) # Idempotent: safe to run on first deploy or any subsequent update # ============================================================================= DEPLOY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BRANCH="main" FILE_OWNER="vadym.samoilenko:vadym.samoilenko" # ----------------------------------------------------------------------------- log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" } error() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $*" >&2 exit 1 } # ----------------------------------------------------------------------------- # Preflight checks # ----------------------------------------------------------------------------- log "Starting deployment of hm-o2e-tool from $DEPLOY_DIR..." [ -d "$DEPLOY_DIR/.git" ] || error "Not a git repository: $DEPLOY_DIR" [ -f "$DEPLOY_DIR/index.html" ] || error "index.html not found in $DEPLOY_DIR" [ -f "$DEPLOY_DIR/config.php" ] || error "config.php not found in $DEPLOY_DIR" command -v php >/dev/null 2>&1 || error "PHP is not installed or not in PATH" # ----------------------------------------------------------------------------- # Pull latest code # ----------------------------------------------------------------------------- log "Pulling latest code from origin/$BRANCH..." git -C "$DEPLOY_DIR" fetch origin git -C "$DEPLOY_DIR" reset --hard "origin/$BRANCH" git -C "$DEPLOY_DIR" clean -fd DEPLOYED_COMMIT=$(git -C "$DEPLOY_DIR" rev-parse --short HEAD) log "Now at commit: $DEPLOYED_COMMIT" # ----------------------------------------------------------------------------- # Set permissions # ----------------------------------------------------------------------------- chown "$FILE_OWNER" "$DEPLOY_DIR/index.html" "$DEPLOY_DIR/config.php" chmod 644 "$DEPLOY_DIR/index.html" "$DEPLOY_DIR/config.php" log "Permissions set (owner: $FILE_OWNER, mode: 644)" # ----------------------------------------------------------------------------- # Validate PHP syntax # ----------------------------------------------------------------------------- php -l "$DEPLOY_DIR/config.php" >/dev/null 2>&1 \ && log "config.php syntax OK" \ || error "config.php has a PHP syntax error" # ----------------------------------------------------------------------------- # Done # ----------------------------------------------------------------------------- log "Deployment complete. Commit $DEPLOYED_COMMIT is live."