Forge deploy scripts in version control

2023-09-05 #laravel-forge

I love Laravel Forge's quick deploy scripts. Forge allows you to set up a deploy script in their web interface and run it when you push to a git branch. However, I generally prefer to keep orchestration in the git repository. It's nice to have history, and I don't want to visit Forge whenever I want to make a change to the deploy script.

An elegant solution: move the script to a file. I created a deploy.sh and call it from Forge.

My deploy script isn't too bulky for now. It runs composer install and npm ci for dependencies, builds assets, clears the cache, caches config files, and does a safe restart of the php fpm process.

composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader
 
npm ci
npm run build
 
php artisan cache:clear
php artisan config:cache
 
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service php8.2-fpm reload ) 9>/tmp/fpmlock

Which I call from Forge:

cd /home/forge/full-stack-artisan.dev
git pull origin $FORGE_SITE_BRANCH
 
./deploy.sh

Don't forget to make deploy.sh executable before use.

chmod +x deploy.sh

Whenever I need to tweak the deploy script, I can update it from my text editor and git push.