5

While deploying a build to multiple environments like UAT, Production. I want to replace one file config.uat.json or config.prod.json with config.json. Is there any option available? just like we have XML Transformation.

I am aware of Json Variable substitution but that doesn't serve my purpose as the variable list is long (almost 50 entries)

Thanks in Advance!

Saurabh Dhingra
  • 109
  • 1
  • 1
  • 5

2 Answers2

2

JSON variable substitution should be a good option, if you don't want to add variables one by one on Variables tab, you can update them with VSTS REST API, or add a powershell script to set the variables.

Otherwise, you may need to delete the config.uat.json or config.prod.json, and copy the config.json to the target machine.

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
0

Inside your Program.cs file, you can get an environment variable which will represent your current environment:

var builder = WebHost.CreateDefaultBuilder(args);
var currentEnv = builder.GetSetting("environnement");

using this currentEnv value, you will be able to load the file config.{currentEnv}.json

builder.AddJsonFile($"config.{currentEnv}.json", optional: false, reloadOnChange: true);

EDIT:

If you want to do this in powershell, you can make a transofmration of your configuration file with a default: appsettings.json containing keys, and appsettings.env.json containing overriding.

To transform your configuration, you can do something like this:

Param(
   [Parameter(Mandatory=$true)][string]$SpecificConfig
)
$defaultConfig = "AppSettings.json";
$settingsContent = ConvertFrom-Json $defaultConfig;
$specificContent = ConvertFrom-Json $SpecificConfig;

# Do this on each <property> to override
if (![string]::IsNullOrEmpty($specificContent.<property>))
{
    $settingsContent.<property> = $specificContent.<property>;
}
Write-Host $settingsContent > $defaultConfig;
Didier Aupest
  • 3,227
  • 2
  • 23
  • 35
  • One of the solutions I thought of if quite similar. I was wondering if there is a way we can do json transformation just like xml transformation – Saurabh Dhingra Jul 24 '18 at 19:38
  • You can, but as far as I know, there is no built-in tool to do this properly. In powershell, you can load your AppSettings.json with ConvertFrom-Json $file. An Edit will be made to complete the answer. – Didier Aupest Jul 24 '18 at 20:16
  • Thanks, I will try replacing using powershell – Saurabh Dhingra Jul 25 '18 at 06:23