如何使用 Replace Tokens 在 web.config 中执行 XML 元素替换?

How to perform XML Element substitution in web.config using Replace Tokens?

我们有一个具有以下 system.net mailSettings 属性的 Web.config 文件:

  <system.net>
    <mailSettings>
      <smtp configSource="mailSettings.config" />
    </mailSettings>
  </system.net>

我们想用值为 <smtp from="reports@companyxyz.com" deliveryMethod="Network"> <network enableSsl="true" host="smtp.sendgrid.net" port="587" userName="apiKey" password="12346576fgb" /> </smtp>.

的管道变量替换 Web 配置中的这一行:<smtp configSource="mailSettings.config" />

此外,由于开发人员不想更改 Web 配置文件以适应替换令牌任务所需的前缀和后缀,我们希望使用自定义令牌前缀,例如:<mailSettings> 和后缀:</mailSettings>

steps:
- task: qetza.replacetokens.replacetokens-task.replacetokens@4
  displayName: 'Replace tokens in **/*.config'
  inputs:
    tokenPattern: custom
    tokenPrefix: '<mailSettings>'
    tokenSuffix: '</mailSettings>'

以上能实现吗? 我是否还要将此 Replace Tokens 任务放在 Azure App Deploy 任务之前?还是之后?

Can the above be accomplished?

是的。这个是可以实现的。

替换令牌任务使用管道变量设置 Web.config 文件中的值。

在您的例子中,变量名称是 <smtp configSource="mailSettings.config" />

并且在替换过程中,会同时替换前缀和后缀。

因此您可以尝试设置变量:

<smtp configSource="mailSettings.config" /> : <mailSettings> <smtp configSource="AA.config" /></mailSettings>

这是一个例子:

定义:

- task: qetza.replacetokens.replacetokens-task.replacetokens@3
  displayName: 'Replace tokens in web.config'
  inputs:
    rootDirectory: '$(build.sourcesdirectory)'
    targetFiles: web.config
    tokenPrefix: '<mailSettings>'
    tokenSuffix: '</mailSettings>'

结果:

Also do i place this Replace Tokens task before Azure App Deploy task? or after?

您需要在 Azure App Deploy 任务之前添加此任务。

然后更改将应用​​于 Azure App Deploy 任务。

更新:

正如Cataster在评论中所说:

Azure 应用服务任务中有两个关键点:

  1. 确保我的 Azure App Deploy Task packageForLinux 属性必须设置为 $(System.DefaultWorkingDirectory)/Build Artifact Name/ArtifactName

  2. 禁用 Azure App Deploy 任务中的 XML 转换选项 替换的令牌格式将与管道变量中的格式相同。但是如果我们启用这个选项,它将 format/transfom 变得整洁 XML