Auto-persist APP_KEY in storage volume across redeploys

- Key saved to storage/app/.app_key (persistent volume)
- On redeploy: restored automatically from volume
- On first deploy: generated and saved
- No manual Dokploy env var needed
This commit is contained in:
root
2026-04-05 20:28:31 +02:00
parent 086cce4617
commit bd145607b0

View File

@@ -59,21 +59,26 @@ fi
echo "[✓] .env file ready."
# -----------------------------------------------
# 2. Application key
# 2. Application key (persisted in storage volume)
# -----------------------------------------------
if grep -q "^APP_KEY=$" .env; then
KEY_FILE="/var/www/html/storage/app/.app_key"
if [ -f "$KEY_FILE" ]; then
# Restore key from persistent volume
STORED_KEY=$(cat "$KEY_FILE")
sed -i "s|^APP_KEY=.*|APP_KEY=${STORED_KEY}|" .env
echo "[✓] Application key restored from storage."
elif grep -q "^APP_KEY=$" .env; then
# No stored key and .env has empty key: generate new one
echo "[*] Generating application key..."
php artisan key:generate --ansi
echo ""
echo "╔══════════════════════════════════════════════════════╗"
echo "║ IMPORTANT: Save this APP_KEY in Dokploy env vars! ║"
echo "║ Otherwise encrypted data will be lost on redeploy. ║"
echo "╚══════════════════════════════════════════════════════╝"
echo ""
echo " APP_KEY=$(grep '^APP_KEY=' .env | cut -d= -f2-)"
echo ""
# Save to persistent volume
grep '^APP_KEY=' .env | cut -d= -f2- > "$KEY_FILE"
echo "[✓] Application key saved to persistent storage."
else
echo "[✓] Application key already set."
# .env already has a key (from env var override or .env.example), persist it
grep '^APP_KEY=' .env | cut -d= -f2- > "$KEY_FILE"
echo "[✓] Application key already set, saved to persistent storage."
fi
# -----------------------------------------------