#!/bin/bash # Deployment script for Bike Service Application # Make sure to run this script from the project root directory set -e # Exit on any error echo "🚀 Starting deployment..." # Check if we're in the right directory if [ ! -f "backend/artisan" ] || [ ! -f "frontend/package.json" ]; then echo "❌ Error: Please run this script from the project root directory" exit 1 fi # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running on Ubuntu server if [[ "$OSTYPE" != "linux-gnu"* ]]; then print_warning "This script is designed for Ubuntu servers. Some commands may not work on your system." fi # Check if running as root if [[ $EUID -eq 0 ]]; then print_error "Please don't run this script as root. Use a regular user with sudo privileges." exit 1 fi # Check if required commands exist check_command() { if ! command -v $1 &> /dev/null; then print_error "$1 is not installed. Please install it first." exit 1 fi } print_status "Checking required commands..." check_command "php" check_command "composer" check_command "npm" check_command "git" # Get current branch CURRENT_BRANCH=$(git branch --show-current) print_status "Current branch: $CURRENT_BRANCH" # Ask for confirmation read -p "Do you want to deploy from branch '$CURRENT_BRANCH'? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_warning "Deployment cancelled." exit 0 fi # Pull latest changes print_status "Pulling latest changes from git..." git pull origin $CURRENT_BRANCH # Backend deployment print_status "Deploying Laravel backend..." cd backend # Install/update dependencies print_status "Installing Composer dependencies..." composer install --no-dev --optimize-autoloader # Check if .env exists, if not copy from example if [ ! -f ".env" ]; then print_warning ".env file not found, copying from .env.example..." if [ -f ".env.example" ]; then cp .env.example .env print_warning "Please configure your .env file before continuing!" read -p "Press Enter after configuring .env file..." else print_error ".env.example file not found!" exit 1 fi fi # Generate key if not set if ! grep -q "APP_KEY=base64:" .env; then print_status "Generating application key..." php artisan key:generate fi # Run migrations print_status "Running database migrations..." php artisan migrate --force # Clear and rebuild caches print_status "Clearing and rebuilding caches..." php artisan config:clear php artisan route:clear php artisan view:clear php artisan cache:clear php artisan config:cache php artisan route:cache php artisan view:cache # Set production environment if ! grep -q "APP_ENV=production" .env; then print_status "Setting production environment..." sed -i 's/APP_ENV=.*/APP_ENV=production/' .env fi if ! grep -q "APP_DEBUG=false" .env; then print_status "Disabling debug mode..." sed -i 's/APP_DEBUG=.*/APP_DEBUG=false/' .env fi cd .. # Frontend deployment print_status "Deploying Vue.js frontend..." cd frontend # Install/update dependencies print_status "Installing npm dependencies..." npm install # Build for production print_status "Building frontend for production..." npm run build # Copy built files to Laravel public directory print_status "Copying built files to Laravel public directory..." if [ -d "dist" ]; then cp -r dist/* ../backend/public/ print_status "Frontend files copied successfully" else print_error "dist directory not found! Build may have failed." exit 1 fi cd .. # Set proper permissions print_status "Setting proper permissions..." if command -v sudo &> /dev/null; then sudo chown -R www-data:www-data backend/storage backend/bootstrap/cache sudo chmod -R 775 backend/storage backend/bootstrap/cache print_status "Permissions set successfully" else print_warning "sudo not available, skipping permission changes" fi # Restart services (if running on server) if command -v sudo &> /dev/null; then print_status "Restarting services..." # Check if services exist and restart them if sudo systemctl list-units --type=service | grep -q "php8.2-fpm"; then sudo systemctl restart php8.2-fpm print_status "PHP-FPM restarted" fi if sudo systemctl list-units --type=service | grep -q "nginx"; then sudo systemctl restart nginx print_status "Nginx restarted" fi if sudo systemctl list-units --type=service | grep -q "bikeservice-queue"; then sudo systemctl restart bikeservice-queue print_status "Queue worker restarted" fi else print_warning "sudo not available, skipping service restarts" fi # Final checks print_status "Running final checks..." # Check if Laravel is working cd backend if php artisan --version &> /dev/null; then print_status "Laravel is working correctly" else print_error "Laravel is not working correctly" exit 1 fi # Check if frontend files are accessible if [ -f "public/index.html" ]; then print_status "Frontend files are in place" else print_error "Frontend files are missing" exit 1 fi cd .. echo echo -e "${GREEN}✅ Deployment completed successfully!${NC}" echo echo "Next steps:" echo "1. Test your application in the browser" echo "2. Check application logs if there are any issues" echo "3. Verify all features are working correctly" echo echo "Useful commands:" echo "- Check Laravel logs: tail -f backend/storage/logs/laravel.log" echo "- Check Nginx logs: sudo tail -f /var/log/nginx/error.log" echo "- Check PHP-FPM logs: sudo tail -f /var/log/php8.2-fpm.log" echo "- Restart all services: sudo systemctl restart nginx php8.2-fpm"