How to set environment variables in drush.yml or other config file?

I want to set environment variables in drush.yml

  1. Locate or Create drush.yml: Find the drush.yml file in your Drush configuration directory. If it’s not there, create one. You might place it in the site-specific directory (sites/default/drush.yml) or globally (~/.drush/drush.yml).

  2. Edit drush.yml: Open the file in a text editor. Use a simple YAML syntax to define your environment variables:

    variables:
      MY_VARIABLE: 'my_value'
      ANOTHER_VARIABLE: 'another_value'
    

    Replace MY_VARIABLE and ANOTHER_VARIABLE with your desired variable names and corresponding values.

  3. Save Changes: Save the drush.yml file.

Here’s an example:

variables:
  DATABASE_URL: 'mysql://user:password@localhost/db_name'
  MY_CUSTOM_VARIABLE: 'custom_value'

Once set, you can use these variables within Drush commands or scripts. If you need them more broadly, you can access them using PHP functions like getenv() in your custom Drush commands or scripts.

Remember, while drush.yml is specific to Drush configuration, for more general environment variables accessible system-wide, consider setting them at the system level or through tools like Docker.

1 Like