Update appsettings.json at deploy time with VSTS Release Management

Lately I have to deal more with .NET Core Web Applications to setup build and release definitions in VSTS. What always comes up is how to deal with specific application settings which must me updated for a specific environment.

I have always been a big advocate of making a clear separation between build and release. The build should simply generate a generic package while the release should pick up the package and deploy it to any possible environment. At deployment time the specific enviroment values should be injected. Web Deploy has been the obvious tool in the past to make this happen with the capability to update the generated setparameters.xml file in a deployment action which injected the environment values into the web.config file.

Now with .NET Core and the typical appsettings.json file, it has become really easy in VSTS to inject custom values into the appsettings.json file.

Example of appsettings.json file in my solution:

AppSettings

Imagine that you would want to replace the values for the different settings (as from line 8). Note that it will also be possible to replace the values in the “Administrators” array.

First, you will need to create a build in VSTS which produces the deployment zip package (via dotnet publish command).

BuildOutput

The VSTS release definition will link to the build output and you can use the built-in release task “Azure App Service Deploy” to deploy the build output to an Azure App Service.

ReleaseTask

The “File Transformation” section in the release task offers the possibility to define the JSON variable substitution. You will need to provide the file name from the root and the environment values (pay attention to the format of the variable names) can be set for the “DEV” environment.

Variables

Doing a file lookup from the console in the Azure Portal after deployment shows the result of the appsettings file.

image

Simple solutions are always the best solutions!

One Response to Update appsettings.json at deploy time with VSTS Release Management

  1. Sindre Røbekk Hagerup says:

    It’s worth mentioning that you may have to use a search expression for the json file, as the file may not always be on the root (depending on the structure of the zip file).

    I had to use this: **/appsettings.json
    before it was able to find the json file.

Leave a comment