虽然替换令牌方法在 Azure devops 管道中非常方便易用,但正如您所提到的,它给其他开发人员的工作(尤其是本地开发)带来了很多麻烦。
为什么不考虑使用File transformation 任务来完成这项转换工作?这个任务有一个特性变量替换可以让你避免对配置文件的任何格式更改。只需要定义相应的变量,这些变量将替换到配置文件中。
让我举一个例子来解释一下,下面是一个简单的web.config文件示例:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="apiConfig" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<apiConfig>
<add key="ClientBasetUrl" value="http://localhost:4200" />
</apiConfig>
<system.web>
<compilation debug="true" targetFramework="4.6.2">
<assemblies>
<add assembly="System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.6.1" maxRequestLength="20480" />
</system.web>
</configuration>
现在我需要将 ClientBasetUrl 值:http://localhost:4200 替换为 http://localhost:8080。
1) 既然你关心的是将 Azure Key Vault 与 Asp.net Web 应用程序结合起来,那就去创建一个秘密ClientBasetUrl 到 Azure key Vault 中,它的值为http://localhost:8080。
2) 将 Azure 密钥保管库连接到 azure devops 管道。
3) 这里是关键步骤:配置File Transform task。
steps:
- task: FileTransform@2
displayName: 'File Transform with Variable: '
inputs:
folderPath: '$(System.DefaultWorkingDirectory)'
xmlTargetFiles: MonoApp.config //Here put your config file name that relative to the root folder
然后您将看到此任务结束后替换成功完成。
您可以看到我不需要对我的配置文件进行任何语法更改,只需将相应的变量存储在 Azure 密钥库中并确保它们可以在管道运行期间下载。
另外,它可以让我在本地开发工作上非常顺利。