【问题标题】:PHPDeployer: set variable for local task based on host?PHPDeployer:根据主机为本地任务设置变量?
【发布时间】:2019-10-25 15:35:27
【问题描述】:

我让部署程序作为 GitHub Action 管道的一部分运行。我想让管道为我运行 NPM 构建并将其作为部署的一部分复制,而不是让部署程序在服务器上使用 npm dev 依赖项。对于corestage 站点,我想使用调试版本,而prod 获取生产版本。

下面的完整deploy.php:

<?php
namespace Deployer;

require 'recipe/laravel.php';
require 'recipe/rsync.php';
require 'recipe/discord.php';

// Project name
set('application', 'xxxxxxxxxx');

// Project repository
set('repository', 'git@github.com:xxxxxxxxxx');

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

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

set('dev_db', false);
set('dev_js_mode', 'prod');
set('rsync_src', __DIR__ . '/public/');
set('rsync_dest', '{{release_path}}/public/');
set('discord_channel', 'xxxxxxxxxx');
set('discord_token', 'xxxxxxxxxx');

// Hosts
host('core')
    ->hostname('xxxxxxxxxx.com')
    ->user('xxxxxxxxxx')
    ->set('deploy_path', '/home/xxxxxxxxxx.com')
    ->set('branch', 'master')
    ->set('dev_db', true)
    ->set('dev_js_mode', 'dev');

host('stage')
    ->hostname('xxxxxxxxxx')
    ->user('xxxxxxxxxx')
    ->set('deploy_path', '/home/xxxxxxxxxx.com')
    ->set('branch', 'stage')
    ->set('dev_js_mode', 'dev');

host('prod')
    ->hostname('xxxxxxxxxx.com')
    ->user('xxxxxxxxxx')
    ->set('deploy_path', '/home/xxxxxxxxxx.com')
    ->set('branch', 'prod');

// Custom Tasks
task('reload', function () {
    run('sudo systemctl restart php7.2-fpm');
    run('sudo systemctl restart nginx');
});

task('database', function() {
    $freshDb = get('dev_db');

    if ($freshDb === true) {
        run('composer run refresh-db --working-dir={{release_path}}/');
    } else {
        invoke('artisan:migrate');
    }
});

task('local:javascript:package', function() {
    run('npm ci');
    run('npm run {{dev_js_mode}}');
})->local();

task('local:javascript:deploy', [
    'rsync',
]);

task('local:javascript', [
    'local:javascript:package',
    'local:javascript:deploy',
]);

// modifications
before('deploy:symlink', 'local:javascript');
before('deploy:symlink', 'database');

after('deploy', 'reload');
after('rollback', 'reload');
after('deploy:failed', 'deploy:unlock');

before('deploy', 'discord:notify');
after('success', 'discord:notify:success');
after('deploy:failed', 'discord:notify:failure');

我将dev_js_mode 变量默认设置为“prod”,以确保调试版本不会出现在我不希望的地方。

如果我运行dep -vvv deploy core,我可以看到local:javascript:package 步骤将dev_js_mode 设置为“prod”,这是因为该命令未在主机上运行,​​但我想不出办法让它发挥作用。

【问题讨论】:

  • -&gt;local() 添加到它在本地运行的任务中,因此使用第一个 set('dev_js_mode', 'prod'); 作为 var 而不是主机的覆盖
  • 如果你读了我的最后一段,我说了这么多。我的问题是如何我得到我想要的工作
  • 我确实读过.. 问题是-&gt;local() 删除它。 rtm:deployer.org/docs/advanced/deploy-strategies.html#build-server 你在本地运行它,所以正如我之前所说的,它不会去接主机。
  • 是的,我知道本地没有使用主机级别设置,因为它在本地运行并且您的链接没有回答我的问题.我希望在调试模式下构建核心/阶段 NPM,并在生产模式下构建产品。

标签: php php-deployer


【解决方案1】:

经过大量尝试 n 错误后,我想出了两种方法来解决我的问题。

  1. 在本地任务中使用之前通过远程闭包设置 php 变量。哈克,但它有效:
<?php
namespace Deployer;

set('testing', 'prod');

$var = 'original';

host('core')
    ->hostname('example.com')
    ->set('testing', 'dev');

host('prod')
    ->hostname('example.com')
    ->set('testing', 'prod');

task('remote:run', function() use (&$var) {
    $var = get('testing');
    run('echo remote {{testing}}, var is '. $var);
});

task('local:var', function() use (&$var) {
    run('echo local {{testing}}, var is '. $var);
})->local();

task('mytest', [
    'remote:run',
    'local:var',
]);
  1. 可能更受支持的方式(更灵活或更复杂,取决于您如何看待它)是创建本地任务的两个版本并通过类似 stage 的方式限制它们。它会创建 2-3 个任务,而不是 1 个:
<?php
namespace Deployer;

host('core')
    ->hostname('example.com')
    ->stage('core');

host('prod')
    ->hostname('example.com')
    ->stage('prod');

task('remote:run', function(){
    run('echo remote');
});

task('local:core', function() {
    run('echo local, core only');
})->local()->onStage('core');

task('local:prod', function() {
    run('echo local, prod only');
})->local()->onStage('prod');

task('local', [
    'local:core',
    'local:prod',
]);

task('mytest', [
    'remote:run',
    'local', // instead of 'local', you can just call both 'local:core' and 'local:prod'
]);

【讨论】:

    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多