Configuration as Code using Azure DevOps variable templates
Published: 2021-03-09 21:32:47

---
title: Configuration as Code using Azure DevOps variable templates
published: true
date: 2020-10-31 00:00:00 UTC
tags: 
canonical_url: https://gavincampbell.dev/post/config-code-azure-devops-variable-templates/
---

One of the principles of the [twelve-factor app](https://12factor.net/) methodology is [strict separation between code and config](https://12factor.net/config), where _config_ means everything that is likely to vary between deployments, and _code_ is everything that doesn’t. Historically, was not a good fit for .NET Framework applications, which relied on tools such as [`web.config` transformation](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-3.1) and [Slow Cheetah](https://github.com/microsoft/slow-cheetah) to apply build-time transformations to application configuration files. These transformations are based on environment-specific config files stored alongside the application code in the source repo. In the past, this led some commentators to conclude that certain aspects of 12-factor were [just not applicable to .NET applications](https://stackoverflow.com/questions/11086388/is-there-an-example-of-a-twelve-factor-app-with-tweaks-specifically-for-a-net-t).<sup id="fnref:1"><a href="#fn:1" role="doc-noteref">1</a></sup> The world has moved on since then, as has the .NET framework, and separation of code and config is much easier to accomplish. This article will describe one technique for managing version-controlled configuration data alongside - but separate from - our application code using Azure DevOps [variable templates](https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#variable-reuse).

## The example

The example project I am using is the sample ASP.NET Core MVC application for Cosmos DB, which is described in a [tutorial on docs.microsoft.com](https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-dotnet-application) as well as being available to [download from GitHub](https://github.com/Azure-Samples/cosmos-dotnet-core-todo-app). In brief, this consists of a simple website that provides CRUD<sup id="fnref:2"><a href="#fn:2" role="doc-noteref">2</a></sup> operations for a “to-do list” stored in Azure Cosmos DB. I chose this particular example as it requires a handful of configuration items, of which one - the Cosmos DB API key - is a secret. Most real life projects, at least the ones I’ve worked on, require a much larger number of variables to be set.

By default, ASP.NET Core web apps are provided with a [default configuration builder](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1#default-builder-settings-1) that reads configuration from a number of sources in a predetermined order. The important thing to know here is that environment variables take precedence over values in `appsettings.json`. This means that `appsettings.json` and its cousins `appsettings.dev.json`, `appsettings.test.json` and so on can be relegated to providing configuration values on the developer’s desktop, with all other environments being configured via environment variables. As it happens, there is an open-source nuget package [`DotNetEnv`](https://www.nuget.org/packages/DotNetEnv/) which makes it easier to dispense with `appsettings.json` altogether, but I won’t discuss this further here since the example project doesn’t use it. The disadvantage of `appsettings.json` is of course that it’s too easy to end up doing [this kind of thing](https://github.com/Azure-Samples/cosmos-dotnet-core-todo-app/commit/750b594835f0198899cda0424f5ca5fffe7199f5#diff-4f3092c803fde3de70bbd69d14f1156cf031d668c8696dcdaa21c83eb0aaa04c).

## The config repo

The [project](https://dev.azure.com/gavincampbell-dev/code-config-separation-example) contains two repos; [`sample-app-code`](https://dev.azure.com/gavincampbell-dev/code-config-separation-example/_git/sample-app-code) and [`sample-app-config`](https://dev.azure.com/gavincampbell-dev/code-config-separation-example/_git/sample-app-config).

`sample-app-code` is just a fork of the public repo from GitHub, with the addition of an `azure-pipelines.yml` file to build and release the application. `sample-app-config` contains one `.yml` file per environment, which I’ve named after the environment.

![One template file per environment](https://gavincampbell.dev/post/config-code-azure-devops-variable-templates/variable-templates-in-config-repo.png) <figcaption>
<p>One template file per environment</p>
</figcaption>

These `.yml` files are known as [pipeline templates](https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops). When we refer to a pipeline template in our pipeline, the entire contents of the template file is inserted at that point. Pipeline templates can be used to define and re-use a number of pipline objects, such as stages, jobs, steps, and, in our case, variables.

> In general you can’t mix different types of objects, such as variables and steps for example, in a single template - they would end up being inserted at the “wrong” place in the pipeline.

Each template file contains a variable for everything that is likely to change between deployments, or in other words the configuration. These variables will be available to our pipeline, as well as being available as environment variables to our scripts<sup id="fnref:3"><a href="#fn:3" role="doc-noteref">3</a></sup>.

```
# This is a comment. Take that, appsettings.json!
variables:
#deployment config
serviceConnectionName: myNonProdServiceConnection
resourceGroupName: rg-todo-staging
resourceGroupLocation: uksouth
#Cosmos DB Config
cosmosAccountName: cosmos-ac-todo-staging
keyVaultName: kv-todo-staging-fdkjiwh
#Web app config
appServicePlanName: asp-todo-staging
servicePlanSku: S1
# need single quotes around double quotes to pass the pipe and the double quotes!
runtimeVersion: '"DOTNETCORE|3.1"'
webAppName: todo-staging-vdorpsbzaoiku

```

If you do this in “real life”, it’s up to you whether you create one config repo per code repo, or share a single config repo between a number of code repos. In the latter case you’d need to come up with some sort of folder structure and/or naming convention, e.g.

```
.
├── app1
│ ├── production.yml
│ └── staging.yml
└── app2
├── otherenv.yml
├── production.yml
└── staging.yml

```

or

```
.
├── app1-production.yml
├── app1-staging.yml
├── app2-otherenv.yml
├── app2-production.yml
└── app2-staging.yml

```

## The Pipeline

The [pipeline file](https://dev.azure.com/gavincampbell-dev/code-config-separation-example/_git/sample-app-code?path=%2Fazure-pipelines.yml) is pretty short, so I have included it in its entirety here. During the build stage, we just do `dotnet publish` and publish the resulting package as a [pipeline artifact](https://docs.microsoft.com/en-us/azure/devops/pipelines/artifacts/pipeline-artifacts?view=azure-devops&tabs=yaml) for later consumption. The deployment stages have been encapsulated into a job template that takes two parameters. The first is the name of the environment we are deploying to, and the second is the name of a variable group containing some additional secret variables - since we don’t want to store secret variables in our config repo.

![Enviroments in Azure Pipelines](https://gavincampbell.dev/post/config-code-azure-devops-variable-templates/environments-in-azure-devops-ui.png) <figcaption>
<p>Enviroments in Azure Pipelines</p>
</figcaption>

I created an approval on the production environment [using the UI](https://gavincampbell.dev/post/azure-devops-predeployment-approval-multistage-pipeline/).

Observant readers will have noticed that the value we pass in the `stageName` parameter matches the name of the yml file in the config repo as well as the name of the enviroment as defined in Azure Pipelines; this matters as we are going to use this parameter to figure out which variable template to use as well as which enviroment to deploy to. Of course, if you wanted these names to be different, you could accomodate this with some additional mapping logic. Unlike the variable templates, the job template `pipelineTemplates/doTheDevOps.yml` is stored in the same repo as the code, since it doesn’t vary between environments.

The effect of this is that every stage in our pipeline is deployed _identically_. We use the same template for deployment, and we tell the template where to retrieve its configuration. This should greatly increase our confidence in deploying to production, as since we use the same code everywhere we have lots of chances to “practice” before we do the production deployment.

```
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
resources:
repositories:
- repository: config
type: git
name: sample-app-config
stages:
- stage: buildAndPublish
displayName: Build project and publish artifact
jobs:
- job: buildAndPublish
displayName: Build and Publish
steps:
- task: DotNetCoreCLI@2
displayName: dotnet publish
inputs:
command: 'publish'
publishWebProjects: true
arguments: '-c Release -o $(Build.ArtifactStagingDirectory)/todo'
modifyOutputPath: false
- task: PublishPipelineArtifact@1
displayName: Publish todo.zip as Pipeline Artifact
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)/todo/todo.zip'
artifact: 'todozip'
publishLocation: 'pipeline'
- stage: deployStaging
displayName: Deploy Staging Environment
jobs:
- template: pipelineTemplates/doTheDevOps.yml
parameters:
stageName : sample_app_staging
variableGroupForSecrets : SecretVarsForStaging
- stage: deployProduction
displayName: Deploy Production Environment
jobs:
- template: pipelineTemplates/doTheDevOps.yml
parameters:
stageName : sample_app_production
variableGroupForSecrets : SecretVarsForProduction

```

## The deployment job

As the name suggests, [this file](https://dev.azure.com/gavincampbell-dev/code-config-separation-example/_git/sample-app-code?path=%2FpipelineTemplates%2FdoTheDevOps.yml) is where the action takes place. In summary, we create our supporting infrastructure<sup id="fnref:4"><a href="#fn:4" role="doc-noteref">4</a></sup> - the CosmosDB account, a key vault to hold the CosmosDB Key, the App Service plan, and then deploy our web app with appropriate configuration.

In order to demonstrate the use of variable groups to hold secret variables for YAML pipelines, I’m also adding an additional sensitive setting that is never used by the app. Other than for holding secrets that need to be masked in the pipeline log output, I think using variable templates removes the need for variable groups. Once again the template file is short enough to reproduce in full here:

```
parameters:
- name: stageName
type: string
- name: variableGroupForSecrets
type: string
jobs:
- deployment: deploy${{ parameters.stageName }}
displayName: Deploy ${{ parameters.stageName }} Environment
environment: ${{ parameters.stageName }}
variables:
- template: ${{ parameters.stageName }}.yml@config
- group: ${{parameters.variableGroupForSecrets}}
strategy:
runOnce:
deploy:
steps:
- task: AzureCLI@2
inputs:
azureSubscription: ${{ variables.serviceConnectionName }}
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
 az group create --name $(resourceGroupName) --location $(resourceGroupLocation)
az appservice plan create --name $(appServicePlanName) --resource-group $(resourceGroupName) \
--sku $(servicePlanSku) --is-linux
az webapp create --resource-group $(resourceGroupName) --plan $(appServicePlanName) --name $(webAppName) \
--runtime $(runtimeVersion)
az webapp identity assign --name $(webAppName) --resource-group $(resourceGroupName)
# Grant the web app identity permission to read secrets from the vault
az keyvault create --resource-group $(resourceGroupName) --location $(resourceGroupLocation) --name $(keyVaultName)
principal=$(az webapp identity show --resource-group $(resourceGroupName) --name $(webAppName) | jq -r .principalId)
az keyvault set-policy --name $(keyVaultName) --secret-permissions get --object-id $principal
# Add our secret var to the key vault
az keyvault secret set --vault-name $(keyVaultName) --name PointlessSecret --value $(PointlessSecret)
# Create cosmosdb account and retrieve uri and key for app settings
az cosmosdb create --name $(cosmosAccountName) --resource-group $(resourceGroupName)
cosmosUri=$(az cosmosdb show --name $(cosmosAccountName) --resource-group $(resourceGroupName) | jq -r .documentEndpoint)
cosmosAccountKey=$(az cosmosdb keys list --name $(cosmosAccountName) --resource-group $(resourceGroupName) | jq -r .primaryMasterKey)
# Add cosmos key to key vault 
az keyvault secret set --vault-name $(keyVaultName) --name cosmosAccountKey --value $cosmosAccountKey
# Get Secret Uris to add to app settings
cosmosKeyKvUri=$(az keyvault secret list-versions --name cosmosAccountKey --vault-name $(keyVaultName) --maxresults 1 | jq -r .[0].id)
pointlessSecretUri=$(az keyvault secret list-versions --name PointlessSecret --vault-name $(keyVaultName) --maxresults 1 | jq -r .[0].id)
# Set up app settings
az webapp config appsettings set --resource-group $(resourceGroupName) --name $(webAppName) \
--settings Logging __LogLevel__ Default=Warning \
AllowedHosts="*" \
CosmosDb__Account=$cosmosUri \
CosmosDb__Key="@Microsoft.KeyVault(SecretUri=$cosmosKeyKvUri)" \
CosmosDb__DatabaseName=Tasks \
CosmosDb__ContainerName=Item \
PointlessSecret="@Microsoft.KeyVault(SecretUri=$pointlessSecretUri)"
# set web app to mount zip file read-only https://docs.microsoft.com/en-us/azure/app-service/deploy-run-package
az webapp config appsettings set --resource-group $(resourceGroupName) --name $(webAppName) --settings WEBSITE_RUN_FROM_PACKAGE="1"
az webapp deployment source config-zip --resource-group $(resourceGroupName) --name $(webAppName) --src $(Pipeline.Workspace)/todozip/todo.zip

```

There isn’t really much going on here, we create an app service plan and web app, along with a key vault to hold the Cosmos Key, as well as the secret we are reading from the variable group. The web app is created with a managed identity, which we retrieve in order to grant access to the key vault. Next, we create the Cosmos DB Account, and store the account key in the key vault. Finally, we set up all of the app settings, including the key vault references, and deploy the web app. Since this application doesn’t write anything to local paths, we are taking advantage of the facility to [mount our zipped deployment package directly as a read-only filesystem](https://docs.microsoft.com/en-us/azure/app-service/deploy-run-package).

When we run the pipeline, we build the app and deploy it to each of our stages in succession:

![The completed pipeline run](https://gavincampbell.dev/post/config-code-azure-devops-variable-templates/pipeline-run-complete.png) <figcaption>
<p>The completed pipeline run</p>
</figcaption>

If we inspect the logs, we can see that the steps from our template have been included just as if they were defined in the main `azure-pipelines.yml` file:

![The pipeline logs](https://gavincampbell.dev/post/config-code-azure-devops-variable-templates/pipeline-stage-logs.png) <figcaption>
<p>The pipeline logs</p>
</figcaption>

## The payoff

Whilst this approach requires a bit of work to set up, it delivers a number of benefits.

- There is no need to have any logic in our source repo based on what environment we are deploying into.
- Changes to our application config don’t mean we have to rebuild our application code, we can redploy the same version with the new config.
- Since the config repo can be versioned, it is possible for different code repos to pin to different versions of the config.
- Config repos often contain cost-sensitive items, such as the number or size of VMs being deployed to a particular environment. Having this stored in Azure Repos enables us to use the usual source-code management and audit techniques to control changes to these variables
- We can add a new environment to our release pipeline just by creating a new config file in the config repo and a stanza of the following form in the release pipeline.
```
 - stage: deployWhatever
displayName: Deploy Whatever Environment
jobs:
- template: pipelineTemplates/doTheDevOps.yml
parameters:
stageName : sample_app_whatever
variableGroupForSecrets : SecretVarsForWhatever

```
<!-- raw HTML omitted --><!-- raw HTML omitted --><section role="doc-endnotes">
<hr>
<ol>
<li id="fn:1" role="doc-endnote">
<p>This stackoverflow thread is from 2012. It’s included here not as a criticism of the participants, but as a criticism of the ideas that were current at the time. The post that states that the twelve-factor authors “clearly don’t understand the use of configuration files in .NET” has been awarded a “bounty” of 50 points! <a href="#fnref:1" role="doc-backlink">↩︎</a></p>
</li>
<li id="fn:2" role="doc-endnote">
<p><a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">https://en.wikipedia.org/wiki/Create,_read,_update_and_delete</a> <a href="#fnref:2" role="doc-backlink">↩︎</a></p>
</li>
<li id="fn:3" role="doc-endnote">
<p>A quirk of Azure DevOps is that <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&amp;tabs=yaml%2Cbatch#access-variables-through-the-environment">variable names are upper-cased when being exposed as environment variables</a>. <a href="#fnref:3" role="doc-backlink">↩︎</a></p>
</li>
<li id="fn:4" role="doc-endnote">
<p>In the interest of brevity, this example uses the <a href="https://docs.microsoft.com/en-us/cli/azure/">Azure CLI</a> to create all the infrastructure. On a real project I might use a different tool to do this, but the variable templates would be the same, as would the other parts of the pipeline. <a href="#fnref:4" role="doc-backlink">↩︎</a></p>
</li>
</ol>
</section>