81 lines
2.2 KiB
Bash
Executable File
81 lines
2.2 KiB
Bash
Executable File
#!/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
|
|
COMPOSE_FILE="docker-compose.meal.yml"
|
|
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 -f "$COMPOSE_FILE" down 2>&1
|
|
else
|
|
echo "[$(date -Iseconds)] First run — pulling and launching containers..."
|
|
git pull origin main 2>&1 || {
|
|
echo "[$(date -Iseconds)] ERROR: git pull failed, retrying in 20s..."
|
|
sleep 20
|
|
continue
|
|
}
|
|
fi
|
|
|
|
echo "[$(date -Iseconds)] Building images..."
|
|
docker compose -f "$COMPOSE_FILE" build 2>&1
|
|
|
|
echo "[$(date -Iseconds)] Starting containers..."
|
|
docker compose -f "$COMPOSE_FILE" up -d 2>&1
|
|
|
|
echo "[$(date -Iseconds)] Update complete."
|
|
LAST_HASH="$REMOTE_HASH"
|
|
else
|
|
echo "[$(date -Iseconds)] No changes."
|
|
fi
|
|
|
|
sleep 20
|
|
done
|