SeanKilleen
SeanKilleen

Reputation: 8977

How to I perform a web.config transform with MSBuild or MSDeploy?

I've tried a number of different configurations with this and I haven't achieved my result.

TL;DR

I'm trying to add config transforms into my build process and am looking for the right way to do it from MSBuild so that it shows up in my deployments via MSDeploy.

Background

Goal

I would like to add Web.Config transforms to this process. I figured I would do something simple at first, like an app setting called "PEAppsEnvironmentName", which I would make Dev, Test, or Prod based on the current environment.

Theory So Far

To me, it appears that when packaging with MSDeploy, I'm not transforming the config file.

When I run MSBuild with the DeployOnBuild option set to true, it creates another package that has the appropriately transformed config. It just seems like somehow I can't get it all to match up. The end result is that the web page displays "None" (the initial setting) instead of the transformed "Development" string.

I think if I could find out how to use MSDeploy during the packaging phase to transform the MSConfig, I'd be good to go.

Code

My web.config file

<appSettings>
      <add key ="PEAppsEnvironmentName" value="None"/>
      ...  
</appSettings>

My Web.Dev.config file

<appSettings>
    <add key ="PEAppsEnvironmentName" xdt:Transform="Replace" xdt:Locator="Match(key)" value="Development" />
</appSettings>

My MSBuild Targets

Property group showing default config is "Dev"

<PropertyGroup>
    <Configuration Condition="'$(Configuration)' == ''">Dev</Configuration>
</PropertyGroup>

My MSBuild "Compile" Target

   <Target Name="Compile" DependsOnTargets="Init">
        <MSBuild Projects="@(SolutionFile)" Targets="Rebuild" Properties="OutDir=%(BuildArtifacts.FullPath);DeployOnBuild=True"/>
    </Target>

My MSBuild "Package" Target

<Target Name="Package" DependsOnTargets="Compile;Test">
    <PropertyGroup>
        <PackageDir>%(PackageFile.RootDir)%(PackageFile.Directory)</PackageDir>
        <Source>%(WebSite.FullPath)</Source>
        <Destination>%(PackageFile.FullPath)</Destination>
    </PropertyGroup>
    <MakeDir Directories="$(PackageDir)"/>
    <Exec Command='"@(MSDeploy)" -verb:sync -source:iisApp="$(Source)" -dest:package="$(Destination)" '/>
</Target>

My MSBuild "Deploy" Target

(scrubbed for PWs, etc.)

   <Target Name='Deploy' DependsOnTargets='Package'>
        <PropertyGroup>
            <Source>%(PackageFile.FullPath)</Source>
        </PropertyGroup>
        <Exec Command ='"@(MsDeploy)" -verb:sync -source:package="$(Source)" -dest:iisApp=PEApps,computerName=$(WebServerName),username=[User],password=[Password]'/>
    </Target>

Upvotes: 2

Views: 3428

Answers (1)

Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi

Reputation: 44312

There was a lot to this question, I'm not sure if I'm fully on the same page as you but I'll summarize my impression of what you are asking. You have an existing web project which is in a solution with other projects. You need to be able to package the web project so that you can publish it to multiple destinations.

I have created a NuGet package which can be used for this exact purpose. It's called package-web. When you add it to your web project it will update the packaging process. When you create a package a few additional files will be included in the package, including all the web.config transform files. A .ps1 file will be created next to the package as well. You can use this script to publish the package. It will prompt you for which transform to run and for all the Web Deploy parameters. You can also save the responses to a file and then just pass them to the .ps1 file so that you can perform non-interactive publishes. I created a 5 minute video on it at http://nuget.org/packages/PackageWeb package web: http://sedodream.com/2012/03/14/PackageWebUpdatedAndVideoBelow.aspx. FYI this is not yet working with VS 2012 but I'm working on the fix and should have it updated by the time VS 2012 is released.

If you don't find that useful you can see how I implemented the solution at https://github.com/sayedihashimi/package-web and you should see examples of everything that you need to do to roll your own.

FYI if you need to transform any files besides web.config on package create then you should take a look at my VS extension SlowCheetah. Here is a blog about how to integrate it into a build server.

Upvotes: 1

Related Questions