Provide better support for environment variables

Created on 19 May 2017, over 7 years ago
Updated 23 July 2024, 5 months ago

Problem/Motivation

Environment variables to pass in global settings/configurations are a thing:

  • Most hosters have some sort of support for them and expose the DB settings as such, for example https://github.com/pantheon-systems/drops-8/blob/master/sites/default/settings.pantheon.php#L39
  • It really explitely defined to be environment specific.
  • Many systems have .env files to share those.

Usecases

Pro/Cons

Questions

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🌱 Plan
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated 1 day ago

Created by

πŸ‡©πŸ‡ͺGermany dawehner

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    It would be lovely to use a .env to set environment variables for testing. This would make it much simpler to swap between Drupal 10 and 11 for testing purposes.

    I propose we add the Symfony dotenv library as a dependency and use it to load files from the root directory of the project. We could add

    // loads .env, .env.local, and .env.$APP_ENV.local or .env.$APP_ENV
    $dotenv->loadEnv(__DIR__.'/.env');
    

    to core/tests/bootstrap.php so environment variables are available when testing and we could add it to \Drupal\Core\DrupalKernel::bootEnvironment() to ensure it is loaded when we boot.

    This would allow consist environment management for Drupal core dev and projects built on Drupal.

  • Merge request !8886Resolve #2879846 "Provide better support" β†’ (Open) created by alexpott
  • Pipeline finished with Failed
    5 months ago
    Total: 171s
    #231990
  • Pipeline finished with Failed
    5 months ago
    Total: 526s
    #232024
  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    What I have seen is that .env files are helpful for local development, and less so for staging and prod servers which have other means of writing env variables. Given that we have standardized on DDEV for local dev, which already support setting env variables from config or .env files, do we really need this? The OP was written in an age before Docker based local dev.

  • heddn Nicaragua

    While ddev/docker does indeed support env variables natively, other hosting providers like Pantheon and the wild-west of bare metal and shared hosting do not have solutions in this space. Providing a core-supported option here might be the impetus for folks like Pantheon to switch from proprietary secrets text files to env variables.

    I could easily see us taking the base added here and doing a lot of special things in follow-ups. Like injecting the DB credentials for the primary/default DB automatically. Or providing an env variable override process for settings and config. But the base logic/support would be needed first.

  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    That sounds like a feature request for Pantheon (if their workarounds are not sufficient). Acquia and Platform support setting env variables, as do things like .htaccess and nginx config.

    Like I said, DotEnv is really made for local development, so all this talk of web hosting demonstrates how people will be confused if this goes in. If people really want this feature, its a composer require away.

    I could easily see us taking the base added here and doing a lot of special things in follow-ups. Like injecting the DB credentials for the primary/default DB automatically. Or providing an env variable override process for settings and config. But the base logic/support would be needed first.

    I'm totally on board with settings.php honoring env variables. IMO thats the first part to do, not the last.

Production build 0.71.5 2024