Azure DevOps

2. Build Pipeline with Azure DevOps – AppSettings.json Transformations

This is the second article in the series of Build Pipeline with Azure DevOps. Other articles in this series are:

1. Build Pipeline with Azure DevOps – The Basics
2. Build Pipeline with Azure DevOps – AppSettings.json Transformations
3. Build Pipeline with Azure DevOps – Generate EF Core Migration Script
4. Build Pipeline with Azure DevOps - Publish and Bundle Artifacts

The next step in this series is to look at appsettings transformations. It is bad practice to commit sensitive information such as production database connection strings and other secret keys to source control. We can have placeholders in our appsettings.json file and then replace them with the actual values during the build process.

Let’s have a look at updating our build pipeline and see how transformations work.

This is an example of our appsettings.json file, notice we have ConnectionString, I have added ReleaseVersion there to put the BuildNumber in there so we know what build is deployed to a given environment. This will be fetched by front end client via API call obviously.

  "ConnectionStrings": {
    "DefaultConnection": "place_holder_for_actual_connstr"
  "AllowedHosts": "*",
  "ReleaseVersion":  "place_holder"

Let’s go to our pipeline, click Edit and add a new step for transforamtion:

Find File Transform Task in the Task Pane on the right and click on it.

Set Package or folder to be $(System.DefaultWorkingDirectory), (more on these built-in/pre-defined variables here) File format to be JSON and Target files to ‘appsettings.json. Then put the cursor on the line where we want to add this task in the azure-pipeline.yml file and click Add.

The FileTransform@1 task will be added, we want to add this task before the build so all necessary transformations are done before the build and publish.

Now all we have to do is define the variables with same names as the keys in our appsettings.json. These variable values will automatically be taken and replaced with appsettings.json values where the key matches. We have ConnectionStrings > DefaultConnection in a hierarchical order. To represent such hierarchy we simply put a dot . between the names. Let’s look at the variables. Click on the Variables button beside Save button on the top right and click New variable.

The New variable window is pretty much self explanatory. Remember to check the ‘Keep this value secret’ checkbox if the variable value consists sensitive information. We can also define our other build pipeline variables here and then use them in the yml file. But watch out if your appsettings.json file contains a key which matches the variable name here, it’s value will be replaced accidentally.

That’s about it, now when we run this pipeline the values in the appsettings.json will be replaced by the values defined in the variables.

See how on line 11 it does not reveal the value of connection string, that’s because we checked the checked the ‘Keep this value secret’ checkbox.

Next let’s look at Generate Entity Framework Core Migration Script.

Leave a Reply

Your email address will not be published. Required fields are marked *