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
Feel free to suggest any modification if you feel so.