Friday 10 July 2015

Deploying with Envoy Laravel 5.1

We'll use Laravel's Envoy to deploy a PHP application to a production server. This will make a new release directory, clone the repository into it, run any needed build steps, and then finally swap the new code out with the older code, so that Nginx and PHP-FPM serve the new code.


Install Envoy Globally

# Install envoy globally. May need to ensure that
# the composer vendor/bin directory is in your PATH
# so the envoy command is found

composer global require "laravel/envoy=~1.0"

# fails, if the PATH variable is not set with composer's
# bin directory

which envoy
vim ~/.profile

If the which envoy test fails (if there's no output saying the path to the envoy command), edit .profile, .bashrc or whatever is appropriate for your system. Within this file, we'll append the following near the bottom. Adjust the file path as needed for your development server.#
Prepend the composer bin directory to your PATH

# This path is specific to my vagrant server

export PATH=/home/vagrant/.composer/vendor/bin:$PATH

Finally we can create an envoy file for our project:

cd /vagrant/app
vim Envoy.blade.php

Here's a sample we can use to test that Envoy can connect to our server:

@servers(['web' => 'deploy-ex'])

<?php
$repo = 'https://{username}:{password}@github.com:Servers-for-Hackers/deploy-ex.git';
$release_dir = '/var/www/releases';
$app_dir = '/var/www/app';
$release = 'release_' . date('YmdHis');
?>

@macro('deploy', ['on' => 'web'])
    fetch_repo
    run_composer
    update_permissions
    update_symlinks
@endmacro

@task('fetch_repo')
    [ -d {{ $release_dir }} ] || mkdir {{ $release_dir }};
    cd {{ $release_dir }};
    git clone {{ $repo }} {{ $release }};
@endtask

@task('run_composer')
    cd {{ $release_dir }}/{{ $release }};
    composer install --prefer-dist;
@endtask

@task('update_permissions')
    cd {{ $release_dir }};
    chgrp -R www-data {{ $release }};
    chmod -R ug+rwx {{ $release }};
@endtask

@task('update_symlinks')
    ln -nfs {{ $release_dir }}/{{ $release }} {{ $app_dir }};
    chgrp -h www-data {{ $app_dir }};
@endtask

Run that and test it via:

envoy run deploy

Now enjoy, your code will deploy with one command from your local.

No comments:

Post a Comment