From 465eb698242c8cc5f99fda41b2d9f3df20183c69 Mon Sep 17 00:00:00 2001 From: agentbox Date: Fri, 31 Jul 2026 22:38:05 +0000 Subject: [PATCH] add watch-for-updates.sh: auto-pull and docker compose rebuild every 20s --- watch-for-updates.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 watch-for-updates.sh diff --git a/watch-for-updates.sh b/watch-for-updates.sh new file mode 100755 index 0000000..a3e3232 --- /dev/null +++ b/watch-for-updates.sh @@ -0,0 +1,76 @@ +#!/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" +REMOTE_AUTH="https://agentbox:${GITEA_PASSWORD}@gitea.ct.cozytren.ch/romane/agentbox" +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