self-hosted icon indicating copy to clipboard operation
self-hosted copied to clipboard

Can overriding .env variable be done differently?

Open dannyhajj opened this issue 1 year ago • 5 comments

Problem Statement

Currently, overriding .env variables requires creating a .env.custom. This approach currently completely ignores .env if .env.custom exists. This is slightly problematic during upgrades if we forget to update the .env.custom file with the updated values from .env.

Solution Brainstorm

In order not to break compatibility or make it a significant change, my suggestion is something as follows:

  • Support a .env.override file during installation
  • If .env.override exists, .env.custom will be overwritten with an auto-generated before this code block: https://github.com/getsentry/self-hosted/blob/0d3191d9ac0b09a16ab792d04e5dc8f6766e13e2/install/_lib.sh#L24-L28
  • The auto-generated file can be something like cat .env .env.override > .env.custom (of course, this can be done differently, but if the environment exists multiple times, the last one will take effect).
  • The rest of the process will stay as is, and starting the Docker containers will still be using .env.custom.
    docker-compose --env-file /opt/sentry/.env.custom up -d
    

dannyhajj avatar Jan 18 '23 09:01 dannyhajj

Thanks for bringing this up, will categorize this as an enhancement

hubertdeng123 avatar Jan 18 '23 22:01 hubertdeng123

Why not just merge them? Read .env first and then .env.custom (or .env.local?) to overwrite values we want to change. Is there any specific problem with such approach?

fliespl avatar Jan 18 '24 07:01 fliespl

Why not just merge them? Read .env first and then .env.custom (or .env.local?) to overwrite values we want to change. Is there any specific problem with such approach?

No reason not to. We can definitely introduce some sort of logic to merge these two together.

hubertdeng123 avatar Jan 19 '24 23:01 hubertdeng123

Another way is now available with docker compose. However, this requires docker-compose v2.24+, which is only one month old. So, I'm not sure if it's something that can be used already (I am not sure what Sentry's policy is regarding this).

Anyway, docker compose now supports optional env files, so it can now be solved by changing the env_file section in the docker-compose.yml file:

    env_file:
      - path: ./.env
        required: true
      - path: ./.env.custom
        required: false

ref: https://docs.docker.com/compose/environment-variables/set-environment-variables/#additional-information

dannyhajj avatar Feb 12 '24 14:02 dannyhajj

Thanks for bringing this up. That's awesome, but we'll want to at least wait since this is a relatively new change and we have many users right now that are under docker compose v2.24.

hubertdeng123 avatar Feb 13 '24 23:02 hubertdeng123