第一步,我需要在所有Web。*。config文件中的appsettings中更改项目。那就是我无法在发布管道的每个步骤中进行转换。原因是我使用Episerver DXC / DXP。
我有四个阶段; “ Upload Package”,“ Integration”,“ Preproduction”和“ Production”。
值存储在Azure Key Vault中。
是否有任何明智的方法?
EDIT:我想做的是在对配置文件进行转换之前,先从Azure Key Vault替换配置文件中的变量,因为在使用Episerver DXC的发布管道中,此操作无法完成(此时)。我所做的是改为在构建管道中替换它们。
在构建管道中在Powershell中进行变量替换。将Key Vault机密作为单独的任务导入Powershell任务之前,列出我将在Powershell任务中用作环境变量的所有密钥。
我命名的环境变量与应在配置文件(例如__SomeApiKey_Integration__)中替换的环境变量相同。浏览配置文件,在两个双下划线之间查找两个,然后将其替换为环境变量的值((Get-ChildItem $ variable).Value)。
在配置文件和环境变量中,它们的名称如前所述,__ SomeApiKey_Integration__。密钥库名称和环境变量值为SomeApiKey-Integration。
如果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>
现在我要更新connectionString
文件的.config
。使用以下脚本将replace.ps1
添加到存储库中,然后通过传递相应的动态值在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密钥保管库转换为天蓝色devops,然后将其与Variable group
结合即可。