2

I'm looking how I can set up host user and pass from env

this don't work - need string not function

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

in Symfony 4 I use env and don't want setup hosts in ~/.ssh/config

Update1 :

<?php
namespace Deployer;

use Symfony\Component\Dotenv\Dotenv;

require 'recipe/symfony.php';

require_once 'vendor/autoload.php';

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

then

dep deploy production

get

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',

Update 2

composer.json has 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"
    },

but still

namespace Deployer;

use Symfony\Component\Dotenv\Dotenv;

require 'vendor/autoload.php';

require 'recipe/symfony.php';


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

give my

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}        

UPDATE 3
I create test file : ./test.php

<?php

use Symfony\Component\Dotenv\Dotenv;

require 'vendor/autoload.php';

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

php test.php works fine.

Run from same location dep deploy:staging

give my

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


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

my deploy.php file

<?php
namespace Deployer;

use Symfony\Component\Dotenv\Dotenv;

require 'vendor/autoload.php';

require 'recipe/symfony.php';

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

Deployer is installed globaly

whereis dep
dep: /usr/local/bin/dep
Developer
  • 2,731
  • 2
  • 41
  • 71

2 Answers2

2

Did you tried to enable symfony/dotenv in your deploy.php file?

require_once 'vendor/autoload.php';

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

You need first to install dotenv component:

composer require --dev symfony/dotenv

Remove the --dev option if you use it in an environment with only production dependencies.

Nek
  • 2,715
  • 1
  • 20
  • 34
  • So I did! Use composer info to know if you installed dotenv component. – Nek Dec 20 '18 at 12:51
  • composer info : symfony/dotenv v4.2.1 Registers environment variables from a .env file, exist but not work – Developer Dec 20 '18 at 13:13
  • I had to run this instead: `require __DIR__ . '/bootstrap/autoload.php'; \Dotenv\Dotenv::create(__DIR__)->load();` – Ryan Nov 07 '19 at 20:23
0

I'm using .env files to generate settings files and the way I've got around this was to to add a property to the host that indicates the .env and template files:

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

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

Load the credentials with a function:

# 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;
};

Finally, create a task that calls the loadenv() function, writes the credentials to a file and uploads it to the server:

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']]);

    }
});
tmsss
  • 1,979
  • 19
  • 23