Deployment in itself is a messy task, and it becomes a bit more of hectic if you need to run a bunch of commands every time you deploy.
The one of the best way is to create a deployment script and run it every time you deploy.
Step 1: Create deployment script
create a deploy.sh file in the root of your folder and make it executable. You can use the following commands.
touch deploy.sh
sudo chmod +x deploy.sh
Step 2: Update the script
# Turn on maintenance mode
php artisan down
# Pull the latest changes from the git repository
git reset --hard
git clean -df
git pull origin master
# Install/update composer dependecies
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev
# Run database migrations
php artisan migrate --force
# Clear caches
php artisan cache:clear
# Clear expired password reset tokens
php artisan auth:clear-resets
# Clear and cache routes
php artisan route:clear
php artisan route:cache
# Clear and cache config
php artisan config:cache
php artisan config:clear
php artisan view:clear
# Install node modules
npm install
# Build assets using Laravel Mix
npm run prod
# Turn off maintenance mode
php artisan up
Step 3: Run on Deployment
Now every time you need to deploy. just run deploy.sh
./deploy.sh
Conclusion
In this blog post, I have drafted a deploy.sh script for enhancing and making Laravel deployments simpler. It performs several functions such as enabling maintenance mode, fetching latest code, updating Composer dependencies, running migrations, clearing cache, and compiling Laravel Mix assets. This approach helps in streamlining software release hence minimizing variations and defects in the process. If this script was implemented, it would save more time. Therefore, if instead of doing everything manually every time you want to deploy your project using Laravel or there is new version available then you can use deploy.sh script.