Refactor deployment scripts to improve Node.js version handling and PM2 process management
All checks were successful
Code Analysis and Production Deploy / analyze (push) Successful in 7m36s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Successful in 2m55s

- Updated `ensure_node_version` function in `deploy-production.sh` and `deploy-test.sh` to use a common script for Node.js version validation.
- Simplified PM2 instance restart logic by introducing `deploy_restart_pm2` function in a new `deploy-runtime.sh` script.
- Replaced direct PM2 commands with the new function to handle instance restarts and HTTP readiness checks.
- Modified PM2 configuration files to set the script directly instead of using `node` as the interpreter, allowing for better compatibility with older Node.js versions.
- Added tests for the new deployment runtime functions in `deploy-runtime.spec.ts` to ensure correct behavior during deployment.
- Updated `package-lock.json` to include new dependencies and ensure compatibility with the latest changes.
This commit is contained in:
Torsten Schulz (local)
2026-10-29 15:48:17 +01:00
parent 4ecc2cc90c
commit 10a37180de
7 changed files with 693 additions and 100 deletions

View File

@@ -137,13 +137,8 @@ ensure_node_version() {
local node_version
node_version="$(node -p 'process.versions.node')"
if ! node -e 'const [major, minor] = process.versions.node.split(".").map(Number); process.exit(major > 22 || (major === 22 && minor >= 12) ? 0 : 1)' >/dev/null 2>&1; then
echo "ERROR: Node.js >= 22.12.0 wird benötigt, aktuell ist $node_version aktiv."
echo "Bitte Node 22 installieren/aktivieren, z.B.:"
echo " nvm install 22"
echo " nvm alias default 22"
exit 1
fi
# Dieselbe Laufzeitprüfung wie beim Build und im Nitro-Server.
node "$SCRIPT_DIR/scripts/require-safe-node.cjs"
echo " Node.js $node_version"
}
@@ -544,29 +539,11 @@ if ! command -v pm2 &> /dev/null; then
exit 1
fi
# Funktion zum Starten/Neustarten einer PM2-Instanz
# Gemeinsamer Start mit der ausgewählten Node-Laufzeit und HTTP-Prüfung.
# shellcheck source=scripts/deploy-runtime.sh
source "$SCRIPT_DIR/scripts/deploy-runtime.sh"
restart_pm2_instance() {
local instance_name=$1
if ! pm2 describe "$instance_name" &> /dev/null; then
echo " WARNING: PM2-Prozess '$instance_name' existiert nicht."
echo " Versuche, den Prozess zu starten..."
if pm2 start harheimertc.config.cjs --only "$instance_name" --update-env; then
echo " ✓ PM2-Prozess '$instance_name' gestartet"
return 0
else
echo " ERROR: Konnte PM2-Prozess '$instance_name' nicht starten."
return 1
fi
else
echo " Restarting $instance_name with --update-env..."
if pm2 restart "$instance_name" --update-env; then
echo " ✓ PM2-Prozess '$instance_name' neu gestartet"
return 0
else
echo " ERROR: PM2-Restart für '$instance_name' fehlgeschlagen!"
return 1
fi
fi
deploy_restart_pm2 "$1" "$SCRIPT_DIR/harheimertc.config.cjs"
}
# Starte/Neustarte die Produktionsinstanz
@@ -576,23 +553,16 @@ if ! restart_pm2_instance "harheimertc"; then
INSTANCE_ERRORS=$((INSTANCE_ERRORS + 1))
fi
# Prüfe, ob der Prozess läuft
sleep 2
echo ""
echo " Checking PM2 instances status..."
if pm2 describe harheimertc | grep -q "online"; then
echo " ✓ PM2-Prozess 'harheimertc' läuft (online)"
else
echo " WARNING: PM2-Prozess 'harheimertc' ist nicht online. Prüfe Logs: pm2 logs harheimertc"
if ! deploy_wait_for_http "harheimertc" "http://127.0.0.1:3100/"; then
INSTANCE_ERRORS=$((INSTANCE_ERRORS + 1))
fi
if [ "$INSTANCE_ERRORS" -gt 0 ]; then
echo ""
echo "WARNING: Einige PM2-Instanzen haben Probleme. Bitte manuell prüfen:"
echo "ERROR: Produktionsinstanz konnte nicht erfolgreich gestartet werden. Bitte manuell prüfen:"
echo " pm2 status"
echo " pm2 logs harheimertc"
exit 1
fi
echo ""