Deployer官网:https://deployer.org/

自动化部署的好处

  • 减少重复工作
  • 确保全部目标环境代码完全一致
  • 方便快速地进行弹性扩展
  • 如果部署失败,可以快速回滚

Deployer的特性

  • 快速回滚
  • 并行任务:Deployer可以在多台服务器之间同时进行部署,使得部署操作更加快速
  • 一致性:当多台服务器同时部署时,如果有一台部署失败,则全部服务器都回滚,保证了目标环境的一致性
  • 自动化:更新代码、执行migration、修改文件权限、设置软链接等操作无需手动执行
  • Recipes(菜谱):Deployer有许多开发者贡献的部署脚本,如果场景符合,可以直接下载使用

Deployer安装

  1. curl -LO https://deployer.org/deployer.phar
  2. mv deployer.phar /usr/local/bin/dep
  3. chmod +x /usr/local/bin/dep

开始使用

进入到项目根目录: dep init

\QQ20161225-220057@2x.png

选择项目框架类型或者选择common类型,自动生成初始化脚本:

<?php
/*
 * This file has been generated automatically.
 * Please change the configuration for correct use deploy.
 */

require 'recipe/common.php';

// Set configurations
set('repository', 'git@domain.com:username/repository.git');
set('shared_files', []);
set('shared_dirs', []);
set('writable_dirs', []);

// Configure servers
server('production', 'prod.domain.com')
    ->user('username')
    ->password()
    ->env('deploy_path', '/var/www/prod.domain.com');

server('beta', 'beta.domain.com')
    ->user('username')
    ->password()
    ->env('deploy_path', '/var/www/beta.domain.com');

/**
 * Restart php-fpm on success deploy.
 */
task('php-fpm:restart', function () {
    // Attention: The user must have rights for restart service
    // Attention: the command "sudo /bin/systemctl restart php-fpm.service" used only on CentOS system
    // /etc/sudoers: username ALL=NOPASSWD:/bin/systemctl restart php-fpm.service
    run('sudo /bin/systemctl restart php-fpm.service');
})->desc('Restart PHP-FPM service');

after('success', 'php-fpm:restart');

/**
 * Main task
 */
task('deploy', [
    'deploy:prepare',
    'deploy:release',
    'deploy:update_code',
    'deploy:shared',
    'deploy:writable',
    'deploy:symlink',
    'cleanup',
])->desc('Deploy your project');

after('deploy', 'success');

常用命令

  • dep init:脚本初始化
  • dep list:列出所有可执行命令
  • dep cleanup:清理旧版本
  • dep deploy:部署新版本
  • dep rollback:回滚
  • dep self-update:自更新

Configure servers

方式一:

server('test', '127.0.0.1:22')
->user('root')
->password('****')
->env('deploy_path',‘/data/webroot/deployer');

方式二: 将服务器配置信息放到一个yml文件中,在deploy.php中引用server.yml文件

serverList('servers.yml');
beta:
  host: beta.domain.com
  
  user: www
  
  password: pass
  
  stage: beta
  
  deploy_path: /home/www/

test:
  
  host: test.domain.com
  
  user: www
  
  password: pass
  
  stage: beta
  
  deploy_path: /home/www/ 

常用方法

run(string $command)
cd(string $path)
runLocally(string $command, int $timeout = 60)
upload(string $file, string $uploadFile)
download(string $localFile, string $deploymentFile)
writeln(string $message)

End