Forge deployment script (no downtime)
Mar 5, 2020 7:50 PM (9 months ago)

# Forge deployment script (no downtime)

I just made a simple Forge (opens new window) deployment script with no downtime. Works pretty well so I'm sharing it.

# Prerequisites:

  • Make sure to add your server's access key into the accepted Access Key in your Git repo.
  • SSH into the server and git clone your repo in /home/default for the first time so that so that your server will trust the new source and you'll have git initialised.
# stop script on error signal
set -e

# remove old deployment folders
if [ -d "/home/forge/deploy" ]; then
  rm -R /home/forge/deploy
if [ -d "/home/forge/backup" ]; then
  rm -R /home/forge/backup

cp -R /home/forge/default/ /home/forge/deploy/

cd /home/forge/deploy
rm composer.lock
rm -R vendor

# Pull the latest changes from the git repository
git pull origin master

# Install/update composer dependecies
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev

cd /home/forge

# Switch (downtime for microseconds)
mv /home/forge/default /home/forge/backup
mv /home/forge/deploy /home/forge/default

cd /home/forge/default

# Clear caches
php artisan cache:clear

# Clear and cache routes
php artisan route:cache

# Clear and cache config
php artisan config:cache

# Resets FPM
( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service php7.3-fpm reload ) 9>/tmp/fpmlock

The last step is to add the .env file and you're set!

Enjoy your new 1 click deploy with no downtime 🎉.