Custom env variables trellis

How do I add custom env variables based on trellis deploy environment?

I’m referring to the variables accessible by get_env

1 Like

If you want custom variables in the .env file created on deploy, each WP site will create env variables using the wordpress_env_defaults from deploy.yml. To override or add to these env variables, you can edit group_vars/<environment>/wordpress_sites.yml.

For example, if for example.com you wanted to override the default db_name and add a new var new_env_var, you could add this for the site example.com:

wordpress_sites:
  example.com:
  ...
  env:
    db_name: override_name
    new_env_var: foo

(Note. If you’ve already created the db and then change the db_name, the deploy won’t be able to connect to the db unless you use the original name. To use a changed db_name you’ll probably need to rerun server.yml or maybe rebuild the server completely.)

You’ll find the created .env file on the remote at /srv/www/example.com/current/.env:

DB_NAME='override_name'
NEW_ENV_VAR='foo'
etc.

Edit. If you are creating or overriding sensitive env vars like passwords, use group_vars/<environment>/vault.yml to define the vars instead.

See also Where and how are values from .env defined as PHP constants

11 Likes

This was so thorough and well written. Thank you so much fullyint!

1 Like