【问题标题】:How to properly do file transforms on Azure Pipelines如何在 Azure Pipelines 上正确进行文件转换
【发布时间】:2021-03-26 04:35:57
【问题描述】:

我正在尝试根据我发布到的每个环境对我的 Web.config 进行文件转换。大多数情况下,在我部署到发布管道上的 UAT 阶段之前,一切看起来都很好。

在我的构建管道中,这是我正在使用的 YAML 文件:

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

pool:
  vmImage: 'windows-latest'

variables:
  solution: 'SubmissionService.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1
  displayName: 'Install Newer Version on NuGet'
  inputs:
    checkLatest: true

- task: NuGetCommand@2
  displayName: 'Restore NuGet Packages for Solution'
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: config
    nugetConfigPath: ./Nuget.config
    
- task: VSBuild@1
  displayName: 'Create Artifact For Solution'
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.ArtifactStagingDirectory)" /p:TransformWebConfigEnabled=false /p:AutoParameterizationWebConfigConnectionStrings=false /p:MarkWebConfigAssistFilesAsExclude=false /p:ProfileTransformWebConfigEnabled=false /p:IsTransformWebConfigDisabled=true'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifacts'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

这是我的Web.config 的区域,我想通过文件转换进行转换:

 <system.serviceModel>
    <client>
      <endpoint address="Address_1" name="BasicHttpBinding_1"/>
      <endpoint address="Address_2" name="BasicHttpBinding_2" />
    </client>
  </system.serviceModel>

这是我的Web.UAT.config

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
  <system.serviceModel>
    <client>
      <endpoint xdt:Transform="Replace" xdt:Locator="Match(name)" address="Address_3" name="BasicHttpBinding_1" />
      <endpoint xdt:Transform="Replace" xdt:Locator="Match(name)" address="Address_4" name="BasicHttpBinding_2" />
    </client>
  </system.serviceModel>
</configuration>


流水线化的构建工作正常。在我的发布管道中,我在 UAT 阶段有一个“部署 IIS 网站/应用程序”任务,并且我同时选中了 XML transformsXML Variable Substitution 复选框。

在我部署之后,UAT web.config 类似于 DEV 阶段的 web.config。我创建了另一个任务文件转换任务来进行文件转换。该任务显示成功,但配置文件仍未更改。

这是 Azure 上显示转换成功的日志部分:

2020-12-15T16:23:37.1236261Z ==============================================================================
2020-12-15T16:23:37.1236588Z Task         : IIS web app deploy
2020-12-15T16:23:37.1236857Z Description  : Deploy a website or web application using Web Deploy
2020-12-15T16:23:37.1237081Z Version      : 0.178.0
2020-12-15T16:23:37.1237278Z Author       : Microsoft Corporation
2020-12-15T16:23:37.1237595Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/iis-web-app-deployment-on-machine-group
2020-12-15T16:23:37.1237965Z ==============================================================================
2020-12-15T16:23:56.5036495Z ConnectionString attributes in Web.config is parameterized by default. Note that the transformation has no effect on connectionString attributes as the value is overridden during deployment by 'Parameters.xml or 'SetParameters.xml' files. You can disable the auto-parameterization by setting /p:AutoParameterizationWebConfigConnectionStrings=False during MSBuild package generation.
2020-12-15T16:23:56.5141446Z [command]C:\agent\_work\_tasks\IISWebAppDeploymentOnMachineGroup_1b467810-6725-4b6d-accd-886174c09bba\0.178.0\ctt\ctt.exe s:C:\agent\_work\_temp\temp_web_package_2874722683939497\Content\D_C\a\1\s\SubmissionService\obj\Release\Package\PackageTmp\Web.config t:C:\agent\_work\_temp\temp_web_package_2874722683939497\Content\D_C\a\1\s\SubmissionService\obj\Release\Package\PackageTmp\Web.UAT.config d:C:\agent\_work\_temp\temp_web_package_2874722683939497\Content\D_C\a\1\s\SubmissionService\obj\Release\Package\PackageTmp\Web.config pw i verbose
2020-12-15T16:23:58.6903744Z XML Transformations applied successfully

我还继续 .csproj 文件删除了标签并将配置文件添加为 ,但仍然没有结束。

这是.csproj:

如何正确使用文件转换来转换标签上的地址属性?

【问题讨论】:

  • 您是否尝试过禁用 XML 变量替换并仅启用 XML 转换?
  • 不,但我也需要变量替换,因为我有环境特定变量@MauricioAtanache
  • 您可以尝试禁用它来证明这是否是问题所在。
  • @MauricioAtanache 我取消选中了变量替换选项,但这也没有解决转换问题
  • 这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?

标签: azure-pipelines azure-pipelines-release-pipeline azure-pipelines-build-task azure-pipelines-tasks azure-pipelines-release-task


【解决方案1】:

如何在 Azure Pipelines 上正确进行文件转换

我一开始可以在我这边重现这个问题。修改以下注释后,我可以在 Azure Pipelines 上成功转换文件,您可以检查它是否对您有帮助:

  • 确保您的项目包含转换文件 Web.UAT.config,并设置属性 Build Action = "Content" Copy to Output Directory = "Copy Always"

  • 删除了&lt;DependendUpon&gt;Web.config&lt;/DependentUpon&gt; 行。这会将您所有的web.configs 转储到根目录。

  • 在构建过程中禁用配置转换,在构建任务的 MSBuild Arguments 部分添加参数 /p:TransformWebConfigEnabled=False。如果要在发布期间更新连接字符串,也可以添加/p:AutoParameterizationWebConfigConnectionStrings=False

    注意:您可以检查构建管道的工件文件以检查包.zip是否包含文件Web.UAT.config

  • 为任务File transform设置正确的Transformation rules

    -transform **\*.UAT.config -xml **\*.config

测试结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多