-2

Is their any possible way of referencing an environment variable into another one.

Here is the problem I'm trying to solve.

I have two deployment server for testing purpose both have different databases, I want to switch the DATABASE_URL secret during the deployment based on the server name provided during the deployment.

I have two database url secrets

  1. DATABASE_URL_SERVER_1
  2. DATABASE_URL_SERVER_2

in parameters I'm trying to do something like this.

parameters
    db_url: 'DATABASE_URL_%env(resolve:SERVER)%' # giving me DATABASE_URL_SERVER_1 or DATABASE_URL_SERVER_2
    env(DATABASE_URL): %env(db_url)% # Environment variable not found: "db_url".

    env(DATABASE_URL): %env(resolve:db_url)% # Environment variable not found: "db_url".
Moeen Basra
  • 733
  • 4
  • 18
  • found one reference but seems unresolved https://github.com/symfony/symfony/issues/26522 – Moeen Basra Jun 21 '22 at 11:13
  • 2
    Have a read through the [environment configuration](https://symfony.com/doc/5.4/configuration.html#configuration-environments). You can have different config files and set them depending on your environment... – Bossman Jun 21 '22 at 11:54
  • @Bossman thanks but it has to be a secret – Moeen Basra Jun 21 '22 at 12:11
  • Why did you do this? @MoeenBasra – vinceAmstoutz Jun 21 '22 at 12:22
  • Apparently its a requirement we can not reveal the DATABASE_URL in `.env`files. Please let me know if there is any possibility. – Moeen Basra Jun 21 '22 at 12:25
  • @MoeenBasra, not sure i am understanding when you say "secret"? You can have different `DATABASE_URL` values depending on your environment, test_server_1, test_server_2 etc.. If you dont want to have the URL in the env file, then set the envs on the server directly.. – Bossman Jun 21 '22 at 12:26
  • @Bossman here you can find details about the secrets https://symfony.com/doc/current/configuration/secrets.html – Moeen Basra Jun 21 '22 at 12:28
  • Ah right i get you. You could still use different environments utilising the secrets. Quote: *"Be careful that you don't accidentally define a secret and an environment variable with the same name: environment variables override secrets."*. So don't set the url in the env files.. – Bossman Jun 21 '22 at 12:39
  • Ya, there is no variable in the environment file for DATABASE_URL. I have two different secrets `DATABASE_URL_SERVER_1` and `DATABASE_URL_SERVER_2` and now I am trying to resolve the `DATABASE_URL` using the `SERVER` parameter which I'm providing during the deployment. – Moeen Basra Jun 21 '22 at 12:50
  • You can't set the `DATABASE_URL` as it doesn't exist, like you said and have shown. Have different config files for your databases for your each environment, one with `db_url: 'DATABASE_URL_SERVER_1'` and another with `db_url: 'DATABASE_URL_SERVER_2'`. Definatly worth a good read [environment configuration](https://symfony.com/doc/5.4/configuration.html#configuration-environments). – Bossman Jun 21 '22 at 13:04
  • @Bossman I've checked the documentation already but there is no reference for this, I was looking for a solution or an expert opinion if this is possible or not. – Moeen Basra Jun 22 '22 at 06:01

1 Answers1

2

I don't think you understand how environments work as seen from the comments. Environment configuration is used to achieve EXACTLY what you are trying to do but this resolving variable thing is not how you do it.

Your SERVER1 is one of your environment and your SERVER2 is your other environment.

So instead of having a convoluted 'DATABASE_URL_%env(resolve:SERVER)% and then resolving $SERVER, the easier (and correct) way to do it as follows.

Instead of defining you SERVER variable, you should define you APP_ENV variable in a .env.local file (this file is local to the server and is not committed). https://symfony.com/doc/5.4/configuration.html#selecting-the-active-environment

So for example, on server1, create a local file called .env.local and set the APP_ENV to "server1".Similary on server2, do the same with APP_ENV value as "server2".

Then you can set the secret url for DATABASE_URL (the same variable for both servers) using the secret command as shown in this documentation. "prod" and "dev" refers to the env so replace prod or dev with server1 or server2 as applicable. https://symfony.com/doc/5.4/configuration/secrets.html#create-or-update-secrets

Now Symfony will automatically resolve and choose the correct value of secret based on the APP_ENV value in the local file. Read about environment configurations to know better how this works.

About you question whether your weird way of doing it is possible, the answer is no. It is not supported in Symfony because that is not the correct way to do it and there is no reason this needs to be a thing.

Akhil
  • 170
  • 8