Files
Bruchtal/scripts/redeploy-containers.sh
2026-03-10 22:24:11 +01:00

69 lines
2.0 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Auto-Restart Script für geänderte Docker-Compose Stacks
# Nur laufende, aktive Container werden neu gestartet
# Inaktive Container bleiben unberührt
# Logs im Repo-Verzeichnis
#test
REPO_DIR="/srv/docker"
LOGFILE="$REPO_DIR/scripts/docker-update.log"
# Liste der inaktiven Container
INACTIVE_CONTAINERS=("adguard" "kea" "caddy" "wikijs")
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') | $*" | tee -a "$LOGFILE"
}
log "===== Starting Auto-Restart (final) ====="
cd "$REPO_DIR" || { log "ERROR: Cannot enter $REPO_DIR"; exit 1; }
# 1⃣ Git Pull + Hard Reset (VM exakt auf Remote-Stand bringen)
git fetch --all &>/dev/null
git reset --hard origin/main &>/dev/null
log "Pulled latest changes and reset VM to remote state."
# 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 restart."
exit 0
fi
# 3⃣ Nur laufende, geänderte Container neu starten
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
# Prüfen, ob Container läuft
RUNNING=$(docker compose -f "$COMPOSE_DIR/docker-compose.yml" ps -q)
if [ -z "$RUNNING" ]; then
log "Container $CONTAINER_NAME is stopped. Skipping restart."
continue
fi
log "Restarting running container: $CONTAINER_NAME"
cd "$COMPOSE_DIR" || continue
docker compose up -d &>/dev/null
if [ $? -eq 0 ]; then
log "$CONTAINER_NAME restarted successfully"
else
log "❌ Failed to restart $CONTAINER_NAME"
fi
done
log "===== Auto-Restart Completed ====="