Files
harheimertc/deploy-production.sh

160 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
# Immer im Repo-Verzeichnis arbeiten (wichtig für Backup/Restore mit relativen Pfaden)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Deployment Script für Harheimer TC Website
# Sichert Produktivdaten vor dem Build und stellt sie danach wieder her
echo "=== Harheimer TC Deployment ==="
echo ""
echo "Working directory: $(pwd)"
echo ""
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "ERROR: Dieses Script muss im Git-Repository ausgeführt werden (kein .git gefunden)."
exit 1
fi
# Optional (empfohlen): Persistente Daten außerhalb des Git-Repos halten und per Symlink einbinden.
# Das verhindert zuverlässig, dass Git jemals Produktivdaten überschreibt.
DATA_ROOT="${DATA_ROOT:-/var/lib/harheimertc}"
mkdir -p "$DATA_ROOT"
ensure_symlink_dir() {
local src="$1" # z.B. server/data
local target="$2" # z.B. /var/lib/harheimertc/server-data
mkdir -p "$(dirname "$src")"
mkdir -p "$target"
if [ -L "$src" ]; then
return 0
fi
if [ -d "$src" ]; then
echo " Moving $src -> $target (first-time migration)"
# Merge existing content into target
cp -a "$src/." "$target/" || true
rm -rf "$src"
fi
ln -s "$target" "$src"
echo " Linked $src -> $target"
}
echo "0. Ensuring persistent data directories (recommended)..."
ensure_symlink_dir "server/data" "$DATA_ROOT/server-data"
ensure_symlink_dir "public/data" "$DATA_ROOT/public-data"
ensure_symlink_dir "public/uploads" "$DATA_ROOT/public-uploads"
echo ""
# 1. BACKUP: Laufende Produktivdaten VOR allen Git-Operationen sichern
echo "1. Backing up current production data (pre-git)..."
# Fresh backup dir
rm -rf .backup
mkdir -p .backup
# Backup server data (JSON) und CSVs immer vom Dateisystem, nicht aus 'stash'
if [ -d server/data ]; then
cp -a server/data .backup/data_backup
echo " Backed up server/data -> .backup/data_backup"
else
echo "ERROR: server/data existiert nicht. Abbruch, damit wir keine Repo-Defaults ausrollen."
exit 1
fi
mkdir -p .backup/public_data
if ls public/data/*.csv >/dev/null 2>&1; then
cp -a public/data/*.csv .backup/public_data/
echo " Backed up public/data/*.csv -> .backup/public_data/"
else
echo " No public CSVs to backup (public/data/*.csv not found)"
fi
# 2. Handle local changes and Git Pull
echo "2. Handling local changes and pulling latest from git..."
# Check if there are merge conflicts first
if [ -n "$(git status --porcelain | grep '^UU\|^AA\|^DD')" ]; then
echo " Resolving existing merge conflicts..."
git reset --hard HEAD
fi
# Stash any local changes (including production data)
echo " Stashing local changes..."
git stash push -m "Production deployment stash $(date)" || true
# Pull latest changes
echo " Pulling latest changes..."
git pull
# Reset any accidental changes from stash restore (should be none now)
git reset --hard HEAD >/dev/null 2>&1
# 3. Install dependencies
echo ""
echo "3. Installing dependencies..."
npm install
# 4. Remove old build (but keep data!)
echo ""
echo "4. Removing old build output..."
rm -rf .output
# 5. Build
echo ""
echo "5. Building application..."
npm run build
# 6. Restore Production Data (überschreibe Repo-Defaults mit Backup)
echo ""
echo "6. Restoring production data..."
# Stelle server/data vollständig wieder her (inkl. config.json, users.json, news.json, sessions.json, members.json, membership-applications)
if [ ! -d .backup/data_backup ]; then
echo "ERROR: Backup-Verzeichnis .backup/data_backup fehlt. Abbruch."
exit 1
fi
mkdir -p server/data
cp -a .backup/data_backup/. server/data/
echo " Restored server/data from backup."
# Stelle alle CSVs wieder her
if ls .backup/public_data/*.csv >/dev/null 2>&1; then
mkdir -p public/data
cp -a .backup/public_data/*.csv public/data/
echo " Restored public/data/*.csv from backup."
else
echo "No public CSVs to restore"
fi
# Sanity Check: users.json muss existieren und darf nicht leer sein
if [ ! -s server/data/users.json ]; then
echo "ERROR: server/data/users.json fehlt oder ist leer nach Restore. Abbruch."
exit 1
fi
# 7. Cleanup backup and stash
echo ""
echo "7. Cleaning up backup and stash..."
rm -rf .backup
# Clear the deployment stash (keep other stashes)
echo " Clearing deployment stash..."
git stash list | grep "Production deployment stash" | head -1 | cut -d: -f1 | xargs -r git stash drop
# 8. Restart PM2
echo ""
echo "8. Restarting PM2..."
pm2 restart harheimertc
echo ""
echo "=== Deployment completed successfully! ==="
echo "The application is now running with the latest code and your production data preserved."