Files

199 lines
6.3 KiB
Bash
Executable File

#!/bin/bash
# Comprehensive Build, Test, and Deploy Script
# Usage: ./build-test-deploy.sh
set -e # Exit on error
CYAN='\033[0;36m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN} Build, Test & Deploy - Tilbudgivern${NC}"
echo -e "${CYAN}========================================${NC}"
echo ""
# Function to print step
print_step() {
echo -e "${CYAN}$1${NC}"
}
# Function to print success
print_success() {
echo -e "${GREEN}$1${NC}"
}
# Function to print warning
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
# Function to print error
print_error() {
echo -e "${RED}$1${NC}"
}
# 1. Set working directory
print_step "Setting working directory..."
cd /mnt/HC_Volume_103713257/tilbudgivern
print_success "Working directory set to: $(pwd)"
# 2. Check database
print_step "Testing database connection..."
if mysql -u tilbudgivern_service -p"${DB_PASSWORD}" tilbudgivern -e "SELECT 1;" &>/dev/null; then
print_success "Database connection OK"
else
print_error "Database connection failed"
exit 1
fi
# 3. Check material_packages columns
print_step "Verifying material_packages table schema..."
COLUMNS=$(mysql -u tilbudgivern_service -p"${DB_PASSWORD}" tilbudgivern -e "SHOW COLUMNS FROM material_packages WHERE Field IN ('package_type', 'difficulty_level', 'estimated_hours');" -sN | wc -l)
if [ "$COLUMNS" -eq 3 ]; then
print_success "Table schema OK (all required columns present)"
else
print_warning "Table schema incomplete, adding missing columns..."
mysql -u tilbudgivern_service -p"${DB_PASSWORD}" tilbudgivern << 'EOF'
ALTER TABLE material_packages
ADD COLUMN IF NOT EXISTS package_type VARCHAR(50) DEFAULT 'standard',
ADD COLUMN IF NOT EXISTS difficulty_level VARCHAR(20) DEFAULT 'medium',
ADD COLUMN IF NOT EXISTS estimated_hours DECIMAL(10,2) DEFAULT 0.00,
ADD COLUMN IF NOT EXISTS hourly_rate DECIMAL(10,2) DEFAULT 0.00,
ADD COLUMN IF NOT EXISTS installation_notes TEXT,
ADD COLUMN IF NOT EXISTS is_template TINYINT(1) DEFAULT 0,
ADD COLUMN IF NOT EXISTS popularity_score INT DEFAULT 0;
EOF
print_success "Table schema updated"
fi
# 4. Check unified-server.js database config
print_step "Checking unified-server.js database configuration..."
if grep -q "database: process.env.DB_NAME" unified-server.js; then
print_success "Database config uses environment variable ✓"
elif grep -q "database: 'ordrestyring_local'" unified-server.js; then
print_error "Database hardcoded to ordrestyring_local - fixing..."
sed -i "s/database: 'ordrestyring_local'/database: process.env.DB_NAME || 'tilbudgivern'/g" unified-server.js
print_success "Database config fixed"
else
print_success "Database config OK"
fi
# 5. Build frontend
print_step "Building frontend..."
cd frontend
if npm run build 2>&1 | tee build.log | tail -20; then
print_success "Frontend build completed"
BUILD_SIZE=$(du -sh build | cut -f1)
echo -e " ${GREEN}Build size: ${BUILD_SIZE}${NC}"
else
print_error "Frontend build failed"
exit 1
fi
cd ..
# 6. Stop existing PM2 processes
print_step "Stopping existing unified-server..."
pm2 stop unified-server 2>/dev/null || true
pm2 delete unified-server 2>/dev/null || true
print_success "Old processes stopped"
# 7. Start server with correct environment
print_step "Starting unified-server on port 4031..."
cd /mnt/HC_Volume_103713257/tilbudgivern
PORT=4031 pm2 start unified-server.js --name unified-server --update-env
# Wait for server to start
print_step "Waiting for server to initialize..."
sleep 8
# 8. Check if port is listening
print_step "Verifying server is listening on port 4031..."
if lsof -i :4031 &>/dev/null || ss -tuln | grep -q :4031; then
print_success "Server listening on port 4031 ✓"
else
print_error "Server NOT listening on port 4031"
pm2 logs unified-server --lines 50 --nostream
exit 1
fi
# 9. Test API endpoints
print_step "Testing API endpoints..."
# Test health
if curl -s http://localhost:4031/ | grep -q "html"; then
print_success "Frontend serving OK"
else
print_warning "Frontend check unclear"
fi
# Test smart-packages API
print_step "Testing /api/smart-packages endpoint..."
RESPONSE=$(curl -s http://localhost:4031/api/smart-packages?limit=1)
if echo "$RESPONSE" | jq -e '.success' &>/dev/null; then
PACKAGE_COUNT=$(echo "$RESPONSE" | jq -r '.packages | length')
print_success "Smart packages API working (${PACKAGE_COUNT} packages returned)"
else
print_error "Smart packages API failed"
echo "$RESPONSE" | jq '.' || echo "$RESPONSE"
exit 1
fi
# Test categories endpoint
print_step "Testing /api/smart-packages/categories endpoint..."
CATEGORIES=$(curl -s http://localhost:4031/api/smart-packages/categories)
if echo "$CATEGORIES" | jq -e '.success' &>/dev/null; then
CAT_COUNT=$(echo "$CATEGORIES" | jq -r '.categories | length')
print_success "Categories API working (${CAT_COUNT} categories)"
else
print_warning "Categories API check unclear"
fi
# 10. Test nginx configuration
print_step "Testing nginx configuration..."
if sudo nginx -t 2>&1 | grep -q "successful"; then
print_success "Nginx config valid"
else
print_error "Nginx config invalid"
sudo nginx -t
exit 1
fi
# 11. Reload nginx
print_step "Reloading nginx..."
if sudo systemctl reload nginx; then
print_success "Nginx reloaded"
else
print_warning "Nginx reload had issues"
fi
# 12. Final status check
print_step "Checking PM2 status..."
pm2 list
# 13. Show recent logs
print_step "Recent server logs (last 20 lines)..."
pm2 logs unified-server --lines 20 --nostream | tail -20
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} ✅ Build & Deploy Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "🌐 Frontend: ${CYAN}https://tilbudsgiveren.alw.dk${NC}"
echo -e "🔧 API: ${CYAN}http://localhost:4031/api${NC}"
echo -e "📦 Smart Packages: ${CYAN}https://tilbudsgiveren.alw.dk/smart-packages${NC}"
echo ""
echo -e "💡 ${YELLOW}Next steps:${NC}"
echo -e " 1. Open browser console (F12)"
echo -e " 2. Go to https://tilbudsgiveren.alw.dk/smart-packages"
echo -e " 3. Watch for console logs starting with 🔵"
echo -e " 4. Click 'Opret ny pakke' button"
echo ""
echo -e "📝 ${YELLOW}Monitor logs:${NC}"
echo -e " pm2 logs unified-server --lines 100"
echo ""