What is Deployer?
It is CLI deployment tool written in PHP with support for popular frameworks such as WordPress, Magento and Laravel and CakePHP out of the box.
In short , Deployer is a PHP file (called Recipe) containing tasks definitions. Recipe can require other recipes and extend/ override functionality.
Some PHP Deployer of the advantage are:
- Fast: Contains loads of time-saving features
- Modular: Can customize the deployment script
- Clean Code: Clean and easy to use code.
- Rollbacks: Easy to rollback to the previous version is something that breaks the code.
- Atomic Deploy: Can do lots of other tasks before doing the symlink and update the live site
- Parallel tasks: Can run the parallel tasks with no other extension
- Consistency: It’s consistent between servers
- Easy to use
Install Deployer:
Using composer to install Deployer with the following command:
1 2 3 4 5 | composer require deployer/deployer --dev |
Or install it globally:
1 2 3 4 5 | composer global require deployer/deployer |
Set Config
To create the deployer settings, we need to use the set function.
set function get 2 arguments.
1 2 3 4 5 6 7 8 | set("neme_of_setting","value"); // Project repository set('repository', 'link_to_repository'); |
Hosts
Defining a host in Deployer is necessary to deploy your application. It can be remote server or local host or EC2. Each host should conyain stage, one or more roles and configuration document.
1 2 3 4 5 6 7 8 | host('your_domain.com or IP') ->stage('production') ->roles('app') ->set('deploy_path', '~/app'); |
Add ssh login details to the host:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | host('domain.com') ->user('name') ->('branch', 'master') //Overriding global config per host(eg switch to master branch. ->become('deployer')</code> //switch user ->port(22) ->configFile('~/.ssh/config') ->identityFile('~/.ssh/id_rsa') ->forwardAgent(true) ->multiplexing(true) ->addSshOption('UserKnownHostsFile', '/dev/null') ->addSshOption('StrictHostKeyChecking', 'no'); |
Tasks
Tasks are the functions which will be run as soon as server contacted.
Task function, receive 2 arguments, the name and a function or callback
Below is the function that print the text on screen after deploy finished.
1 2 3 4 5 6 7 | task("deploy:done",function(){ write('Deploy Finished"); })->desc("some description for other programmer"); |
Another example to restart the server by using the run function.:
1 2 3 4 5 6 7 | task("reload:service":function(){ run("sudo /usr/sbin/service nginx:reload"); })->desc("Restart the nginx Service"); |
Please Note: run() function will execute the command on server
Pre-defined Task
To use the pre-written tasks in default Deployer recipe, we need to use the array and add the default task names:
1 2 3 4 5 6 7 8 9 10 11 12 | task('deploy' , [ 'deploy:prepare', //prepare server for release 'deploy:release', //Create release and shared directory 'deploy:update_code', //get the code from git 'deploy:vendors', //runs composer install and bring in external dependencies 'deploy:symlink', //create current symlink point to latest version of release 'cleanup' //remove old releases ])->desc("Main deploy tasks"); |
Please note: its possible to override the pre-defined tasks, by using the same name on the custom task.
Hooks
Deployer provides 2 hooks called before and after. they can run the function before or after deploy.
1 2 3 4 5 | after("deploy" , "name_of_the_task"); |
CLI Usage
After installation of Deployer you will have the ability to run the dep
command from your terminal.
1 2 3 4 5 | vendor/bin/dep deploy $env |
Sample WordPress deploy.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | namespace Deployer; require_once __DIR__ . '/common.php'; set('shared_files', ['wp-config.php']); set('shared_dirs', ['wp-content/uploads']); set('writable_dirs', ['wp-content/uploads']); task('deploy', [ 'deploy:info', 'deploy:setup', 'deploy:lock', 'deploy:release', 'deploy:update_code', 'deploy:shared', 'deploy:writable', 'deploy:symlink', 'deploy:unlock', 'deploy:cleanup', ])->desc('Deploy your project'); after('deploy', 'success'); |
More information: https://deployer.org/docs/getting-started.html