#!/bin/bash set -eo pipefail # WebsiteBox Update Script # Usage: ./scripts/update.sh SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" cd "${PROJECT_DIR}" echo "═══════════════════════════════════════════════════════════" echo " WebsiteBox Update" echo "═══════════════════════════════════════════════════════════" # Check for uncommitted changes to tracked files if ! git diff --quiet HEAD 2>/dev/null; then echo "WARNING: You have uncommitted changes to tracked files." echo "These files have been modified:" git diff --name-only HEAD echo "" echo "Please commit or stash your changes before updating." exit 1 fi # Fetch latest changes echo "Checking for updates..." git fetch origin main 2>/dev/null || git fetch origin master 2>/dev/null || { echo "ERROR: Could not fetch from remote repository." exit 1 } # Determine branch name BRANCH="main" if ! git rev-parse --verify origin/main >/dev/null 2>&1; then BRANCH="master" fi # Check if already up to date LOCAL=$(git rev-parse HEAD) REMOTE=$(git rev-parse "origin/${BRANCH}") if [ "${LOCAL}" = "${REMOTE}" ]; then echo "Already up to date!" exit 0 fi # Show what's changing echo "" echo "Changes to be applied:" git log --oneline "HEAD..origin/${BRANCH}" echo "" # Prompt for confirmation read -rp "Apply these updates? (y/N) " confirm if [ "${confirm}" != "y" ] && [ "${confirm}" != "Y" ]; then echo "Update cancelled." exit 0 fi # Pull changes echo "Pulling updates..." git pull origin "${BRANCH}" # Pull latest base images echo "Pulling latest Docker images..." docker compose pull # Rebuild containers echo "Rebuilding containers..." docker compose up -d --build # Clean up old images echo "Cleaning up old images..." docker image prune -f # Run migrations if any exist MIGRATIONS_DIR="${PROJECT_DIR}/scripts/migrations" MIGRATIONS_STATE="${PROJECT_DIR}/.websitebox-migrations" if [ -d "${MIGRATIONS_DIR}" ]; then touch "${MIGRATIONS_STATE}" for migration in "${MIGRATIONS_DIR}"/*.sh; do [ -f "$migration" ] || continue migration_name=$(basename "$migration") if ! grep -qF "$migration_name" "${MIGRATIONS_STATE}" 2>/dev/null; then echo "Running migration: ${migration_name}..." bash "$migration" echo "$migration_name" >> "${MIGRATIONS_STATE}" fi done fi # Health check echo "Checking container health..." sleep 10 "${SCRIPT_DIR}/healthcheck.sh" echo "" echo "═══════════════════════════════════════════════════════════" echo " Update complete!" echo "═══════════════════════════════════════════════════════════"