views:

490

answers:

3

I'm using the config file replacement feature of Visual Studio 2010's "Publish" functionality, as described in this article. I want to automate this using MSBuild/Hudson. Does anybody know how to do this?

I like how it works but if I cannot automate it I'll have to switch to XmlMassUpdate or similar.

+2  A: 

Edit your web project.csproj

under

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

Add -
<UseMsDeployExe>True</UseMsDeployExe>

Look at the Build output (make sure VS Tools - Options - Project & Solutions -Build & Run - MSBuild Output Verbosity - Detailed)

You should be able to see the msdeploy commands VS uses to produce the package. It's my understanding that VS actually uses Web Platform Pipeline API's and .target files to actually produce the deploy packages when building using MSBuild, and this command changes to use MsDeploy instead.

This stuff is so in need of documentation, its very frustrating.

Lukie
Thanks for the response. I added that line (except under my specific desired platform) and did a build but still don't see the command in my build output. I cranked things to verbose where I could see that the UseMsDeploy setting is being captured, but once past the `--- Publish started:` text the string `msbuild` doesn't appear (let alone msbuild.exe). :'-(
roufamatic
+3  A: 

Explanation

To transform your config file you'll have to execute the TransformWebConfig target.

This target takes two files Web.config and Web.$(Configuration).config and generates a Web.config. The generated file is the transformed version of the original one for the current configuration.

This file is generated in folder : obj\$(Configuration)\TransformWebConfig

Usage

You don't really explain what you want to achieve, so here a basic usage, a job that generates a transformed config file in a given folder.

Add the following piece in the end of your project file *.csproj after the import of Microsoft.WebApplication.targets

<PropertyGroup>
  <!-- Directory where your web.config will be copied -->
  <TransformedWebConfigDestination>$(MSBuildProjectDirectory)</TransformedWebConfigDestination>
</PropertyGroup>

<!--
  This target transforms the web.config based on current configuration and
  put the transformed files in $(TransformedWebConfigDestination) folder
-->
<Target Name="ConfigSubstitution">
  <CallTarget Targets="TransformWebConfig"/>

  <ItemGroup>
    <TransformedWebConfig Include="obj\$(Configuration)\TransformWebConfig\Web.config"/>
  </ItemGroup>

  <!-- Copy the transformed web.config to the configured destination -->
  <Copy SourceFiles="@(TransformedWebConfig)"
        DestinationFolder="$(TransformedWebConfigDestination)"/>
</Target>

In Hudson you could add a Build step in your build, or create a dedicated job configured as follow:

  • MsBuild Build File : Your csproj file.
  • Command Line Arguments : /t:ConfigSubstitution /p:Platform=AnyCpu;Configuration=Test;TransformedWebConfigDestination=DestinationFolder
madgnome
I'm sorry for being daft: So this feature is a part of MSBuild 4.0, meaning that version of MSBuild needs to be on the Hudson server, correct?
roufamatic
That's right. MSBuild should be installed on the server (I think you'll have to install Visual Studio, to have everything). And you need to configure hudson to use the right version of msbuild.
madgnome
For future searchers, it looks like you just need the .NET 4.0 Framework + a couple of extra vs2010 files to do 4.0 builds. http://www.scmgalaxy.com/release-updates/161-running-msbuild-40-and-msbuild-35-on-continuous-integration.html
roufamatic
Thank you, this does the trick!
roufamatic
One last question if you have a moment -- where did you find the reference for this target? A google search pulls up nada. I'm interested in automating other aspects of the publish tools, namely the simple "publish to a folder" feature.
roufamatic
You can look what happen during a step like Publish in Visual Studio Output View (Show output from Build). After that you'll search the various called targets in different file. Publish targets could be found in C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets
madgnome
+1  A: 

I am using this in Hudson to target Release:

/Property:Configuration=Release

The exact settings are:

Build
MSBuild Version: msbuild-4 (configured to point to v4 msbuild)
MsBuild Build File: project_name.sln
Command Line Arguments: /Property:Configuration=Release

You can test this in your project directory by running something similar (as your .NET framework version may differ) to this:

%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319\msbuild project.sln /Property:Configuration=Release

Cymen
Whoops, madgnome pretty much beat me to it. Leaving my response too as it was harder than expected to find this information so maybe this will help with the search indexing.
Cymen
Oh, and on a side note: If you add the Batch Tasks Plugin to Hudson you can setup msdeploy as a batch task to automatically push to staging and add a batch task that can be manually run to push staging to production with msdeploy. I'm still figuring out the database aspect (looking at Migrator.NET, etc). My script for that is: `msdeploy -verb:sync -source:dirPath='%CD%' -dest:dirPath='e:\some\dir\on\remote\system',computername=_STAGING_SERVER_,username=_USER_,password=_PASS_ -skip:objectName=dirPath,absolutePath=.*\.hg` (skipping .hg directories as using Mercurial for SCM).
Cymen