[MAGNOLIA-7883] Enable different configuration for different environments Created: 21/Sep/20  Updated: 22/Sep/22  Resolved: 20/May/21

Status: Closed
Project: Magnolia
Component/s: None
Affects Version/s: 6.2.3
Fix Version/s: 6.2.9

Type: Story Priority: Neutral
Reporter: Christopher Zimmermann Assignee: Maxime Michel
Resolution: Fixed Votes: 0
Labels: freetrials
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relates
documentation
to be documented by MAGNOLIA-8106 DOC: Different configurations for dif... Closed
relation
is related to MAGNOLIA-7882 Enable different configuration for pu... Accepted
is related to MAGNOLIA-8126 Interpolation of magnolia properties ... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[X]* Doc/release notes changes? Comment present?
[X]* Downstream builds green?
[X]* Solution information and context easily available?
[X]* Tests
[X]* FixVersion filled and not yet released
[X]  Architecture Decision Record (ADR)
Release notes required:
Yes
Documentation update required:
Yes
Date of First Response:

 Description   

As a developer, I want to be able to provide different configuration to different environments, so that I can handle the different needs of the different environments such as dev, integration and production.

Context

Configurations can be supplied in light modules or Java modules. Typically, the same definition configurations are used in all environments, with the only thing changing being the new configuration that is being tested. 

However in some cases we want different configurations in one or more of these environments, for example we may want a restClient to point to a sandbox API while in development, but point to a production API while in production. Or on a site definition, we might want to use differnet domain names based on environment.

Bigger Picture

Consider the related need of applying differnet configurations to author and public instances within each of those environments as well.

See: https://jira.magnolia-cms.com/browse/MAGNOLIA-7882



 Comments   
Comment by Maxime Michel [ 20/May/21 ]

What we ended up implementing is that, only when the (new) feature flag magnolia.yaml.envsubst is set to true, i.e. -Dmagnolia.yaml.envsubst=true, then the following syntax in a YAML file:

baseUrl: !env https://${ENV}.api.com/endpoint

Will parse the environment variable ${ENV}. Property can of course be anything, not only baseUrl. Same with the environment variable, $ENV is only an example.

Important note: this won't work in YAML decorator files, only in plain YAML definitions.

Comment by Christopher Zimmermann [ 02/Jun/21 ]

I can have multiple environment variables right?

BASE_URL

PREVIEW_URL

MY_BUNNY_FACE 

etc?

Then

faces: !env https://${MY_BUNNY_FACE}/eyes

Comment by Christopher Zimmermann [ 02/Jun/21 ]

Where does it read the variables from - only from the system itself - or is it possible to supply a file - similar to '.env' in frontend frameworks? Or other locations?

Comment by Maxime Michel [ 02/Jun/21 ]

I can have multiple environment variables right?

I have not tried that but I don't see any reason why not.

Where does it read the variables from - only from the system itself - or is it possible to supply a file - similar to '.env' in frontend frameworks? Or other locations?

The util we use to do the substitution has a couple of options:

"Base64 Decoder: ${base64Decoder:SGVsbG9Xb3JsZCE=}\n"
"Base64 Encoder: ${base64Encoder:HelloWorld!}\n"
"Java Constant: ${const:java.awt.event.KeyEvent.VK_ESCAPE}\n"
"Date: ${date:yyyy-MM-dd}\n" + "DNS: ${dns:address|apache.org}\n"
"Environment Variable: ${env:USERNAME}\n"
"File Content: ${file:UTF-8:src/test/resources/document.properties}\n"
"Java: ${java:version}\n" + "Localhost: ${localhost:canonical-name}\n"
"Properties File: ${properties:src/test/resources/document.properties::mykey}\n"
"Resource Bundle: ${resourceBundle:org.example.testResourceBundleLookup:mykey}\n"
"Script: ${script:javascript:3 + 4}\n" + "System Property: ${sys:user.dir}\n"
"URL Decoder: ${urlDecoder:Hello%20World%21}\n"
"URL Encoder: ${urlEncoder:Hello World!}\n"
"URL Content (HTTP): ${url:UTF-8:http://www.apache.org}\n"
"URL Content (HTTPS): ${url:UTF-8:https://www.apache.org}\n"
"URL Content (File): ${url:UTF-8:file:///${sys:user.dir}/src/test/resources/document.properties}\n"
"XML XPath: ${xml:src/test/resources/document.xml:/root/path/to/node}\n");

That being said, we chose to only allow environment variables for now.

For the .env file, I think that's a good suggestion, but I don't think it's the same mechanism. Magnolia shouldn't be looking up environment variables in the .env file when it comes to resolving them. Instead, it should convert read the .env file and export the environment variables as soon as the light module gets loaded.

Generated at Mon Feb 12 04:27:41 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.