Web.config Transformation Using TeamCity explained

Usually Web.config Transformation is being done during a deployment of dotnet application to various server during deployment time. Here Teamcity is a CI server thus deployment can be done done using WebDAV or Octopus Deploy and integration with Teamcity. Octopus Deploy has in build integration with Teamcity and Web.config Transformation feature.

However, Sometimes you would like to do Web.config Transformation using teamcity as well thus this article might help you understand a scnario.

Before using Web.config Transformation in Temacity, Lets understand a Web.config Transformation basics first using following url.

Now let me give you some more background about Web.config Transformation.

Web.config files typically include settings that have to be different depending on which environment the application is running in. For example, you might have to make changes such as the following when you deploy a Web.config file to a destination server:

  • Change a database connection string to point to the production database.
  • Disable debugging in the production environment.
  • Remove sensitive information such as connection strings, such as if you provide a package to a community site.

To manually manage changes to a Web.config file, you can do the following:

  • Edit the Web.config file on the destination server each time the project is deployed.
  • Maintain separate versions of the Web.config file for each environment (perhaps in separate folders or using distinctive names), and copy only the version that is appropriate to the destination environment when you deploy.
  • Create XSLT files to transform the Web.config and apply the transforms when the application is deployed.

You may read more about this at
https://docs.microsoft.com/en-us/previous-versions/dd394698(v=vs.100)?redirectedfrom=MSDN#webconfig_transformation

Now, There are following methods in which you may call Web.config Transformation using TeamCity

  • .NET CLI (dotnet)
  • .NET Process Runner
  • Command Line
  • MSBuild
  • MSpec
  • NuGet Installer
  • PowerShell
  • Visual Studio (sln)
  • Visual Studio Tests

Condition and Checklist to verify for web.config transformation to work on build server

Condition 1 – The MSBuild script should be like this

MSBuild.exe PATH_TO_PROJ.csproj /p:DeployOnBuild=true /p:PublishProfile=PUBLISH_TARGET /p:Configuration=Release

Still needed to specify the Configuration as Debug or Release (even though this appears to be an attribute of the publish target.)

Condition 2 – Needed parameters in Teammcity:

system.PublishProfile – set to the value of your PublishProfile (such as Release)
system.DeployOnBuild – set to true

When this is done TeamCity will automatically pass these parameters to your Visual Studio build step and transformation now works.

Method 1 – csproj file changes
If you add the following xml to the bottom of the .csproj file for your web application, you’ll ensure that the config transformation occurs before every build:

Method 2 – csproj file changes

Method 3 – Apply a web.config transform within a Teamcity Build Step
One thing that seems to work (not sure if its the best way) is to add a before/after build event on the csproj file for the website e.g.

Source Reference –

  • https://stackoverflow.com/questions/10506308/web-config-file-transform-from-command-line/10506476#10506476
  • https://stackoverflow.com/questions/39569396/apply-a-web-config-transform-within-a-teamcity-build-step

Change value or transform in app.config within TeamCity

  • Using PowerShell script
  • Using XDT
  • File Content Replacer build feature
  • Nuget Package “SlowCheetah”

You may find a stackoverflow link for here –

  • https://stackoverflow.com/questions/37201731/change-value-in-app-config-within-teamcity
  • https://github.com/devopsschool-sample-projects/teamcity-config-transform
  • https://stackoverflow.com/questions/37201731/change-value-in-app-config-within-teamcity

Automatic Deployment From TeamCity Using WebDeploy

  • http://www.mikevalenty.com/automatic-deployment-from-teamcity-using-webdeploy/

Perform web.config transforms on build using MSBuild

  • http://danielsaidi.com/blog/2013/04/04/perform-webconfig-transforms-on-build

How to inject Teamcity’s build parameters into web.config

  • https://teamcity-support.jetbrains.com/hc/en-us/community/posts/206833975-How-to-inject-Teamcity-s-build-parameters-into-web-config

Using a MSBuild Script in TeamCity

  • https://cgeers.wordpress.com/2011/06/04/using-a-msbuild-script-in-teamcity/

Web.config Transformation Using TeamCity

  • https://cgeers.wordpress.com/2011/08/21/web-config-transformation-using-teamcity/

Running Unit Tests With TeamCity

  • https://cgeers.wordpress.com/2011/05/28/running-unit-tests-with-teamcity/
Rajesh Kumar
Follow me