#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" cd "$SCRIPT_DIR" # Source .env for credentials if [ -f ".env" ]; then set -a source .env set +a fi if [ -z "${GITEA_PASSWORD:-}" ]; then echo "[$(date -Iseconds)] ERROR: GITEA_PASSWORD is not set. Exiting." exit 1 fi # Configure the remote with credentials for non-interactive pulls REMOTE_BASE="https://gitea.ct.cozytren.ch/romane/agentbox-test" REMOTE_AUTH="https://agentbox:${GITEA_PASSWORD}@gitea.ct.cozytren.ch/romane/agentbox-test" git remote set-url origin "$REMOTE_AUTH" LAST_HASH="" echo "[$(date -Iseconds)] Starting watch loop (polling every 20s)..." while true; do # Fetch latest from remote git fetch origin main 2>&1 || { echo "[$(date -Iseconds)] WARN: git fetch failed, retrying in 20s..." sleep 20 continue } REMOTE_HASH=$(git rev-parse origin/main 2>/dev/null || echo "") if [ -z "$REMOTE_HASH" ]; then echo "[$(date -Iseconds)] WARN: could not resolve origin/main, retrying in 20s..." sleep 20 continue fi if [ "$REMOTE_HASH" != "$LAST_HASH" ]; then if [ -n "$LAST_HASH" ]; then echo "[$(date -Iseconds)] New commits detected! Updating..." echo " Previous: $LAST_HASH" echo " Current: $REMOTE_HASH" git pull origin main 2>&1 || { echo "[$(date -Iseconds)] ERROR: git pull failed, retrying in 20s..." LAST_HASH="" sleep 20 continue } echo "[$(date -Iseconds)] Bringing containers down..." docker compose down 2>&1 echo "[$(date -Iseconds)] Building images..." docker compose build 2>&1 echo "[$(date -Iseconds)] Starting containers..." docker compose up -d 2>&1 echo "[$(date -Iseconds)] Update complete." else echo "[$(date -Iseconds)] Initial state recorded: $REMOTE_HASH" fi LAST_HASH="$REMOTE_HASH" else echo "[$(date -Iseconds)] No changes." fi sleep 20 done