【问题标题】:How can I transform an XML config file with a build task in DevOps?如何在 DevOps 中使用构建任务转换 XML 配置文件?
【发布时间】:2021-02-08 16:56:18
【问题描述】:

在我的 .NET Framework 应用程序中,我需要在 ApplicationInsights.config 中更改 Application Insights InstrumentationKey,作为构建不同环境(Dev、UAT 等)的一部分。

在我的解决方案中,我有一个不包含 InstumentationKeyApplicationInsights.config 文件

我还有一个ApplicationInsights.Dev.config转换文件如下

<?xml version="1.0"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings"
                     xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <InstrumentationKey>89c95f9d-0f89-41cb-938b-175499e3052a</InstrumentationKey>
</ApplicationInsights>

在我的 YAML 管道中,我有以下步骤:

        - task: FileTransform@1
          inputs:
            folderPath: 'path/to/directory' 
            enableXmlTransform: true
            xmlTransformationRules: '-transform ApplicationInsights.Dev.config -xml ApplicationInsights.config'

构建任务完成,但是当我查看工作目录中生成的 ApplicationInsight.config 文件时,InstrumentationKey 不存在。

谁能看到我在这里做错了什么?

【问题讨论】:

  • 这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?

标签: azure-devops azure-application-insights


【解决方案1】:

我需要更改 ApplicationInsights.config 中的 Application Insights InstrumentationKey,作为针对不同环境的构建的一部分

要转换 XML 配置文件,您需要在 ApplicationInsights.Dev.config 文件中指定转换属性语法。

由于你的ApplicationInsights.config文件不包含InstumentationKey,你应该使用变换属性Insert,xdt:Transform="Insert"

<InstrumentationKey xdt:Transform="Insert">89c95f9d-0f89-41cb-938b-175499e3052a</InstrumentationKey>

更多详情请查看文档Transformation Syntax

ApplicationInsights.Dev.config 应该是:

<?xml version="1.0"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings"
                     xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <InstrumentationKey xdt:Transform="Insert">89c95f9d-0f89-41cb-938b-175499e3052a</InstrumentationKey>
</ApplicationInsights>

测试结果:

【讨论】:

    【解决方案2】:

    如果你想替换 InstrumentationKey 你的 ApplicationInsight.Config 应该是:

    <?xml version="1.0"?>
    <ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings"
                         xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <InstrumentationKey>89c95f9d-0f89-41cb-938b-175499e3052a</InstrumentationKey>
    </ApplicationInsights>
    

    还有你的转换配置ApplicationInsights.Dev.config:

    <?xml version="1.0"?>
    <!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
    <!-- In case configuration is not the root element, replace it with root element in source configuration file -->
    <ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <InstrumentationKey xdt:Transform="Replace">
            Value for dev
        </InstrumentationKey>
    </ApplicationInsights>
    

    然后对于这样的管道:

    trigger: none
    pr: none
    
    pool:
      vmImage: 'windows-latest'
    
    steps:
    - task: FileTransform@1
      inputs:
        folderPath: $(Build.SourcesDirectory)/stackoverflow/76-config-transform/
        enableXmlTransform: true
        xmlTransformationRules: '-transform ApplicationInsights.Dev.config -xml ApplicationInsights.config'
    - bash: cat ApplicationInsights.config
      workingDirectory: $(Build.SourcesDirectory)/stackoverflow/76-config-transform/
    

    你会得到

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2018-05-04
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      相关资源
      最近更新 更多