asp.net - 如何使用msbuild发布网页?

标签 asp.net visual-studio-2010 msbuild teamcity msdeploy

Visual Studio 2010 有一个发布命令,允许您将 Web 应用程序项目发布到文件系统位置。我想在我的 TeamCity 构建服务器上执行此操作,因此我需要使用解决方案运行程序或 msbuild 来执行此操作。我尝试使用发布目标,但我认为这可能适用于 ClickOnce:

msbuild Project.csproj /t:Publish /p:Configuration=Deploy

我基本上想要完全按照 Web 部署项目的方式进行操作,但没有加载项。我需要它来编译 WAP,删除执行不需要的任何文件,执行任何 web.config transformations ,并将输出复制到指定位置。

我的解决方案,基于 Jeff Siver 的回答

<Target Name="Deploy">
    <MSBuild Projects="$(SolutionFile)" 
             Properties="Configuration=$(Configuration);DeployOnBuild=true;DeployTarget=Package" 
             ContinueOnError="false" />
    <Exec Command="&quot;$(ProjectPath)\obj\$(Configuration)\Package\$(ProjectName).deploy.cmd&quot; /y /m:$(DeployServer) -enableRule:DoNotDeleteRule" 
          ContinueOnError="false" />
</Target>

最佳答案

我大部分时间都在没有自定义 msbuild 脚本的情况下工作。以下是相关的 TeamCity 构建配置设置:

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>

您还可以从 PropertyGroup 标记中删除 SolutionName 和 ProjectName 属性并将它们传递给 msbuild。

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

更新2

由于这个问题仍然有大量流量,我认为值得用我当前使用 Web Deploy 的脚本更新我的答案。 (也称为 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>

在 TeamCity 中,我有名为 env.Configurationenv.ProjectNameenv.DeployServiceUrl 的参数。 MSBuild 运行程序具有构建文件路径,并且参数会自动传递(您不必在命令行参数中指定它们)。

您还可以从命令行运行它:

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

关于asp.net - 如何使用msbuild发布网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3097489/

相关文章:

c++ - 在 VS 2010 中创建文件/文件夹结构

msbuild - 我可以从我的 msbuild 脚本中取消 TeamCity 构建吗?

asp.net - Response.Write 在标记的特定部分

c# - WCF 服务如何与我的 winform 应用程序交互?

asp.net-mvc - MVC 3 引用自动更新到 MVC 4

python - 是否有适用于 Visual Studio 2010 的 Python 语法荧光笔?

.net - 使用 MSBuild 在 Server 2016 上构建面向 .Net 4.0 的 ASP.Net 项目

tfs - 将程序集版本号与 TFS Buildnumber 对齐

asp.net - 如何在 Azure 中启动 ASP.Net 状态服务

c# - 在 Controller 类之外获取 IRouter 实例的最简单方法