如何在 VSTS CD 中的 release 中替换 json 文件?

Sau*_*gra 5 azure continuous-delivery azure-devops azure-pipelines azure-pipelines-release-pipeline

在将构建部署到 UAT、生产等多个环境时。我想用 config.json 替换一个文件 config.uat.json 或 config.prod.json。有没有可用的选择?就像我们有 XML 转换一样。

我知道 Json 变量替换,但这不符合我的目的,因为变量列表很长(近 50 个条目)

提前致谢!

Did*_*est 0

Program.cs文件中,您可以获得一个代表当前环境的环境变量:

var builder = WebHost.CreateDefaultBuilder(args);
var currentEnv = builder.GetSetting("environnement");
Run Code Online (Sandbox Code Playgroud)

使用这个currentEnv值,您将能够加载文件config.{currentEnv}.json

builder.AddJsonFile($"config.{currentEnv}.json", optional: false, reloadOnChange: true);
Run Code Online (Sandbox Code Playgroud)

编辑

如果您想在 powershell 中执行此操作,您可以使用默认值对配置文件进行转换:包含键的 appsettings.json 和包含覆盖的 appsettings.env.json。

要转换您的配置,您可以执行以下操作:

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;
Run Code Online (Sandbox Code Playgroud)