From 8561ee3e748817f38876af4cb9ea0f9bc410f6f2 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Mar 2026 13:47:12 +0000 Subject: [PATCH] feat: add systemd timer for automatic git pull + service restart MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit auto-update.sh fetches origin every 5 minutes. If new commits are found it pulls and selectively restarts only what changed: - server/ or Cargo.* → rebuild + restart server container - docker-compose.yml → full stack up -d - proxy/Caddyfile → caddy reload - anything else → no restart needed start.sh now installs hiy-update.service + hiy-update.timer alongside the existing hiy.service boot unit. https://claude.ai/code/session_01FKCW3FDjNFj6jve4niMFXH --- infra/auto-update.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++ infra/start.sh | 34 +++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 infra/auto-update.sh 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" <