#!/bin/bash # Check if docker compose (new) or docker-compose (old) is available if command -v docker &>/dev/null; then if docker compose version &>/dev/null; then DOCKER_COMPOSE="docker compose" elif command -v docker-compose &>/dev/null; then DOCKER_COMPOSE="docker-compose" else echo "โŒ Error: Neither 'docker compose' nor 'docker-compose' found." echo "Please install Docker Desktop from https://www.docker.com/products/docker-desktop/" exit 1 fi else echo "โŒ Error: Docker is not installed." echo "Please install Docker Desktop from https://www.docker.com/products/docker-desktop/" exit 1 fi echo "๐Ÿš€ Starting rebuild process..." # Function to display usage show_usage() { echo "Usage: ./rebuild.sh [OPTIONS]" echo "Options:" echo " -h, --help Show this help message" echo " -c, --clean Perform a clean rebuild (no cache)" echo " -f, --fresh Perform a fresh migration" } # Default values CLEAN_BUILD=false FRESH_MIGRATION=false # Parse arguments while [[ "$#" -gt 0 ]]; do case $1 in -h|--help) show_usage; exit 0 ;; -c|--clean) CLEAN_BUILD=true ;; -f|--fresh) FRESH_MIGRATION=true ;; *) echo "Unknown parameter: $1"; show_usage; exit 1 ;; esac shift done # Pull latest changes from git echo "๐Ÿ“ฅ Pulling latest changes from git..." if ! git pull; then echo "โŒ Git pull failed. Please resolve any conflicts and try again." exit 1 fi # Stop running containers echo "๐Ÿ“ฅ Stopping running containers..." $DOCKER_COMPOSE down # Remove old images if clean build if [ "$CLEAN_BUILD" = true ] ; then echo "๐Ÿงน Cleaning Docker cache..." $DOCKER_COMPOSE rm -f docker system prune -f fi # Build and start containers echo "๐Ÿ—๏ธ Building containers..." if [ "$CLEAN_BUILD" = true ] ; then $DOCKER_COMPOSE build --no-cache else $DOCKER_COMPOSE build fi # Start containers echo "๐Ÿš€ Starting containers..." $DOCKER_COMPOSE up -d # Wait for the containers to be ready echo "โณ Waiting for containers to be ready..." sleep 10 # Clear Laravel cache echo "๐Ÿงน Clearing Laravel cache..." $DOCKER_COMPOSE exec app php artisan cache:clear $DOCKER_COMPOSE exec app php artisan config:clear $DOCKER_COMPOSE exec app php artisan view:clear # Run composer install echo "๐Ÿ“ฆ Installing dependencies..." $DOCKER_COMPOSE exec app composer install # Generate application key if .env exists and APP_KEY is empty if [ -f .env ] && ! grep -q "^APP_KEY=[A-Za-z0-9+/]\{40\}$" .env; then echo "๐Ÿ”‘ Generating application key..." $DOCKER_COMPOSE exec app php artisan key:generate fi # Run migrations if requested if [ "$FRESH_MIGRATION" = true ] ; then echo "๐Ÿ”„ Running fresh migrations..." $DOCKER_COMPOSE exec app php artisan migrate:fresh else echo "๐Ÿ”„ Running migrations..." $DOCKER_COMPOSE exec app php artisan migrate fi echo "โœจ Rebuild completed!" echo "๐ŸŒ Your application should be available at http://localhost:8080"