How to Publish Web with msbuild? How to Publish Web with msbuild? asp.net asp.net

How to Publish Web with msbuild?


I got it mostly working without a custom msbuild script. Here are the relevant TeamCity build configuration settings:

Artifact paths: %system.teamcity.build.workingDir%\MyProject\obj\Debug\Package\PackageTmp Type of runner: MSBuild (Runner for MSBuild files) Build file path: MyProject\MyProject.csproj Working directory: same as checkout directory MSBuild version: Microsoft .NET Framework 4.0 MSBuild ToolsVersion: 4.0 Run platform: x86 Targets: Package Command line parameters to MSBuild.exe: /p:Configuration=Debug

This will compile, package (with web.config transformation), and save the output as artifacts. The only thing missing is copying the output to a specified location, but that could be done either in another TeamCity build configuration with an artifact dependency or with an msbuild script.

Update

Here is an msbuild script that will compile, package (with web.config transformation), and copy the output to my staging server

<?xml version="1.0" encoding="utf-8" ?><Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">    <PropertyGroup>        <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>        <SolutionName>MySolution</SolutionName>        <SolutionFile>$(SolutionName).sln</SolutionFile>        <ProjectName>MyProject</ProjectName>        <ProjectFile>$(ProjectName)\$(ProjectName).csproj</ProjectFile>    </PropertyGroup>    <Target Name="Build" DependsOnTargets="BuildPackage;CopyOutput" />    <Target Name="BuildPackage">        <MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration)" />        <MSBuild Projects="$(ProjectFile)" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration)" />    </Target>    <Target Name="CopyOutput">        <ItemGroup>            <PackagedFiles Include="$(ProjectName)\obj\$(Configuration)\Package\PackageTmp\**\*.*"/>        </ItemGroup>        <Copy SourceFiles="@(PackagedFiles)" DestinationFiles="@(PackagedFiles->'\\build02\wwwroot\$(ProjectName)\$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/>    </Target></Project>

You can also remove the SolutionName and ProjectName properties from the PropertyGroup tag and pass them to msbuild.

msbuild build.xml /p:Configuration=Deploy;SolutionName=MySolution;ProjectName=MyProject

Update 2

Since this question still gets a good deal of traffic, I thought it was worth updating my answer with my current script that uses Web Deploy (also known as MSDeploy).

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">  <PropertyGroup>    <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>    <ProjectFile Condition=" '$(ProjectFile)' == '' ">$(ProjectName)\$(ProjectName).csproj</ProjectFile>    <DeployServiceUrl Condition=" '$(DeployServiceUrl)' == '' ">http://staging-server/MSDeployAgentService</DeployServiceUrl>  </PropertyGroup>  <Target Name="VerifyProperties">    <!-- Verify that we have values for all required properties -->    <Error Condition=" '$(ProjectName)' == '' " Text="ProjectName is required." />  </Target>  <Target Name="Build" DependsOnTargets="VerifyProperties">    <!-- Deploy using windows authentication -->    <MSBuild Projects="$(ProjectFile)"             Properties="Configuration=$(Configuration);                             MvcBuildViews=False;                             DeployOnBuild=true;                             DeployTarget=MSDeployPublish;                             CreatePackageOnPublish=True;                             AllowUntrustedCertificate=True;                             MSDeployPublishMethod=RemoteAgent;                             MsDeployServiceUrl=$(DeployServiceUrl);                             SkipExtraFilesOnServer=True;                             UserName=;                             Password=;"             ContinueOnError="false" />  </Target></Project>

In TeamCity, I have parameters named env.Configuration, env.ProjectName and env.DeployServiceUrl. The MSBuild runner has the build file path and the parameters are passed automagically (you don't have to specify them in Command line parameters).

You can also run it from the command line:

msbuild build.xml /p:Configuration=Staging;ProjectName=MyProject;DeployServiceUrl=http://staging-server/MSDeployAgentService


Using the deployment profiles introduced in VS 2012, you can publish with the following command line:

msbuild MyProject.csproj /p:DeployOnBuild=true /p:PublishProfile=<profile-name> /p:Password=<insert-password> /p:VisualStudioVersion=11.0

For more information on the parameters see this.

The values for the /p:VisualStudioVersion parameter depend on your version of Visual Studio. Wikipedia has a table of Visual Studio releases and their versions.


I came up with such solution, works great for me:

msbuild /t:ResolveReferences;_WPPCopyWebApplication /p:BuildingProject=true;OutDir=C:\Temp\build\ Test.csproj

The secret sauce is _WPPCopyWebApplication target.