In most cases, any type of software development requires several variables to be configurable as per environments/client/requirements, and for those to be able to be managed without any development effort.
Developers use several options to create such variables –
- Constants
- Configuration Files
- Online resources
It is always better to have developer independent options providing control to the DevOps teams to manage these configurations as required.
In Sitecore, we generally use the Sitecore Patch Configuration Files instead of directly changing Sitecore OOTB configuration files.
How do we pass the dynamic configuration to the deployment in Containers
To understand this, let’s assume that in our code base, we have to access the notification/service/domain-related details of configuration files by considering different environments can have different values.
Solution
For the above use case, we can use the following approaches:
Option – 1:
Add configuration values to Sitecore Patch Configuration File and deploy the same values in all environments if there are no environment-specific changes
Option – 2:
Add configuration values to Web Config File and deploy different values in different environments using config transformation.
You can check more details at ASP.NET Web Deployment using Visual Studio: Web.config File Transformations and Transform web.config
Option – 3:
Add configuration values to the Web Config File and use environment variables to replace environment-specific values.
You can check more details at Configuration builders for ASP.NET
Option – 4:
Use environment variables in the Sitecore code base and environment-specific environment variables would be available
Option – 5:
Add configuration values to Sitecore Patch Configuration and use environment variables to replace environment-specific values
Let’s also look at the schema of the configuration files and code blocks to access the configuration values.
Configuration files and Code:
Option – 1:
Configurations
Code Block
Sitecore.Configuration.Settings.GetSetting("NotificationAPI");
Option – 2:
Configurations
Code Block
ConfigurationManager.AppSettings["Environment"];
Option – 3:
Configurations
Code Block
string ServiceID = ConfigurationManager.AppSettings["ServiceID"];
string AppSetting_default = ConfigurationManager.AppSettings["default"];
string ConString = ConfigurationManager.ConnectionStrings["default"]?.ConnectionString;
If you will not define variables in Environment then it will take the default value from the Web. Config file.
I have only defined ServiceID then the rest of the settings, e.g.default value would be served from the Web. Config file
Option – 4:
Configurations
Code Block
System.Environment.GetEnvironmentVariable("AppSetting_ServiceID");
Option – 5:
Configurations
Code Block
Sitecore.Configuration.Settings.GetSetting("ServiceID");
With the help of these options, you can dynamically pass the required configuration to your code base running in the Sitecore Docker Container.
Credit/References
- Use a patch file to customize the Sitecore configuration
- Configuration patch file examples
- Sitecore Docker Build Connection String
- The structure of an SCCPL transformation
- Windows Azure Web Sites: How Application Strings and Connection Strings Work
- Building Microsoft Azure DevOps Build Pipeline
Amit is an IT Solution Architect with Assurex. Reach out to Amit on LinkedIn
.