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

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

我基本上想要做一个web部署项目所做的事情,但是没有外接程序。我需要它来编译WAP,删除任何不必要的执行文件,执行任何web。配置转换,并将输出复制到指定的位置。

我的解决方案,基于杰夫·西尔弗的回答

<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>

当前回答

您可以通过下面的代码发布所需路径的解决方案,这里PublishInDFolder是我们需要发布的路径的名称(我们需要在下面的图片中创建这个)

您可以像这样创建发布文件

在批处理文件(.bat)中添加以下2行代码

@echo OFF 
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsMSBuildCmd.bat"
MSBuild.exe  D:\\Solution\\DataLink.sln /p:DeployOnBuild=true /p:PublishProfile=PublishInDFolder
pause

其他回答

可以使用此命令发布带有发布配置文件的web应用程序。

msbuild SolutionName.sln /p:DeployOnBuild=true /p:PublishProfile=PublishProfileName

这个发布概要文件示例可以创建一个版本号在网络路径中的AssemblyInfo.cs file中的发布zip文件(使用PowerShell命令创建zip文件并删除其他发布文件是可选的)。

 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <Major>0</Major>
    <Minor>1</Minor>
    <Build>2</Build>
    <Publish>C:\</Publish>
    <publishUrl>$(Publish)</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>
  <Target Name="GetBuildUrl">
    <PropertyGroup>      <In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs'))</In>
      <TargetPath>\\NetworkPath\ProjectName</TargetPath>
      <Pattern>^\s*\[assembly: AssemblyVersion\(\D*(\d+)\.(\d+)\.(\d+)\.(\d+)</Pattern>
      <Major>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</Major>
      <Minor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[2].Value)</Minor>
      <Build>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[3].Value)</Build>
      <Sub>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[4].Value)</Sub>
      <Publish>$(TargetPath)\$(Major).$(Minor).$(Build).$(Sub)\</Publish>
      <publishUrl Condition=" '$(Publish)' != '' ">$(Publish)</publishUrl>
      <publishUrl Condition=" '$(Publish)' == '' and '$(LastUsedBuildConfiguration)'!='' ">$(LastUsedBuildConfiguration)</publishUrl>
    </PropertyGroup>
  </Target>
  <Target Name="BeforeBuild" DependsOnTargets="GetBuildUrl">
    <Message Importance="High" Text="|" />
    <Message Importance="High" Text=" ================================================================================================" />
    <Message Importance="High" Text="    BUILD INFO                                                                                    " />
    <Message Importance="High" Text="    Version [$(Major).$(Minor).$(Build)] found in [$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs] " />
    <Message Importance="High" Text="    Build will be saved to [$(publishUrl)]                                                        " />
    <Message Importance="High" Text=" =================================================================================================" />
    <Message Importance="High" Text="|" />
  </Target>
  <Target Name="Zip" BeforeTargets="AfterBuild">
    <Exec Command="PowerShell -command Compress-Archive -Path $(Publish) -DestinationPath $(Publish)Release.zip" />
    <Exec Command="PowerShell -command Remove-Item -Recurse -Force $(Publish) -Exclude Release.zip" />
  </Target>
</Project>

我不知道TeamCity,所以我希望这对你有用。

我发现最好的方法是使用MSDeploy.exe。这是微软WebDeploy项目的一部分。你可以在这里下载。

使用WebDeploy,您可以运行命令行

msdeploy.exe -verb:sync -source:contentPath=c:\webApp -dest:contentPath=c:\DeployedWebApp

这与VS Publish命令所做的事情相同,只复制必要的位到部署文件夹。

这是我的工作批次

publish-my-website.bat

SET MSBUILD_PATH="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin"
SET PUBLISH_DIRECTORY="C:\MyWebsitePublished"
SET PROJECT="D:\Github\MyWebSite.csproj"


cd /d %MSBUILD_PATH%
MSBuild %PROJECT%  /p:DeployOnBuild=True /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=True /p:publishUrl=%PUBLISH_DIRECTORY%

请注意,我在服务器上安装了Visual Studio以便能够运行MsBuild.exe,因为. net Framework文件夹中的MsBuild.exe不能工作。

我得到了它大部分工作没有自定义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

这将编译,打包(与web。配置转换),并将输出保存为工件。唯一缺少的是将输出复制到指定的位置,但这可以在另一个TeamCity构建配置中使用工件依赖项或使用msbuild脚本完成。

更新

这是一个msbuild脚本,将编译,打包(与web。配置转换),并将输出复制到登台服务器

<?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的参数。配置,env。ProjectName和env.DeployServiceUrl。MSBuild运行器具有构建文件路径,并且自动传递参数(您不必在命令行参数中指定它们)。

你也可以从命令行运行它:

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

您必须设置您的环境

<网站名称> 域> <

并参考我的博客。(抱歉帖子是韩语)

http://xyz37.blog.me/50124665657 http://blog.naver.com/PostSearchList.nhn?SearchText=webdeploy&blogId=xyz37&x=25&y=7 @ECHO OFF :: http://stackoverflow.com/questions/5598668/valid-parameters-for-msdeploy-via-msbuild ::-DeployOnBuild -True :: -False :: ::-DeployTarget -MsDeployPublish :: -Package :: ::-Configuration -Name of a valid solution configuration :: ::-CreatePackageOnPublish -True :: -False :: ::-DeployIisAppPath -<Web Site Name>/<Folder> :: ::-MsDeployServiceUrl -Location of MSDeploy installation you want to use :: ::-MsDeployPublishMethod -WMSVC (Web Management Service) :: -RemoteAgent :: ::-AllowUntrustedCertificate (used with self-signed SSL certificates) -True :: -False :: ::-UserName ::-Password SETLOCAL IF EXIST "%SystemRoot%\Microsoft.NET\Framework\v2.0.50727" SET FXPath="%SystemRoot%\Microsoft.NET\Framework\v2.0.50727" IF EXIST "%SystemRoot%\Microsoft.NET\Framework\v3.5" SET FXPath="%SystemRoot%\Microsoft.NET\Framework\v3.5" IF EXIST "%SystemRoot%\Microsoft.NET\Framework\v4.0.30319" SET FXPath="%SystemRoot%\Microsoft.NET\Framework\v4.0.30319" SET targetFile=<web site fullPath ie. .\trunk\WebServer\WebServer.csproj SET configuration=Release SET msDeployServiceUrl=https://<domain>:8172/MsDeploy.axd SET msDeploySite="<WebSite name>" SET userName="WebDeploy" SET password=%USERNAME% SET platform=AnyCPU SET msbuild=%FXPath%\MSBuild.exe /MaxCpuCount:%NUMBER_OF_PROCESSORS% /clp:ShowCommandLine %MSBuild% %targetFile% /p:configuration=%configuration%;Platform=%platform% /p:DeployOnBuild=True /p:DeployTarget=MsDeployPublish /p:CreatePackageOnPublish=False /p:DeployIISAppPath=%msDeploySite% /p:MSDeployPublishMethod=WMSVC /p:MsDeployServiceUrl=%msDeployServiceUrl% /p:AllowUntrustedCertificate=True /p:UserName=%USERNAME% /p:Password=%password% /p:SkipExtraFilesOnServer=True /p:VisualStudioVersion=12.0 IF NOT "%ERRORLEVEL%"=="0" PAUSE ENDLOCAL