Using Environment Variables in OSGi configurations as part of String | Community
Skip to main content
NageshRaja
Level 5
November 6, 2024
Solved

Using Environment Variables in OSGi configurations as part of String

  • November 6, 2024
  • 4 replies
  • 2220 views

Hey all,

 

I am trying to use environment variables to setup certain configurations -

 

{ "getAPIEndPoint": "$[env:ENDPOINT_Sys;default=/public/dev4/v1/store/store]", }

 

 

This I know if allowed but is it possible to reference the environment variable directly in any OSGi configuration as shown below -
Here the api_env is an environment variable but not tied to any OSGi configuration - Is it allowed to use this environment variable directly on the fly?

 

{ "getAPIEndPoint": "/public/$[env:api_env;default=dev4]/v1/category/api/v1/categories", }

 

As per https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager-forms/how-to-access-environment-variables-from-aem-as-cloud-service/td-p/635078 we need to establish an OSGi configuration if we want to use an environment variable as part of another java osgi service.

 

thanks,

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Rohan_Garg

@nageshraja - It will work as part of the string too.

{ "getAPIEndPoint": "/public/$[env:api_env;default=dev4]/v1/category/api/v1/categories", }

Assuming your api_env has value 123 set in environment variable it will set the config as -  /public/123/v1/category/api/v1/categories else it will be DEFAULT -  /public/dev4/v1/category/api/v1/categories

 

Hope this helps!

4 replies

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 6, 2024

Hi @nageshraja,

I believe you can use Environment variables directly in your code without having them included in the OSGi configuration. I haven't tried it myself, but the Java API: System.getenv("YOUR_ENV_VARIABLE") should work normally.

 

Good luck,

Daniel

NageshRaja
Level 5
November 7, 2024

yeah I don't want to actually change any code if possible was hoping this too work directly! thanks for the response!

Rohan_Garg
Community Advisor
Rohan_GargCommunity AdvisorAccepted solution
Community Advisor
November 6, 2024

@nageshraja - It will work as part of the string too.

{ "getAPIEndPoint": "/public/$[env:api_env;default=dev4]/v1/category/api/v1/categories", }

Assuming your api_env has value 123 set in environment variable it will set the config as -  /public/123/v1/category/api/v1/categories else it will be DEFAULT -  /public/dev4/v1/category/api/v1/categories

 

Hope this helps!

NageshRaja
Level 5
November 7, 2024

thanks for the confirmation mate! kudos! 🙂

arunpatidar
Community Advisor
Community Advisor
November 6, 2024
NageshRaja
Level 5
November 7, 2024

thanks arun i did check but was not sure whether it would work as part of string too!

Tethich
Community Advisor
Community Advisor
November 6, 2024

hi @nageshraja 

I believe that Cloud evaluates your configs and wherever it finds placeholders like $[secret:SECRET_VAR_NAME] or $[env:ENV_VAR_NAME] it replaces them with the var values (if there are any), or replaces them with the default values (if any were provided) or it replaces them with nothing (I believe you'll have an empty string).

 

But this is really simple to test out. Just add some configs and see how things behave (learning through empirical)

NageshRaja
Level 5
November 7, 2024

hey @tethich, we still don't have access to the cloud so just preparing the code migration changes which will come up hence the reason for asking!