#!/bin/bash ############################################################################### # LuckyChit Frontend Deployment Script # Deploys Flutter Web App to Nginx ############################################################################### set -e # Exit on error # Colors GREEN='\033[0;32m' BLUE='\033[0;34m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Configuration SERVER_IP="192.168.8.148" API_URL="http://${SERVER_IP}:3000/api" PROJECT_DIR="/home/luckychit/apps/chitfund/luckychit" WEB_ROOT="/var/www/luckychit" API_SERVICE_FILE="lib/core/services/api_service.dart" print_info() { echo -e "${BLUE}ℹ️ $1${NC}" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } print_header() { echo "" echo -e "${GREEN}╔════════════════════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ LuckyChit Frontend Deployment Script ║${NC}" echo -e "${GREEN}╚════════════════════════════════════════════════════════╝${NC}" echo "" } ############################################################################### # Main Deployment ############################################################################### print_header # Check if running as root if [ "$EUID" -ne 0 ]; then print_error "Please run as root (sudo ./deploy-frontend.sh)" exit 1 fi print_info "Deployment Configuration:" echo " Server IP: $SERVER_IP" echo " API URL: $API_URL" echo " Project Dir: $PROJECT_DIR" echo " Web Root: $WEB_ROOT" echo "" read -p "Continue with deployment? (y/n) " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_error "Deployment cancelled" exit 1 fi ############################################################################### # Step 1: Navigate to project directory ############################################################################### print_info "Step 1/8: Navigating to project directory..." cd $PROJECT_DIR print_success "In directory: $(pwd)" ############################################################################### # Step 2: Create backup of current API service ############################################################################### print_info "Step 2/8: Creating backup of API service..." if [ -f "$API_SERVICE_FILE" ]; then cp $API_SERVICE_FILE ${API_SERVICE_FILE}.backup print_success "Backup created: ${API_SERVICE_FILE}.backup" else print_error "API service file not found: $API_SERVICE_FILE" exit 1 fi ############################################################################### # Step 3: Update API URL ############################################################################### print_info "Step 3/8: Updating API URL to $API_URL..." # Replace localhost URL with server IP sed -i "s|http://localhost:3000/api|$API_URL|g" $API_SERVICE_FILE # Verify the change if grep -q "$API_URL" $API_SERVICE_FILE; then print_success "API URL updated successfully" else print_error "Failed to update API URL" exit 1 fi ############################################################################### # Step 4: Update Flutter ############################################################################### print_info "Step 4/8: Updating Flutter..." cd /opt/flutter git pull origin stable flutter upgrade print_success "Flutter updated: $(flutter --version | head -n 1)" ############################################################################### # Step 5: Get dependencies ############################################################################### print_info "Step 5/8: Getting Flutter dependencies..." cd $PROJECT_DIR flutter pub get print_success "Dependencies installed" ############################################################################### # Step 6: Build Flutter web app ############################################################################### print_info "Step 6/8: Building Flutter web app..." flutter build web --release --web-renderer html print_success "Flutter web app built successfully" ############################################################################### # Step 7: Deploy to web server ############################################################################### print_info "Step 7/8: Deploying to web server..." # Create web root if it doesn't exist mkdir -p $WEB_ROOT # Backup existing deployment if [ -d "$WEB_ROOT" ] && [ "$(ls -A $WEB_ROOT)" ]; then BACKUP_DIR="${WEB_ROOT}_backup_$(date +%Y%m%d_%H%M%S)" print_info "Backing up existing deployment to $BACKUP_DIR" cp -r $WEB_ROOT $BACKUP_DIR fi # Copy new build cp -r build/web/* $WEB_ROOT/ # Set proper permissions chown -R www-data:www-data $WEB_ROOT chmod -R 755 $WEB_ROOT print_success "Deployed to $WEB_ROOT" ############################################################################### # Step 8: Verify deployment ############################################################################### print_info "Step 8/8: Verifying deployment..." if [ -f "$WEB_ROOT/index.html" ]; then print_success "Deployment verified - index.html exists" else print_error "Deployment failed - index.html not found" exit 1 fi ############################################################################### # Cleanup ############################################################################### print_info "Restoring original API service file for future builds..." if [ -f "${API_SERVICE_FILE}.backup" ]; then mv ${API_SERVICE_FILE}.backup $API_SERVICE_FILE print_success "Original API service restored" fi ############################################################################### # Complete ############################################################################### echo "" echo -e "${GREEN}╔════════════════════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ Deployment Complete! 🎉 ║${NC}" echo -e "${GREEN}╚════════════════════════════════════════════════════════╝${NC}" echo "" print_success "Frontend deployed successfully!" echo "" print_info "Access your application at:" echo " 🌐 http://$SERVER_IP" echo "" print_info "Backend API is at:" echo " 🔌 http://$SERVER_IP:3000/api" echo " 💚 Health: http://$SERVER_IP:3000/health" echo "" print_info "Next steps:" echo " 1. Test the application in your browser" echo " 2. Check backend is running: pm2 status" echo " 3. View logs: pm2 logs luckychit-api" echo "" print_info "To rollback, use the backup at:" echo " $(ls -td ${WEB_ROOT}_backup_* 2>/dev/null | head -1)" echo ""