Deploying new versions of a Laravel app with Fabric

Published by 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'])
2
3@task('deploy', ['on' => 'web'])
4 cd /var/www
5 sudo chown -R matthew:matthew .
6 git pull origin master
7 php artisan migrate
8 php artisan view:clear
9 composer dump-autoload
10 sudo chown -R www-data:www-data .
11 sudo supervisorctl restart mail-queue
12@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 python
2from fabric.api import local, env, run, sudo
3from fabric.context_managers import cd, prefix
4
5env.hosts = ['server1.example.com']
6env.path = "/var/www"
7env.user = "matthew"
8env.password = "password"
9# Or...
10env.key_filename = '/path/to/ssh/key'
11
12def deploy():
13 """
14 Deploy the latest version
15 """
16 # Push changes to Bitbucket
17 local("git push origin master")
18
19 # Switch to project directory
20 with cd(env.path):
21 # Change owner
22 sudo('chown -R matthew:matthew .')
23
24 # Pull changes to server
25 run('git pull origin master')
26
27 # Run migrations
28 run('php artisan migrate')
29
30 # Clear cached files
31 run('php artisan view:clear')
32 run('composer dump-autoload')
33
34 # Change owner back
35 sudo('chown -R www-data:www-data .')
36
37 # restart mail queue
38 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.