This commit is contained in:
overcuriousity 2025-08-07 10:06:39 +02:00
parent 73ace5965f
commit 95ce48192b

105
deploy.sh
View File

@ -34,27 +34,84 @@ if [ ! -f "package.json" ] || [ ! -f "astro.config.mjs" ]; then
exit 1 exit 1
fi fi
# Build application # Function to find and use npm
echo "📦 Building application..." find_and_use_npm() {
if [ ! -d "node_modules" ]; then echo "🔍 Searching for npm installation..."
echo "📦 Installing dependencies..."
sudo -u "$ORIGINAL_USER" npm install # Try system npm first
if command -v npm &> /dev/null; then
echo "✅ Found system npm: $(which npm)"
echo "📦 Installing dependencies..."
sudo -u "$ORIGINAL_USER" npm install
echo "📦 Building application..."
sudo -u "$ORIGINAL_USER" npm run build
return 0
fi
# Try nvm-installed npm
echo "🔍 Checking for nvm installation..."
if sudo -u "$ORIGINAL_USER" bash -c "
export NVM_DIR='$ORIGINAL_HOME/.nvm'
[ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh'
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
command -v npm &> /dev/null
"; then
echo "✅ Found nvm-managed npm"
echo "📦 Installing dependencies with nvm..."
sudo -u "$ORIGINAL_USER" bash -c "
export NVM_DIR='$ORIGINAL_HOME/.nvm'
[ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh'
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
npm install
"
echo "📦 Building application with nvm..."
sudo -u "$ORIGINAL_USER" bash -c "
export NVM_DIR='$ORIGINAL_HOME/.nvm'
[ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh'
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
npm run build
"
return 0
fi
echo "❌ npm not found in system or user environment"
echo ""
echo "💡 Please install Node.js and npm first:"
echo " # Option 1: System package manager"
echo " sudo apt update && sudo apt install nodejs npm"
echo ""
echo " # Option 2: NodeSource repository (recommended)"
echo " curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -"
echo " sudo apt-get install -y nodejs"
echo ""
echo " # Option 3: nvm (as user $ORIGINAL_USER)"
echo " curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash"
echo " source ~/.bashrc"
echo " nvm install 20"
echo ""
return 1
}
# Check for existing build or build if needed
if [ ! -d "dist" ] || [ ! "$(ls -A dist 2>/dev/null)" ]; then
echo "📦 No dist/ directory found, building..."
if ! find_and_use_npm; then
exit 1
fi
else
echo "📦 Found existing dist/ directory"
read -p "🤔 Rebuild application? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if ! find_and_use_npm; then
echo ""
echo "💡 Using existing dist/ due to build failure"
fi
else
echo "📦 Using existing build"
fi
fi fi
# Build with proper user context
sudo -u "$ORIGINAL_USER" bash -c "
# Load user environment
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
[ -s '$ORIGINAL_HOME/.profile' ] && source '$ORIGINAL_HOME/.profile'
# Load nvm if available
export NVM_DIR='$ORIGINAL_HOME/.nvm'
[ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh'
# Build
npm run build
"
# Verify build succeeded # Verify build succeeded
if [ ! -d "dist" ] || [ ! "$(ls -A dist 2>/dev/null)" ]; then if [ ! -d "dist" ] || [ ! "$(ls -A dist 2>/dev/null)" ]; then
echo "❌ Error: Build failed or dist/ is empty" echo "❌ Error: Build failed or dist/ is empty"
@ -98,13 +155,9 @@ fi
# Setup environment configuration # Setup environment configuration
echo "🔧 Setting up environment configuration..." echo "🔧 Setting up environment configuration..."
if [ -f "$WEBROOT/.env" ]; then cp .env.example "$WEBROOT/.env"
echo "📝 Existing .env found, keeping current configuration" echo "✅ Created .env from .env.example template"
else echo "⚠️ IMPORTANT: Edit $WEBROOT/.env with your configuration"
echo "📝 Creating new .env from template..."
cp .env.example "$WEBROOT/.env"
echo "⚠️ IMPORTANT: Edit $WEBROOT/.env with your configuration"
fi
# Create log files # Create log files
echo "📝 Creating log files..." echo "📝 Creating log files..."