Update deploy-to-opt.sh to enhance npm cache management by excluding .npm and .npm-cache directories, and setting a custom npm cache directory during dependency installation and client build.

This commit is contained in:
Torsten Schulz (local)
2025-12-04 17:01:51 +01:00
parent c63c26bfe4
commit 34df89c1ac
2 changed files with 75 additions and 3 deletions

View File

@@ -38,7 +38,7 @@ if [ ! -d "$TARGET_DIR" ]; then
echo "✓ Verzeichnis erstellt"
fi
# Kopiere Dateien (ausschließlich node_modules, .git, dist, logs)
# Kopiere Dateien (ausschließlich node_modules, .git, dist, logs, npm cache)
echo "Kopiere Dateien nach $TARGET_DIR..."
rsync -av --progress \
--exclude 'node_modules' \
@@ -48,6 +48,8 @@ rsync -av --progress \
--exclude 'docroot/dist' \
--exclude 'logs' \
--exclude '.env' \
--exclude '.npm-cache' \
--exclude '.npm' \
"$SOURCE_DIR/" "$TARGET_DIR/"
echo "✓ Dateien kopiert"
@@ -62,10 +64,22 @@ echo ""
echo "Führe Installation in $TARGET_DIR durch..."
cd "$TARGET_DIR"
# Repariere npm Cache für www-data falls nötig
echo ""
echo "Prüfe npm Cache-Berechtigungen..."
if [ -d "/var/www/.npm" ]; then
chown -R $USER:$GROUP "/var/www/.npm" 2>/dev/null || true
fi
# Setze npm Cache auf ein Verzeichnis im App-Verzeichnis
NPM_CACHE_DIR="$TARGET_DIR/.npm-cache"
mkdir -p "$NPM_CACHE_DIR"
chown -R $USER:$GROUP "$NPM_CACHE_DIR"
# Installiere Dependencies
echo ""
echo "Installiere Dependencies..."
sudo -u $USER bash -c "cd '$TARGET_DIR' && npm run install:all"
sudo -u $USER bash -c "cd '$TARGET_DIR' && npm config set cache '$NPM_CACHE_DIR' && npm run install:all"
if [ $? -ne 0 ]; then
echo "FEHLER: Installation der Dependencies fehlgeschlagen!"
@@ -77,7 +91,7 @@ echo "✓ Dependencies installiert"
# Baue Client
echo ""
echo "Baue Client für Production..."
sudo -u $USER bash -c "cd '$TARGET_DIR' && npm run build"
sudo -u $USER bash -c "cd '$TARGET_DIR' && npm config set cache '$NPM_CACHE_DIR' && cd client && npm config set cache '$NPM_CACHE_DIR' && cd .. && npm run build"
if [ $? -ne 0 ]; then
echo "FEHLER: Build fehlgeschlagen!"

58
fix-npm-cache.sh Executable file
View File

@@ -0,0 +1,58 @@
#!/bin/bash
# Fix npm Cache-Berechtigungen für /opt/ypchat
set -e
TARGET_DIR="/opt/ypchat"
USER="www-data"
GROUP="www-data"
echo "=========================================="
echo "npm Cache-Berechtigungen reparieren"
echo "=========================================="
# Prüfe ob als root ausgeführt
if [ "$EUID" -ne 0 ]; then
echo "FEHLER: Dieses Skript muss als root ausgeführt werden!"
echo "Bitte führe aus: sudo ./fix-npm-cache.sh"
exit 1
fi
# Erstelle lokales npm Cache-Verzeichnis
NPM_CACHE_DIR="$TARGET_DIR/.npm-cache"
echo "Erstelle lokales npm Cache-Verzeichnis: $NPM_CACHE_DIR"
mkdir -p "$NPM_CACHE_DIR"
chown -R $USER:$GROUP "$NPM_CACHE_DIR"
echo "✓ Cache-Verzeichnis erstellt"
# Setze npm Cache für root (falls nötig)
echo "Setze npm Cache-Konfiguration..."
npm config set cache "$NPM_CACHE_DIR" --global 2>/dev/null || true
# Setze npm Cache für www-data
echo "Setze npm Cache für $USER..."
sudo -u $USER bash -c "npm config set cache '$NPM_CACHE_DIR'"
sudo -u $USER bash -c "cd '$TARGET_DIR/client' && npm config set cache '$NPM_CACHE_DIR'"
echo "✓ npm Cache konfiguriert"
# Optional: Repariere /var/www/.npm falls vorhanden
if [ -d "/var/www/.npm" ]; then
echo "Repariere /var/www/.npm Berechtigungen..."
chown -R $USER:$GROUP "/var/www/.npm" 2>/dev/null || true
echo "✓ /var/www/.npm repariert"
else
echo " /var/www/.npm existiert nicht (ok, wird nicht benötigt)"
fi
echo ""
echo "=========================================="
echo "npm Cache repariert!"
echo "=========================================="
echo ""
echo "Du kannst jetzt die Installation fortsetzen:"
echo " cd $TARGET_DIR"
echo " sudo -u $USER npm run install:all"
echo ""