#!/bin/bash # Auto-update Docker-Compose stacks nach Git-Pull # Läuft als dockeradmin, prüft geänderte Compose-Dateien # Stillgelegte Container werden ignoriert REPO_DIR="/srv/docker" LOGFILE="/var/log/docker-update.log" INACTIVE_CONTAINERS=("adguard" "kea" "caddy" "wikijs") log() { echo "$(date '+%Y-%m-%d %H:%M:%S') | $*" | tee -a "$LOGFILE" } log "===== Starting Auto-Update (changed Compose only) =====" cd "$REPO_DIR" || { log "ERROR: Cannot enter $REPO_DIR"; exit 1; } # 1️⃣ Pull neueste Änderungen git fetch --all &>/dev/null git reset --hard origin/main &>/dev/null log "Pulled latest changes from Git." # 2️⃣ Geänderte Compose-Dateien ermitteln CHANGED=$(git diff --name-only HEAD~1 HEAD | grep -E '^compose/.+/docker-compose\.yml$' || true) if [ -z "$CHANGED" ]; then log "No Compose files changed. Nothing to update." exit 0 fi # 3️⃣ Nur geänderte Container updaten for FILE in $CHANGED; do CONTAINER_NAME=$(echo "$FILE" | cut -d'/' -f2) # Inaktive Container überspringen if [[ " ${INACTIVE_CONTAINERS[@]} " =~ " ${CONTAINER_NAME} " ]]; then log "Skipping inactive container: $CONTAINER_NAME" continue fi COMPOSE_DIR="$REPO_DIR/compose/$CONTAINER_NAME" if [ ! -d "$COMPOSE_DIR" ]; then log "Warning: $COMPOSE_DIR does not exist, skipping..." continue fi log "Updating container: $CONTAINER_NAME" cd "$COMPOSE_DIR" || continue docker compose pull &>/dev/null docker compose up -d &>/dev/null if [ $? -eq 0 ]; then log "✅ $CONTAINER_NAME updated successfully" else log "❌ Failed to update $CONTAINER_NAME" fi done log "===== Auto-Update Completed ====="