Deploying new versions of a Laravel app with Fabric
Published by Matthew Daly at 5th September 2016 9:22 pm
Envoy is the official way to run tasks on a remote server for Laravel apps. A typical Envoy task for deploying a new version might look like this:
1@servers(['web' => 'matthew@server1.example.com'])23@task('deploy', ['on' => 'web'])4 cd /var/www5 sudo chown -R matthew:matthew .6 git pull origin master7 php artisan migrate8 php artisan view:clear9 composer dump-autoload10 sudo chown -R www-data:www-data .11 sudo supervisorctl restart mail-queue12@endtask
This would be defined in Envoy.blade.php
. With this in place, and Envoy set up globally, you can then run envoy run deploy
to run the deploy
command.
However, Envoy requires the PHP SSH library, which I haven't been able to get working with PHP 7. Fortunately I was already familiar with Fabric, which makes an excellent alternative as long as you don't mind writing the task in Python.
The same kind of task might look like this in a Fabric script, saved as fabfile.py
:
1#!/usr/bin/env python2from fabric.api import local, env, run, sudo3from fabric.context_managers import cd, prefix45env.hosts = ['server1.example.com']6env.path = "/var/www"7env.user = "matthew"8env.password = "password"9# Or...10env.key_filename = '/path/to/ssh/key'1112def deploy():13 """14 Deploy the latest version15 """16 # Push changes to Bitbucket17 local("git push origin master")1819 # Switch to project directory20 with cd(env.path):21 # Change owner22 sudo('chown -R matthew:matthew .')2324 # Pull changes to server25 run('git pull origin master')2627 # Run migrations28 run('php artisan migrate')2930 # Clear cached files31 run('php artisan view:clear')32 run('composer dump-autoload')3334 # Change owner back35 sudo('chown -R www-data:www-data .')3637 # restart mail queue38 sudo('supervisorctl restart mail-queue')
Then, assuming Fabric is already installed locally, you can run fab deploy
to push up the latest revision.
Either of these solutions will do a fine job of deploying your app. If you do need to store user-specific data in your Fabric script, it's probably prudent to keep it out of version control.
Whichever way you choose, it's a really good idea to do what you can to automate deployment. It can be a boring, repetitive job, and both of these solutions make it much easier.