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

8

我试图根据我发布到的每个环境,在Web.config文件上执行文件转换。大部分情况下,一切看起来都很好,直到我在发布管道上部署到我的UAT阶段。

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

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.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变换XML变量替换复选框。

部署完成后,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://learn.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 文件:

enter image description here

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


1
你尝试过禁用XML变量替换,只启用XML转换吗? - Mauricio Atanache
1
不,但我还需要变量替换,因为我在其中有特定于环境的变量 @MauricioAtanache - Hussam Ahmed
1
你可以尝试禁用它来证明这是否是问题。 - Mauricio Atanache
1
@MauricioAtanache 我取消了变量替换选项,但这并没有解决转换问题。 - Hussam Ahmed
1
问题解决了吗?如果没有,您能否告诉我关于这个问题的最新信息? - Leo Liu
1个回答

7

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

起初我也遇到了这个问题。在修改以下注释后,我成功地在Azure Pipelines上转换了文件。您可以检查一下是否有帮助:

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

  • 删除<DependendUpon>Web.config</DependentUpon>行。这会将所有web.configs转储到根目录。

  • 在构建期间禁用配置转换,在构建任务的MSBuild Arguments部分添加参数/p:TransformWebConfigEnabled=False。如果您想在发布期间更新连接字符串,请同时添加/p:AutoParameterizationWebConfigConnectionStrings=False

    注意:您可以检查构建流水线的工件文件,以查看包.zip是否包含文件Web.UAT.config

  • 为任务文件转换设置正确的转换规则

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

    enter image description here

测试结果:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接