phpphp-deployersymfony6

Deployer 7 distribution use dotenv variables


Problem

Using deployer in a Symfony 6 applaction has required me to change from deployer/deployer to deployer/dist (7.0.0-rc.3), with the downside being that I cannot load enviroment variables from my .env anymore with the way I used to do it.

Situation

With deployer/deployer I had the following example to use the dotenv variables in the deploy script:

<?php

namespace Deployer;

use Symfony\Component\Dotenv\Dotenv;

require 'recipe/common.php';
require 'contrib/discord.php';

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

set('application', $_ENV['APP_NAME']);
set('discord_channel', $_ENV['DISCORD_DEPLOY_CHANNEL']);
set('discord_token', $_ENV['DISCORD_DEPLOY_TOKEN']);

But with deployer/dist I can no longer do this, giving the error message:

Class "Symfony\Component\Dotenv\Dotenv" not found

Could someone firstly explain why it isn't working anymore? And secondly, what could be a possible solution (or alternative) for loading the environment variables from my .env file?


Solution

  • The dist version includes its own dependencies in the phar package (and it does not depend on dotenv), while the deployer version uses your project's own dependencies. Since your project is most likely using the dotenv component, you were able to use it in your deploy script.

    But since you are using symfony 6 you cannot install deployer 7 because it depends on symfony 5, causing a conflict with your project.

    It's a bit of a hassle, but you could fork the package to add the dotenv dependency and build your own phar with the provided bin/build script, and copy it manually to your project.