Today’s short post will be about environment variables and how to to use them in your Sitecore solution/application.
There are already some really good posts about it:
Vitalii Tylyk’s great post – Sitecore environment variables config builder
– This is really interesting, using environment variables everywhere(not only in Sitecore settings config)
Brian Pedersen’s post – Sitecore use global and environment variables in config files
– Good instructive post
As Brian explained in his post, Sitecore has been supporting environment variables since Sitecore 8.2. So it’s been around for a while… 🙂
*To be honest, I haven’t had the need of using environment variables in a Sitecore solution/application…
But since the Azure era (especially Azure Paas), environment variables are indeed very useful.
So how to use environment variables in your solution? You just do something like this:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <settings> <setting name="ImASetting" value="$(env:SITECORE_SETTINGS_IM_A_SETTING)" /> </settings> /sitecore> </configuration>
*Good practice is to prefix the environment variables, so we know where they belong(Thanks to Vitalii Tylyk’s great post)
That’s all good… But how do we do this in an Azure Paas Sitecore web app?
Well, it’s quite easy. Just open up your web app in Azure Portal, locate Configuration and start adding your environment variables(application settings):
Hmm ok, but I want to create/set my environment variables on the fly. Let’s say when I deploy my web app…
No worries, you can do this in your release pipeline. And use the wonderful and powerful Azure CLI – Azure Command-Line Interface.
The Azure command-line interface (CLI) is Microsoft’s cross-platform command-line experience for managing Azure resources. The Azure CLI is designed to be easy to learn and get started with, but powerful enough to be a great tool for building custom automation to use Azure resources.
az functionapp config appsettings set --name $(appName) --resource-group $(resourceGroup) --settings "SITECORE_SETTINGS_IM_A_SETTING=$(Build.BuildId)"
Here we are using the predefined variable – Build.BuildId
That’s all for now folks 🙂