Sitecore Experience Commerce – Conditional Configuration

So recently I had to create a Continuous Delivery setup for a Sitecore Commerce environment. This included 4 different Azure WebApps to which Sitecore XC should be deployed to. One of the struggles I had right from the start, is configuration. The configuration has to vary between all the environments (like Testing, Acceptance and Production) because, for example, you don’t want to use the same SQL connection settings on all your environments.
With the release of Sitecore XP 9 rule-based configuration was introduced, and that works like a charm and is precisely what I would want as well for my XC setup. But as far as I could find out, also after asking the Sitecore product team, there was no such thing in XC. So that left me with two options, either I would have to create some funky stuff inside the release pipeline in Azure DevOps(VSTS) to alter/overwrite some specific JSON configuration files, or I would have to create my own kind of rule-based configuration.

So that’s what I did!

How does it work? When you bootstrap your XC Environment, the BootstrapPipeline gets run. This pipeline only contains one single BootStrapImportJsons block, which was quite easy to replace with my own version. This block, by default, checks if the JSON it is trying to import has a type specified and runs either the ImportEnvironmentCommand or the ImportPolicySetCommand based on that. So, what I did was add a new Entity based on the PolicySet Entity, which contains a “Conditions” dictionary.

{
  "$type": "Sitecore.Commerce.Plugin.ConditionalConfigs.Entities.ConditionalPolicySet, Sitecore.Commerce.Plugin.ConditionalConfigs",
  "Id": "Entity-PolicySet-ContentPolicySet",
  "Version": 1,
  "IsPersisted": false,
  "Name": "ContentPolicySet",
  "Policies": {
    "$type": "System.Collections.Generic.List`1[[Sitecore.Commerce.Core.Policy, Sitecore.Commerce.Core]], mscorlib",
    "$values": []
  },
  "Conditions":{
    "HostingEnvironment": "Development"
  }
}

The key of this dictionary represents the AppSetting name. The value, which can be a Regex, will then be checked for a match before running the ImportPolicySetCommand to import the PolicySet. And that is pretty much it, way easier than I expected it to be!

The source code is available on GitHub: https://github.com/GuidovTricht/Sitecore.Commerce.Plugin.ConditionalConfigs