2

我正在寻找如何设置主机用户并从 env 传递

这不起作用 - 需要字符串而不是函数

host('51.38.98.87')
    ->stage('production')
    ->user( function(){return getenv('PRODUCTION_USER'); )
    ->pass( function(){return getenv('PRODUCTION_PASS'); )
    ->port(22)

在 Symfony 4 中,我使用 env 并且不想在 ~/.ssh/config 中设置主机

更新1:

<?php
namespace Deployer;

use Symfony\Component\Dotenv\Dotenv;

require 'recipe/symfony.php';

require_once 'vendor/autoload.php';

$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');

然后

dep 部署生产

得到

dep deploy production


 [Error] Class 'Symfony\Component\Dotenv\Dotenv' not found                                                              


 #0 phar:///usr/local/bin/dep/src/Deployer.php(325): require()                                                          
 #1 [internal function]: Deployer\Deployer::Deployer\{closure}()                                                        
 #2 phar:///usr/local/bin/dep/src/Deployer.php(326): call_user_func(Object(Closure))                                    
 #3 phar:///usr/local/bin/dep/bin/dep(119): Deployer\Deployer::run('6.3.0',

更新 2

composer.json 有 symfony/dotenv

 "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0",
        "symfony/browser-kit": "^4.0",
        "symfony/css-selector": "^4.0",
        "symfony/debug-pack": "*",
        "symfony/dotenv": "^4.0",
        "symfony/maker-bundle": "^1.0",
        "symfony/phpunit-bridge": "^4.0",
        "symfony/profiler-pack": "*",
        "symfony/web-server-bundle": "^4.0"
    },

但仍然

namespace Deployer;

use Symfony\Component\Dotenv\Dotenv;

require 'vendor/autoload.php';

require 'recipe/symfony.php';


$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');

给我

Class 'Symfony\Component\Dotenv\Dotenv' not found                                                              


 #0 phar:///usr/local/bin/dep/src/Deployer.php(325): require()                                                          
 #1 [internal function]: Deployer\Deployer::Deployer\{closure}()                                                        
 #2 phar:///usr/local/bin/dep/src/Deployer.php(326): call_user_func(Object(Closure))                                    
 #3 phar:///usr/local/bin/dep/bin/dep(119): Deployer\Deployer::run('6.3.0', '/home/grek/Phps...')                       
 #4 /usr/local/bin/dep(4): require('phar:///usr/loc...')                                                                
 #5 {main}        

更新 3
我创建测试文件:./test.php

<?php

use Symfony\Component\Dotenv\Dotenv;

require 'vendor/autoload.php';

(new Dotenv())->load(__DIR__.'/.env');

php test.php 工作正常。

从同一位置运行 dep deploy:staging

给我

 [Error] Class 'Symfony\Component\Dotenv\Dotenv' not found                                                              


 #0 phar:///usr/local/bin/dep/src/Deployer.php(325): require()     

我的 deploy.php 文件

<?php
namespace Deployer;

use Symfony\Component\Dotenv\Dotenv;

require 'vendor/autoload.php';

require 'recipe/symfony.php';

(new Dotenv())->load(__DIR__.'/.env');

Deployer 全局安装

whereis dep
dep: /usr/local/bin/dep
4

2 回答 2

2

您是否尝试在 deploy.php 文件中启用 symfony/dotenv?

require_once 'vendor/autoload.php';

$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');

您需要首先安装 dotenv 组件:

composer require --dev symfony/dotenv

--dev如果您在仅具有生产依赖项的环境中使用该选项,请删除该选项。

于 2018-12-20T12:44:19.660 回答
0

我使用 .env 文件来生成设置文件,我解决这个问题的方法是向主机添加一个属性,指示 .env 和模板文件:

# hosts.yml
hostname:
  ...
  env_file: .env.stage
  settings_template: somewhere/file.template

# file.template
...
'database' => '{{DB_NAME}}',
...

使用函数加载凭据:

# deploy.php
// Loading of .env files
// add Symfony's dotenv as a requirement: composer global require symfony/dotenv:"^3"
function loadenv() {
    $env_file = get('env_file');
    $environment = file_get_contents($env_file);
    $dotenv = new \Symfony\Component\Dotenv\Dotenv();
    $data = $dotenv->parse($environment);
    return $data;
};

最后,创建一个调用该loadenv()函数的任务,将凭据写入文件并将其上传到服务器:

task('upload:settings', function () {
    if (askConfirmation('Are you sure to generate and upload the settings?')) {
        //Get template
        $template = get('settings_template');
        //Import secrets
        $secrets = loadenv();
        //Prepare replacement variables
        $iterator = new \RecursiveIteratorIterator(
            new \RecursiveArrayIterator($secrets)
        );
        $replacements = [];
        foreach ($iterator as $key => $value) {
            $keys = [];
            for ($i = $iterator->getDepth(); $i > 0; $i --) {
                $keys[] = $iterator->getSubIterator($i - 1)->key();
            }
            $keys[] = $key;
            $replacements['{{' . implode('.', $keys) . '}}'] = $value;
        }
        //Create settings from template
        $settings = file_get_contents($template);
        $settings = strtr($settings, $replacements);
        $tmpFilename = 'settings.tmp';
        file_put_contents($tmpFilename, $settings);
        upload($tmpFilename, '{{release_path}}/somewhere', ['options'=> ['--inplace']]);

    }
});
于 2019-10-24T14:24:04.353 回答