Working with environment variables in your Sitecore solution – Let AZURE CLI guide you


Dear fellow sitecorians! I hope you are enjoying the new year, the spring is in the air ๐Ÿ˜‰

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)

Then as Brian instructed in his post. Create the environment variable locally and that’s it ๐Ÿ™‚

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.

So just add the Azure CLI task to your release pipeline.
Select your Azure subscription and go for Inline script. The script is taken from az functionapp config appsettings:

 
az webapp 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 ๐Ÿ™‚


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.