An Introduction to Deployer

30 July, 2018

What is Deployer?

Deployer is a super simple open-source PHP based deployment tool. It provides out-of-the-box support for a number of popular PHP frameworks, making the process of deploying your websites and applications from Git quick and easy.

Why should I consider Deployer, or any deployment tool?

Deployment tools help to reduce errors when using manual deployments, so a file won't be missed when deploying directly from your code repository. Using a deployment tool allows teams to deploy easily by using identical configurations. A new developer is able to quickly make changes to another developers' codebase and deploy safe in the knowledge that the correct codebase is being deployed to the correct destination. 

And, let's not forget every modern developers friend - automation. Automating the deployment stage, where tasks very rarely change as an app grows, allows more time for development.

Installing Deployer

Conveniently, there are three ways to install Deployer. Installation instructions can be found on their official website.

To check Deployer has been installed correctly run `dep --version`. If this doesn’t return `Deployer version master` or similar, something has gone wrong with your installation. Please refer back to the Deployer installation instructions again to see whether a step has been missed.

Working with Deployer

Once installed, the `dep` command will become available - this is the foundation of all Deployer commands. The first Deployer command to use is `dep init`, which should be run in the apps root directory and will create a `deploy.php` file. 

The `deploy.php` file forms the base of each applications' Deployer configuration and holds all the tasks required for deployments. Upon firing the `dep init` command, Deployer will offer a list of default "recipes" to use for the new Deployer instance. Currently; Common, Laravel, Symfony, Yii, Zend Framework, CakePHP, CodeIgnitor and Drupal. 

Recipes are used by Deployer as a neat and tidy way to make each of the PHP framework deployment options readily available to the `deploy.php` file. Follow the steps from `dep init` to also assign your Git repository URL.

In its simplest form two variables need updating in the `deploy.php` file to enable automated deployment to your host server. The `application` value should be changed to match your application name, along with the `host` domain (or IP address). Once these three variables have been configured you are able to deploy your codebase to the server using `dep deploy`. If you have not already done so, you will need to add the SSH key of the host server to your Git account online; in our case Github.

Deploying with Deployer

To execute your first deploy using the default Deployer settings, you can simply run `dep deploy`. Running this command will connect to your host server, clone the Git repository and work down the list of tasks in your `deploy.php` configuration file. We'll run over the default tasks and briefly example each one:

task('deploy', [
    // outputs the branch and IP address to the command line
    'deploy:info',
    // preps the environment for deploy, creating release and shared directories
    'deploy:prepare',
    // adds a .lock file to the file structure to prevent numerous deploys executing at once
    'deploy:lock',
    // removes outdated release directories and creates a new release directory for deploy
    'deploy:release',
    // clones the project Git repository
    'deploy:update_code',
    // loops around the list of shared directories defined in the config file
    // and generates symlinks for each
    'deploy:shared',
    // loops around the list of writable directories defined in the config file
    // and changes the owner and permissions of each file or directory
    'deploy:writable',
    // if Composer is used on the site, the Composer install command is executed
    'deploy:vendors',
    // loops around release and removes unwanted directories and files
    'deploy:clear_paths',
    // links the deployed release to the "current" symlink
    'deploy:symlink',
    // deletes the unlock file, allowing further deploys to be executed
    'deploy:unlock',
    // loops around a list of release directories and removes any which are now outdated
    'cleanup',
    // can be used by the user to assign custom tasks to execute on successful deployments
    'success'
]);

Should an error occur with a previous deploy, or if a bug is discovered in the release, this can easily be rectified by executing `dep rollback`. This command will rollback the latest deploy to the previous release.

One of the beauties of Deployer is how easily a developer can create and integrate their own tasks into the deployment workflow. The following example shows how simple it would be to run database migrations on deploy for a Laravel deployment. This will execute the `artisan:migrate` command directly before the default `deploy:symlink` task:

before('deploy:symlink', 'artisan:migrate');

The second example shows how easy it is to execute the PHP5 FPM command after the `deploy:cleanup` task:

task('reload:php-fpm', function() {
    run('sudo /usr/sbin/service php5-fpm reload');
})->desc('PHP5 FPM reloaded');
    
after('cleanup', reload:php-fpm');

Conclusion

Deployer is the "new kid on the block" in automated PHP deployment scripts and it is quite similar in its execution to Capistrano and Rocketeer. The approach Deployer have taken with recipes for popular PHP frameworks makes deploying to different instances really simple. The ease at which custom tasks can be integrated into any deployment workflow makes Deployer a breeze to work with.

This blog post will be the first in a series about Deployer where we will delve a little deeper into the workflow and more advanced functionality.

References 

https://deployer.org/docs
https://deployer.org/docs/inst...
https://code.tutsplus.com/tuto...
https://www.silverstripe.org/b...
https://www.digitalocean.com/c...
https://medium.com/@nickdenard...

Written by Stewart

Having been with Evoluted for more than 9 years, Stewart has helped to develop successful websites for countless clients. With vast experience related to web development, he’s comfortable putting his skills to use across a wide variety of projects. His ability was highlighted by the key role he played in a site we built for Bamboo Travel, which won best website at the 2013 Wanderlust Travel Awards.

Up next…
Understanding Adwords Attribution Models
25 July, 2018

0 Comments

Leave a comment

Replying to: - Cancel