在发布管道的第一阶段更改所有 Web.*.config 文件

Change all Web.*.config files in first stage in release pipeline

我需要在第一步更改所有 Web.*.config 文件中的 appsettings 中的项目。那就是我不能在发布管道的每一步都进行转换。原因是我用的是EpiserverDXC/DXP。

我有4个阶段; "Upload Package"、"Integration"、"Preproduction""Production"

值存储在 Azure Key Vault 中。

有什么聪明的方法可以做到这一点吗?

如果File transformation不适合您的项目,使用powershell脚本进行项目更改怎么样?

示例:

这是我的例子web.product.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="service.tasks" type="HRNetTaskService.TaskConfigurationSection, TaskService" />
  </configSections>
  <connectionStrings>
    <add name="Production" connectionString="xxxx" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="RestServiceUrl" value="https://sample.net" />   
  </appSettings> 
</configuration>

现在我想更新 .config 文件的 connectionString。使用以下脚本将 replace.ps1 添加到 repos 中,然后通过传递相应的动态值在 Powershell 任务中调用此 replace.ps1 文件:

Param(
[string]$source,
[string]$connectionstring
)
$webConfig = $source
$doc = (Get-Content $webConfig) -as [Xml]
$root = $doc.get_DocumentElement();
$activeConnection = $root.connectionStrings.SelectNodes("add");
$activeConnection.SetAttribute("connectionString", $connectionstring);
$doc.Save($webConfig)


这里 $(ProductValue) 是你在 Azure 密钥保管库中配置的变量。其调用方式与管道变量相同。只需要 link Azure key vault 到 azure devops 中,然后将它与 Variable group 结合起来。

你读过guide on config transforms for DXC? https://world.episerver.com/digital-experience-cloud-service/development-considerations/environment-configurations/

我试图做的是在转换配置文件之前从 Azure Key Vault 中替换配置文件中的变量,因为在使用 Episerver DXC 时无法在发布管道期间(此时)完成。我所做的是在构建管道期间替换它们。

在构建管道期间在 Powershell 中进行了变量替换。在 Powershell 任务之前将 Key Vault 机密作为单独的任务导入,列出我将在 Powershell 任务中用作环境变量的所有机密。

我命名的环境变量与它应该在配置文件中替换的变量相同(例如 SomeApiKey_Integration)。浏览配置文件,查找两个双下划线之间的任何内容,并将它们替换为环境变量 ((Get-ChildItem $variable).Value) 中的值。

在配置文件和环境变量中,它们的命名方式如前所述,SomeApiKey_Integration。 Key Vault 名称和环境变量值作为 SomeApiKey-Integration。