-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-deploy.sh
More file actions
executable file
·115 lines (99 loc) · 3.94 KB
/
Copy pathauto-deploy.sh
File metadata and controls
executable file
·115 lines (99 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
#
# auto-deploy.sh — Lokaler Polling-Deploy für OpenSIN-Chat
# -----------------------------------------------------------------------------
# Holt regelmäßig den neuesten Stand von origin/main. Wenn sich etwas geändert
# hat, wird das Docker-Image NEU gebaut (--no-cache erzwingt ein frisches
# Frontend-Bundle) und der Container neu gestartet.
#
# Gedacht für einen lokalen Mac, der den Container hostet und von außen nicht
# direkt erreichbar ist. Wird per cron / launchd alle paar Minuten ausgeführt.
#
# Einrichtung siehe: docs/AUTO-DEPLOY.md
# -----------------------------------------------------------------------------
set -euo pipefail
# --- Konfiguration (bei Bedarf anpassen) ------------------------------------
# Wurzel des Repos. Standard: zwei Ebenen über diesem Skript (scripts/..).
REPO_DIR="${OPENSIN_REPO_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
BRANCH="${OPENSIN_BRANCH:-main}"
COMPOSE_FILE="${OPENSIN_COMPOSE_FILE:-docker-opensin/docker-compose.yml}"
HEALTH_URL="${OPENSIN_HEALTH_URL:-http://localhost:43939/api/ping}"
LOCK_FILE="${OPENSIN_LOCK_FILE:-/tmp/opensin-chat-auto-deploy.lock}"
LOG_FILE="${OPENSIN_LOG_FILE:-$REPO_DIR/logs/auto-deploy.log}"
# docker compose vs. docker-compose automatisch erkennen
if docker compose version >/dev/null 2>&1; then
COMPOSE="docker compose"
elif command -v docker-compose >/dev/null 2>&1; then
COMPOSE="docker-compose"
else
echo "FEHLER: weder 'docker compose' noch 'docker-compose' gefunden" >&2
exit 1
fi
# --- Logging ----------------------------------------------------------------
mkdir -p "$(dirname "$LOG_FILE")"
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"; }
# --- Nur eine Instanz gleichzeitig ------------------------------------------
# Verhindert, dass sich zwei cron-Läufe überlappen, während gebaut wird.
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log "Ein anderer Deploy-Lauf ist aktiv — überspringe."
exit 0
fi
cd "$REPO_DIR"
# --- Änderungen prüfen ------------------------------------------------------
log "Prüfe origin/$BRANCH auf neue Commits…"
git fetch origin "$BRANCH" --quiet
LOCAL="$(git rev-parse "$BRANCH" 2>/dev/null || echo none)"
REMOTE="$(git rev-parse "origin/$BRANCH")"
if [ "$LOCAL" = "$REMOTE" ]; then
log "Keine Änderungen ($LOCAL). Nichts zu tun."
exit 0
fi
log "Neue Version gefunden: $LOCAL -> $REMOTE. Starte Deploy."
# --- Code aktualisieren -----------------------------------------------------
git checkout "$BRANCH" --quiet
git reset --hard "origin/$BRANCH" --quiet
log "Code auf origin/$BRANCH gesetzt."
# --- Image neu bauen & Container neu starten --------------------------------
# --no-cache ist entscheidend: ohne ihn kann Docker den Frontend-Build-Layer
# aus dem Cache wiederverwenden und das alte Bundle bliebe online.
log "Baue Image neu (--no-cache)…"
$COMPOSE -f "$COMPOSE_FILE" build --no-cache
log "Starte Container neu…"
$COMPOSE -f "$COMPOSE_FILE" up -d
# --- Healthcheck ------------------------------------------------------------
log "Warte auf Healthcheck ($HEALTH_URL)…"
ok=0
for i in $(seq 1 30); do
if curl -fsS "$HEALTH_URL" >/dev/null 2>&1; then
ok=1
break
fi
sleep 3
done
if [ "$ok" = "1" ]; then
log "Deploy erfolgreich. Live auf neuer Version $REMOTE."
else
log "WARNUNG: Healthcheck nicht bestanden. Versuche Rollback auf $LOCAL…"
log " $COMPOSE -f $COMPOSE_FILE logs --tail=100"
git reset --hard "$LOCAL" --quiet
log "Code auf $LOCAL zurückgesetzt. Baue altes Image neu…"
$COMPOSE -f "$COMPOSE_FILE" build --no-cache
$COMPOSE -f "$COMPOSE_FILE" up -d
rollback_ok=0
for i in $(seq 1 30); do
if curl -fsS "$HEALTH_URL" >/dev/null 2>&1; then
rollback_ok=1
break
fi
sleep 3
done
if [ "$rollback_ok" = "1" ]; then
log "Rollback erfolgreich. Live auf $LOCAL."
else
log "FEHLER: Rollback ebenfalls fehlgeschlagen. Manuelle Intervention nötig."
exit 2
fi
exit 1
fi