`
xieye
  • 浏览: 803908 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

推荐使用php的部署工具 deployer

    博客分类:
  • PHP
阅读更多
php部署代码可以使用
https://deployer.org/

这个工具。功能非常强大。

可以:
自动执行脚本,例如数据库更新。
可以自动composer更新。
可以 自动清除opcache的缓存,
还可以平滑重启laravel 的守护进程。

下面放个例子,供参考

<?php
namespace Deployer;

require 'recipe/laravel.php';

require 'recipe/cachetool.php';


// 说明,命令中的 dep deploy,就是这个task,这个task包括多个子任务。
task('deploy', [
    'deploy:info',
    'deploy:prepare',
    'deploy:lock',
    'deploy:release',
    'deploy:update_code',
    'deploy:shared',
    'deploy:vendors',
    'deploy:writable',
    'artisan:storage:link',
    'artisan:view:cache',
    'artisan:config:cache',
    'artisan:optimize',
   // 'my:artisan:horizon:terminate',// 1127
    'deploy:symlink',
    'deploy:unlock',
    'cleanup',
]);


set('cachetool', '/tmp/php-cgi-72.sock');

// Project name,
set('application', '项目名称');

// Project repository
set('repository', 'git@XXXXX9/xxx.git');

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true); 

// Shared files/dirs between deploys 
add('shared_files', []);
add('shared_dirs', []);

// Writable dirs by web server 
add('writable_dirs', []);

set('envfile', '.env');
set('pemfile', 'private.pem');
set('http_user', 'www');


// Hosts
// x关键是那个sudo。
task('my:artisan:horizon:terminate', function () {
    run('sudo {{bin/php}} {{release_path}}/artisan horizon:terminate');
});


// 服务器a
host('host1.net')
    ->stage('host1') // 
    ->user('deployer')
    ->identityFile('~/.ssh/id_rsa')
    ->set('deploy_path', '/www/wwwroot/host1')
    ->set('envfile', '.env.host1')
    ->set('branch', 'host1');

// 服务器b
host('host2.net')
    ->stage('host2') 
    ->user('deployer')
    ->identityFile('~/.ssh/id_rsa')
    ->set('deploy_path', '/www/wwwroot/host2')
    ->set('envfile', '.env.host2')
    ->set('branch', 'host2');



// Tasks

// 定义一个上传 .env 文件的任务
desc('Upload .env file');
task('env:upload', function() {
    // 将本地的 .env 文件上传到代码目录的 .env
    upload(get('envfile'), '{{deploy_path}}/shared/.env');
});

desc('Upload pem file');
task('pem:upload', function() {
    // 将本地的 .env 文件上传到代码目录的 .env
    upload(get('pemfile'), '{{release_path}}/config/private.pem');
});


task('sey_hello', function() {
    // 
    writeln('<info>Upload OK!</info>');
});


// 自己定义的清除opcache 缓存的工具
// 要求项目自身的composer.json里有 gordalina/cachetool
task('my:cachetool:clear:opcache', function () {
    $releasePath = get('release_path');
    $options = get('cachetool');
    $fullOptions = get('cachetool_args');

    if (strlen($fullOptions) > 0) {
        $options = "{$fullOptions}";
    } elseif (strlen($options) > 0) {
        $options = "--fcgi={$options}";
    }
    cd($releasePath);
    run("{{bin/php}} ./vendor/bin/cachetool opcache:reset {$options}");
});




// 定义一个后置钩子,在 deploy:shared 之后执行 env:upload 任务
after('deploy:shared', 'env:upload');
after('env:upload', 'pem:upload');

after('env:upload', 'sey_hello');


task('build', function () {
    run('cd {{release_path}} && build');
});

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

// Migrate database before symlink new release.

before('deploy:symlink', 'artisan:migrate');
// 新增。
after('deploy:symlink', 'my:cachetool:clear:opcache');



假如想部署服务器A

dep deployer host1

假如想部署服务器A

dep deployer host2

假如想都部署
dep deployer host1 && dep deployer host2

整个部署过程一目了然,实在是居家旅行必备良药。

==========================

因为composer的原因,这里附带一个技巧。

composer.phar 和 composer符合链接,都必须放在/usr/bin目录下

因为deployer 会去找这个文件,如果没找到,每次拉库之前居然还要先远程安装
composer,耗费大量时间。



0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics