How to include my config transformation files in the web deploy zip?

9.2k views Asked by At

Im setting up a new build and deploy pipeline in Azure Devops. It is an older Web application with some transformation files for the web.config. In the old days we would build the same code x times depending on how many environments. This this is no longer necesary as I read from here https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/transforms-variable-substitution?view=vsts#xmltransform

it looks like the deploy pipeline can pick up the changes from my transform file.

But the problem is that my other transform files does not get included in the package so I get these warning message:

[warning]Unable to apply transformation for the given package. Verify the following.
[warning]1. Whether the Transformation is already applied for the MSBuild generated package during build. If yes, remove the <DependentUpon> tag for each config in the csproj file and rebuild. 
[warning]2. Ensure that the config file and transformation files are present in the same folder inside the package.

And yes when i download the artifact the Web.[stage].config files are not there as suggested.

Is there some setting somewhere that let me include these files? Or stop them from being transformed?

1

There are 1 answers

10
Josh Gust On

For Web applications

MSBuild knows how to transform the web.config files based on the following settings/properties/parameters (in order)

  • Build Configuration
    dotnet publish --configuration Release
  • Publish profile
    dotnet publish --configuration Release /p:PublishProfile=FolderProfile
  • Environment
    dotnet publish --configuration Release /p:EnvironmentName=Production
  • Custom file transform
    dotnet publish --configuration Release /p:CustomTransformFileName=custom.transform

I think it's typical for developers to make this happen based on build configuration only, and I believe MSBuild (and dotnet) know how to do this based on the <DependentUpon>Web.config</DependentUpon> element in the Web.[configuration].config item in the project or build script file.

Azure DevOps Release Pipelines is a little different. The pipeline wants to transform your web.config after the project has been built/published and doesn't know how to do that if MSBuild (or dotnet) has already made an attempt at it. Thus:

[warning]Unable to apply transformation for the given package. Verify the following.
[warning]1. Whether the Transformation is already applied for the MSBuild generated package during build. If yes, remove the tag for each config in the csproj file and rebuild.
[warning]2. Ensure that the config file and transformation files are present in the same folder inside the package.

The warning text states:

  1. Remove the <DependentUpon> tag for each config in the csproj
    • Thus: you need to remove the tag from the csproj to prevent MSBuild from transforming the files
    • Or: you need to use the /p:TransformWebConfigEnabled=False argument to MSBuild.
      (note: I believe it is correct that this can be used w/o removing the dependent upon tag, but I could be wrong)
  2. Make sure the transform source and target files are in the same folder inside the package.
    • There may be several ways to do this. I've chosen to mark the transform source config files as content to force MSBuild to include them in the published package.

Now you need to organize your release pipeline in accordance with the File Transforms and Value Substitutions documentation.

Stage Named According to Desired Transformation

[section]Starting: IIS Web App Deploy
====================================
Task : IIS Web App Deploy
Description : Deploy a website or web application using Web Deploy
Version : 0.0.51
Author : Microsoft Corporation
Help : More information
==================================== ...
[command]C:...\ctt\ctt.exe s:C:...\Web.config t:C:...\Web.Release.config d:C:...\Web.config pw i
[command]C:...\ctt\ctt.exe s:C:...\Web.config t:C:...\Web.Development.config d:C:...\Web.config pw i
XML Transformations applied successfully
...



For Non-Web Applications Needing .config Transformation

Getting your .config files to the release pipeline can happen several ways. Here are two.

  1. Your release should have "access" to the repository as a part of the artifact, which will ensure that the deploy agent downloads the source (not desirable IMHO).

  2. You will need to include the web.[stage].config files as part of your build artifact with a copy task, or a minimatch that picks them up.

Once you have the .config files available to the release pipeline

You can use the File Transform Task or XDT Transform Task to perform the transformation operations.


Option 2 is the route I've gone.

Here is an image of what that task looks like for me.

enter image description here

That task puts the config in the artifact from the build that I can then use in the release pipeline without rebuilding xx times.

enter image description here

Cleanup

If you're in a position where you care to not have the transform files persisting on the agent after the release is complete, then you'll need to add that to your pipeline. enter image description here