refactor(update scripts): enhance directory handling and error management
Some checks failed
Deploy to production / deploy (push) Failing after 1m46s

- Updated update-backend.sh and update-frontend.sh to accept a target directory as a parameter, improving flexibility.
- Refactored paths to use the target directory for all operations, ensuring consistency and reducing hardcoded values.
- Added error handling with 'set -euo pipefail' for better script reliability.
- Improved user feedback by displaying the target directory during updates.
This commit is contained in:
Torsten Schulz (local)
2026-03-30 13:19:11 +02:00
parent 5001292616
commit b2591da428
4 changed files with 71 additions and 37 deletions

View File

@@ -1,6 +1,11 @@
#!/bin/bash
set -euo pipefail
TARGET_DIR="${1:?Zielverzeichnis fehlt: ./update-frontend.sh <z. B. /opt/yourpart-green>}"
FRONTEND_TARGET="$TARGET_DIR/frontend"
echo "=== YourPart Frontend Update ==="
echo "Ziel: $FRONTEND_TARGET"
echo "NOTE: .env files will NOT be overwritten"
# 1. Zum Frontend-Verzeichnis wechseln
@@ -21,9 +26,9 @@ rm -rf node_modules/.cache/
# 4. Verwende bestehende .env-Dateien im Zielverzeichnis für Build
# Kopiere temporär die bestehende .env vom Ziel, falls vorhanden
TEMP_ENV="/tmp/yourpart-frontend-env-$$"
if [ -f /opt/yourpart/frontend/.env ]; then
if [ -f "$FRONTEND_TARGET/.env" ]; then
echo "Nutze bestehende .env-Datei vom Zielverzeichnis für Build..."
sudo cp /opt/yourpart/frontend/.env "$TEMP_ENV"
sudo cp "$FRONTEND_TARGET/.env" "$TEMP_ENV"
sudo chown $USER:$USER "$TEMP_ENV"
cp "$TEMP_ENV" .env
elif [ -f .env.production ]; then
@@ -73,36 +78,36 @@ rm -f "$TEMP_ENV"
# 6. Zielverzeichnis erstellen (falls nicht vorhanden)
echo "Erstelle Zielverzeichnis..."
sudo mkdir -p /opt/yourpart/frontend/dist
sudo mkdir -p "$FRONTEND_TARGET/dist"
# 7. Altes Frontend löschen (außer .env-Dateien)
echo "Lösche altes Frontend (außer .env-Dateien)..."
sudo find /opt/yourpart/frontend/dist -mindepth 1 -exec rm -rf {} + 2>/dev/null || true
sudo find "$FRONTEND_TARGET/dist" -mindepth 1 -exec rm -rf {} + 2>/dev/null || true
# 8. Neues Frontend kopieren
echo "Kopiere neues Frontend..."
sudo cp -r dist/* /opt/yourpart/frontend/dist/
sudo cp -r dist/* "$FRONTEND_TARGET/dist/"
# 9. .env-Dateien NICHT überschreiben - bestehende beibehalten
if [ -f /opt/yourpart/frontend/.env ]; then
if [ -f "$FRONTEND_TARGET/.env" ]; then
echo "✓ Bestehende .env-Datei wurde beibehalten (nicht überschrieben)"
else
echo "⚠ Keine .env-Datei im Zielverzeichnis gefunden"
# Falls .env im Quellverzeichnis existiert, kopiere sie nur wenn sie im Ziel nicht existiert
if [ -f .env ]; then
echo "Kopiere .env-Datei (nur wenn nicht vorhanden)..."
sudo cp .env /opt/yourpart/frontend/
sudo cp .env "$FRONTEND_TARGET/"
fi
fi
# 10. Berechtigungen setzen
echo "Setze Berechtigungen..."
sudo chown -R www-data:www-data /opt/yourpart/frontend/dist
if [ -f /opt/yourpart/frontend/.env ]; then
sudo chown www-data:www-data /opt/yourpart/frontend/.env
sudo chmod 644 /opt/yourpart/frontend/.env
sudo chown -R www-data:www-data "$FRONTEND_TARGET/dist"
if [ -f "$FRONTEND_TARGET/.env" ]; then
sudo chown www-data:www-data "$FRONTEND_TARGET/.env"
sudo chmod 644 "$FRONTEND_TARGET/.env"
fi
sudo chmod -R 755 /opt/yourpart/frontend/dist
sudo chmod -R 755 "$FRONTEND_TARGET/dist"
# 11. Apache neu laden
echo "Lade Apache neu..."
@@ -115,4 +120,3 @@ echo "✅ Frontend aktualisiert"
echo "✅ .env-Dateien wurden NICHT überschrieben"
echo "✅ Apache neu geladen"
echo ""