#!/bin/bash # Deployment Script für Harheimer TC Website # Sichert Produktivdaten vor dem Build und stellt sie danach wieder her echo "=== Harheimer TC Deployment ===" echo "" # 1. Handle local changes and Git Pull echo "1. 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)" # Pull latest changes echo " Pulling latest changes..." git pull if [ $? -ne 0 ]; then echo "ERROR: Git pull failed!" exit 1 fi # 2. Backup Production Data (from stash) echo "" echo "2. Backing up production data from stash..." # Create backup directory mkdir -p .backup # Try to restore stashed data temporarily to backup it echo " Restoring stashed data for backup..." git stash show -p | git apply --index 2>/dev/null || echo " No stashed data to restore" # Backup production data cp -r server/data .backup/data_backup 2>/dev/null || echo " No server/data to backup" cp public/data/termine.csv .backup/termine.csv 2>/dev/null || echo " No termine.csv to backup" cp public/data/mannschaften.csv .backup/mannschaften.csv 2>/dev/null || echo " No mannschaften.csv to backup" cp public/data/spielsysteme.csv .backup/spielsysteme.csv 2>/dev/null || echo " No spielsysteme.csv to backup" cp public/data/vereinsmeisterschaften.csv .backup/vereinsmeisterschaften.csv 2>/dev/null || echo " No vereinsmeisterschaften.csv to backup" # Reset any changes from stash restore git reset --hard HEAD # 3. Install dependencies echo "" echo "3. Installing dependencies..." npm install if [ $? -ne 0 ]; then echo "ERROR: npm install failed!" exit 1 fi # 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 if [ $? -ne 0 ]; then echo "ERROR: Build failed!" exit 1 fi # 6. Restore Production Data echo "" echo "6. Restoring production data..." cp -r .backup/data_backup/* server/data/ 2>/dev/null || echo "No data to restore" cp .backup/termine.csv public/data/termine.csv 2>/dev/null || echo "No termine.csv to restore" cp .backup/mannschaften.csv public/data/mannschaften.csv 2>/dev/null || echo "No mannschaften.csv to restore" cp .backup/spielsysteme.csv public/data/spielsysteme.csv 2>/dev/null || echo "No spielsysteme.csv to restore" cp .backup/vereinsmeisterschaften.csv public/data/vereinsmeisterschaften.csv 2>/dev/null || echo "No vereinsmeisterschaften.csv to restore" # 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."