4

我正在尝试根据我发布到的每个环境对我的 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:

在此处输入图像描述

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

4

1 回答 1

5

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

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

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

  • 删除了<DependendUpon>Web.config</DependentUpon>线条。这会将您的所有内容转储web.configs到根目录。

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

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

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

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

    在此处输入图像描述

测试结果:

在此处输入图像描述

于 2020-12-17T09:51:23.763 回答