Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent msbuild config transforms in particular environments?

Tags:

msbuild

tfs

I have a azure webjob project that uses config transforms to create dev/test/release configuration. We are using TFS for CI/CD deployment to Azure. I want to have MSBuild apply the transforms for dev so we can debug locally. However, when we are building in TFS in the CI/CD pipeline I need to disable the config transforms during the build step.

TFS has an "apply XML transformations" checkbox in the release step, which is where we want the transforms applied since we have the environment variable set during release. Unfortunately, this is not working because the transforms are already applied during build so the release artifact only has the finished output file, not the separate transform files.

I have tried editing the .csproj file to disable the transforms. I assume the transforms are being performed by the following section of the project file:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="AfterCompile" Condition="Exists('App.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="App.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>
  <!--Override After Publish to support ClickOnce AfterPublish. Target replaces the untransformed config file copied to the deployment directory with the transformed one.-->
  <Target Name="AfterPublish">
    <PropertyGroup>
      <DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig>
    </PropertyGroup>
    <!--Publish copies the untransformed App.config to deployment directory so overwrite it-->
    <Copy Condition="Exists('$(DeployedConfig)')" SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config" DestinationFiles="$(DeployedConfig)" />
  </Target>

I tried adding conditions like "$(Configuration)|$(Platform)' == 'Debug|AnyCPU'" to these sections, and it did not help (the transforms still got applied in all three environments). I even commented this section out completely, and I still got the transforms. This leaves me with three questions:

  1. How can the config transforms be disabled?
  2. How can I conditionally disable them so they are still applied when debugging in VS?
  3. Is this the correct approach, or is there a better way to get the correct transform applied when using CI/CD in TFS 2017?
like image 266
emaia Avatar asked Jan 24 '18 06:01

emaia


People also ask

What is Xdt transform SetAttributes?

The xdt:Transform attribute value "SetAttributes" indicates that the purpose of this transform is to change attribute values of an existing element in the Web.

How does web config transform work?

Switching configuration based on configuration is a perfect use of transformations. Web. config transformations are implemented using a markup language called XML Document Transform - XDT for short. XDT is an XML-based document format invented by Microsoft and used to describe changes to a Web.

Why is add config transform greyed out?

You need to create a configuration in a given project before it can use that transform. Either check "Create new project configurations" to create the configuration in all projects at once, or do it individually like @paulv7260 did.


1 Answers

To disable the config transform during the build, you just need to add argument /p:TransformWebConfigEnabled=False in MSBuild Arguments section of your Build task. You also need to add /p:AutoParameterizationWebConfigConnectionStrings=False if you want to update the connection string during the release. enter image description here

Besides, you need to update your project file so that the Web.XXX.Config file will be included in the package if you are generating msdeploy package for deployment.

  1. Change the "Build Action" of the config file from "None" to "Content".
  2. Unload your project file and remove <DependentUpon> tag for the config file. enter image description here
like image 176
Eddie Chen - MSFT Avatar answered Oct 13 '22 18:10

Eddie Chen - MSFT