How to properly do file transforms on Azure Pipelines

20.9k views Asked by At

I am attempting to do file transforms on my Web.config depending on each environment I publish to. Mostly, everything looks fine until I deploy to my UAT stage on the release pipeline.

In my build pipeline, here is the YAML file that I am using:

# 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'

Here is the area of my Web.config that I want to transform with the file transformation:

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

Here is my 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>


The build pipelined works fine. In my release pipeline, I have a "Deploy IIS Website/App" task on the UAT stage and I have both the XML transforms and XML Variable Substitution checkboxes checked.

After I deploy, the UAT web.config is similar to the web.config on the DEV stage. I created another task, File transform task, to do the file transformation. That task shows succeeded, but the config file still isn't changed.

This is the part of the log on Azure that says that the transformations were successful:

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

I also went on the .csproj file the removed the tags and added the config files as , but still to no end.

Here is the .csproj:

enter image description here

How can I use file transforms properly to transform the addresses attribute on the tag?

1

There are 1 answers

0
Leo Liu On

How to properly do file transforms on Azure Pipelines

I could reproduce this issue on my side at first. After modifying the following notes, I can successfully transforms file on Azure Pipelines, you could check if it helps you:

  • Ensure your project that you are including the transform file, Web.UAT.config, and set the properties Build Action = "Content" Copy to Output Directory = "Copy Always"

  • Removed the <DependendUpon>Web.config</DependentUpon> lines. This dumps all your web.configs to the root.

  • Disable the config transform during the build, add argument /p:TransformWebConfigEnabled=False in MSBuild Arguments section of your Build task. Add also add /p:AutoParameterizationWebConfigConnectionStrings=False if you want to update the connection string during the release.

    Note: You could check the artifact file of the build pipeline to check if the package .zip contains file Web.UAT.config.

  • Set the correct Transformation rules for the task File transform:

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

    enter image description here

Test result:

enter image description here