diff --git a/infra/auto-update.sh b/infra/auto-update.sh new file mode 100755 index 0000000..862dd31 --- /dev/null +++ b/infra/auto-update.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# auto-update.sh — pull latest changes and restart affected services. +# Run by the hiy-update.timer systemd user unit every 5 minutes. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" + +log() { echo "[hiy-update] $(date '+%H:%M:%S') $*"; } + +cd "$REPO_ROOT" + +# Fetch without touching the working tree. +git fetch origin 2>&1 | sed 's/^/[git] /' || { log "git fetch failed — skipping"; exit 0; } + +LOCAL=$(git rev-parse HEAD) +REMOTE=$(git rev-parse "@{u}" 2>/dev/null || echo "$LOCAL") + +if [ "$LOCAL" = "$REMOTE" ]; then + log "Already up to date ($LOCAL)." + exit 0 +fi + +log "New commits detected — pulling ($LOCAL → $REMOTE)…" +git pull 2>&1 | sed 's/^/[git] /' + +# Determine which services need restarting based on what changed. +CHANGED=$(git diff --name-only "$LOCAL" "$REMOTE") +log "Changed files: $(echo "$CHANGED" | tr '\n' ' ')" + +# Always rebuild the server if any server-side code changed. +SERVER_CHANGED=$(echo "$CHANGED" | grep -E '^server/|^Cargo' || true) +COMPOSE_CHANGED=$(echo "$CHANGED" | grep '^infra/docker-compose' || true) +CADDY_CHANGED=$(echo "$CHANGED" | grep '^proxy/Caddyfile' || true) + +COMPOSE_CMD="podman compose --env-file $REPO_ROOT/.env -f $SCRIPT_DIR/docker-compose.yml" + +if [ -n "$COMPOSE_CHANGED" ]; then + log "docker-compose.yml changed — restarting full stack…" + $COMPOSE_CMD up -d +elif [ -n "$SERVER_CHANGED" ]; then + log "Server code changed — rebuilding server…" + $COMPOSE_CMD up -d --build server +elif [ -n "$CADDY_CHANGED" ]; then + log "Caddyfile changed — reloading Caddy…" + $COMPOSE_CMD exec caddy caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile +else + log "No service restart needed for these changes." +fi + +log "Done." diff --git a/infra/start.sh b/infra/start.sh index cdca443..67c84ea 100755 --- a/infra/start.sh +++ b/infra/start.sh @@ -183,3 +183,37 @@ systemctl --user daemon-reload systemctl --user enable hiy.service loginctl enable-linger "$(id -un)" 2>/dev/null || true echo "[hiy] Boot service installed: systemctl --user status hiy.service" + +# ── Install systemd timer for auto-update ───────────────────────────────────── +UPDATE_SERVICE="$SERVICE_DIR/hiy-update.service" +UPDATE_TIMER="$SERVICE_DIR/hiy-update.timer" + +cat > "$UPDATE_SERVICE" < "$UPDATE_TIMER" <