27

我正在尝试使用新的发布配置文件支持(在 VS2012 和 VS2010 中通过更新提供)来创建持续交付“部署管道”,从而在第一个“阶段”制作包/压缩包,并且相同的包是使用不同的配置部署到各种环境。

使用 pubxml 文件中定义的设置,从命令行部署现有包时涉及哪些任务/属性,并且不会导致构建?换句话说,我想“发布”到一个,然后再将同一个包“发布”到另一个配置文件而不重建它。

(我知道我可以直接使用 MSDeploy,但如果可能的话,我希望每个项目的管道更少)

4

1 回答 1

19

更新 2014-01-28

使我的自定义脚本与 VS / Azure SDK 的不断变化的版本保持同步最终是一项艰巨的工作,所以我实际上已经恢复到使用生成的deploy.cmd脚本,但有一个细微的区别:

我已经开始将所有参数值从ProfileName.pubxml文件中删除,而是将它们放在一个ProfileName.paramters.xml.SetParameters.xml使用包生成的示例,此处的文档)中。这些将按约定由 Visual Studio / MSBuild 自动拾取,我可以在运行时通过-setParamFile:path\to\ProfileName.parameters.xml调用时传入它们来使用它们deploy.cmd

更新 - 现在正在 GitHub 上维护(并记录)此脚本的更新版本 - https://github.com/richardszalay/msdeploy-package-publish

经过大量挖掘,我发现 Microsoft.Web.Publishing.targets (v10.5) 中的几个问题阻止了它的工作。为了解决这些问题,我创建了以下 MSBuild 脚本,可以将其放置在与 Web 应用程序的 csproj 相同的目录中。我添加了与修复和实现细节相关的评论。

该脚本使用 Microsoft.Web.Publishing.targets,因此大多数标准属性应该仍然有效。以下是您可以使用它的一些方法:

# Convention based
msbuild PackageDeploy.build /p:PublishProfile=Stage;WebPublishPipelineProjectName=Name_of_your_web_application

# Absolute paths to profile + package
msbuild PackageDeploy.build /p:PublishProfile=Path\To\Profile.pubxml;PackageFileName=Path\To\Package.zip;WebPublishPipelineProjectName==Name_of_your_web_application

如果您使用的是 VS2012,请确保您声明VisualStudioVersion=v11.0导入正确的发布文件。

使用此脚本,您无需在部署管道的后续阶段重新检查您的 Web 应用程序。在构建/打包阶段之后,您只需要保留以下工件:

  • PackageDeploy.build(下)
  • 您的发布配置文件
  • Web 应用程序包 zip

这是 PackageDeploy.build 的源代码:

<!--
This build script supports deployment of a website package to a publish profile without rebuilding the project or package

If placed in the same directory as a web project that uses publish profiles, the following arguments will need to be defined:

Convention based required arguments:
  PublishProfile: the name of the publish profile (or a path to a pubxml file if using non-convention based)
  Configuration: Debug/Release

Convention based optional arguments:
  VisualStudioVersion: Property specific to this build script that determines which WPP version to use (v10.5 [default] for VS2010+Azure updates, v11.0 for VS2012)
  WebPublishPipelineProjectName:
  WebPublishPipelineProjectDirectory: The root to the web project directory if this build script isn't there and PublishProfile isn't a path (to auto-detect publish profile directory)

Non-convention based optional arguments:
  PackageFileName: The full path to the website package zip
  UseDeclareParametersXMLInMsDeploy: true to save the parameters to a file and then use that file; false to inline the parameters
  UseMsDeployExe: true to use msdeploy.exe; false to use the VS MSBuild task

-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="DeployFromPackage">

  <PropertyGroup>
    <!-- IMPL: Set this to v11.0 to use VS2012 -->
    <VisualStudioVersion>v10.5</VisualStudioVersion>
  </PropertyGroup>

  <PropertyGroup>
    <!-- IMPL: Declared in Microsoft.Web.Publishing.targets, but we need to declare PublishProfileRootFolder before it's imported -->
    <WebPublishPipelineProjectDirectory Condition="'$(WebPublishPipelineProjectDirectory)'==''">$(MSBuildProjectDirectory)</WebPublishPipelineProjectDirectory>

    <!-- IMPL: Usually detected by ".csproj" vs ".vbproj", but PackageDeploy.build is neither -->
    <PublishProfileRootFolder Condition="'$(PublishProfileRootFolder)' == '' and Exists('My Project\PublishProfiles')">$(WebPublishPipelineProjectDirectory)\My Project\PublishProfiles</PublishProfileRootFolder>
    <PublishProfileRootFolder Condition="'$(PublishProfileRootFolder)' == '' and Exists('Properties\PublishProfiles')">$(WebPublishPipelineProjectDirectory)\Properties\PublishProfiles</PublishProfileRootFolder>
  </PropertyGroup>

  <!-- IMPL: Select the correct version of Microsoft.Web.Publishing.targets (usually done by the csproj via WebApplication.targets) -->
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets" />

  <!-- FIX: MSDeployPublish depends on building the package (can be skipped by clearing MSDeployPublishDependsOn) -->
  <!-- IMPL: ImportPublishingParameterValues transforms all the MSDeployParameterValue+ParameterValue to MsDeployDeclareParameters+Value -->
  <Target Name="DeployFromPackage" Condition="'$(PublishProfile)' != ''" DependsOnTargets="ImportPublishingParameterValues">

    <PropertyGroup>
      <_PublishMsDeployServiceUrl>$(MsDeployServiceUrl)</_PublishMsDeployServiceUrl>
      <_PublishMsDeployServiceUrl Condition="('$(MSDeployPublishMethod)'=='INPROC')"></_PublishMsDeployServiceUrl>
    </PropertyGroup>

    <ItemGroup>
      <!-- IMPL: Always uses "package" source -->
      <MsDeploySourceProviderSetting Remove="@(MsDeploySourceProviderSetting)" />
      <MsDeploySourceProviderSetting Include="package">
        <Path>@(_MSDeployPackageFile->'%(FullPath)')</Path>
        <EncryptPassword>$(DeployEncryptKey)</EncryptPassword>
        <WebServerAppHostConfigDirectory>$(_MSDeploySourceWebServerAppHostConfigDirectory)</WebServerAppHostConfigDirectory>
        <WebServerManifest>$(_MSDeploySourceWebServerManifest)</WebServerManifest>
        <WebServerDirectory>$(_MSDeploySourceWebServerDirectory)</WebServerDirectory>
      </MsDeploySourceProviderSetting>

      <MsDeployDestinationProviderSetting Remove="@(MsDeployDestinationProviderSetting)" />
      <MsDeployDestinationProviderSetting Include="auto">
        <Path></Path>
        <ComputerName>$(_PublishMsDeployServiceUrl)</ComputerName>
        <UserName>$(UserName)</UserName>
        <Password>$(Password)</Password>
        <EncryptPassword>$(DeployEncryptKey)</EncryptPassword>
        <IncludeAcls>False</IncludeAcls>
        <AuthType>$(AuthType)</AuthType>
        <WebServerAppHostConfigDirectory>$(_MSDeployDestinationWebServerAppHostConfigDirectory)</WebServerAppHostConfigDirectory>
        <WebServerManifest>$(_MSDeployDestinationWebServerManifest)</WebServerManifest>
        <WebServerDirectory>$(_MSDeployDestinationWebServerDirectory)</WebServerDirectory>
      </MsDeployDestinationProviderSetting>
    </ItemGroup>

    <!--Debug/Diagnostic message is not localized-->
    <Message Text="MSDeployPublish MsDeploySourceProviderSetting is @(MsDeploySourceProviderSetting)" Condition="$(EnablePackageProcessLoggingAndAssert)" />
    <Message Text="MSDeployPublish MsDeployDestinationProviderSetting is @(MsDeployDestinationProviderSetting)" Condition="$(EnablePackageProcessLoggingAndAssert)"/>

    <ExportParametersFile
      Condition="!$(UseDeclareParametersXMLInMsDeploy) And $(EnablePackageProcessLoggingAndAssert)"
      Parameters="@(MsDeployDeclareParameters)"
      DeclareSetParameterFile="$(PackageLogDir)\MSDeployPublish.parameters.xml"
      GenerateFileEvenIfEmpty="True"
      />

    <!--First delete the ParameterFile-->
    <Delete Files="$(PublishParametersFile)"  Condition="Exists($(PublishParametersFile))" ContinueOnError="true"/>

    <!-- FIX: Use SetParameterFile (rather than DeclareSetParameterFile), which isn't used anywehere in Microsoft.Web.Publishing.targets -->
    <ExportParametersFile
      Parameters="@(MsDeployDeclareParameters)"
      SetParameterFile="$(PublishParametersFile)"
      GenerateFileEvenIfEmpty="True"
      Condition="$(UseDeclareParametersXMLInMsDeploy)"
      />

    <PropertyGroup>
      <_VsPublishParametersFile></_VsPublishParametersFile>
      <_VsPublishParametersFile Condition="$(UseDeclareParametersXMLInMsDeploy) and '$(_VsPublishParametersFile)'==''">$(PublishParametersFile)</_VsPublishParametersFile>
    </PropertyGroup>

    <ItemGroup Condition="!$(UseDeclareParametersXMLInMsDeploy)">
      <_VsPublish_MsDeployDeclareParameters Remove="@(_VsPublish_MsDeployDeclareParameters)" />
      <_VsPublish_MsDeployDeclareParameters Include="@(MsDeployDeclareParameters)" />

      <!-- IMPL: Utilising the real version of this has way too much baggage (simplifying it could have repercussions, though) -->
      <_VsPublish_MsDeployDeclareParameters Include="$(DeployParameterIISAppName)" Condition="'$(DeployIisAppPath)' != ''">
        <Value>$(DeployIisAppPath)</Value>
      </_VsPublish_MsDeployDeclareParameters>
    </ItemGroup>

    <!-- FIX: Microsoft.Web.Publishing.targets uses "SetParameterItems", which doens't appear to work. This uses SimpleSetParameterItems instead  -->
    <VSMSDeploy
      Condition="!$(UseMsdeployExe)"
      MSDeployVersionsToTry="$(_MSDeployVersionsToTry)"
      Source="@(MsDeploySourceProviderSetting)"
      Destination="@(MsDeployDestinationProviderSetting)"
      DisableLink="$(PublishDisableLinks)"
      EnableLink="$(PublishEnableLinks)"
      AllowUntrustedCertificate="$(AllowUntrustedCertificate)"
      BuildingInsideVisualStudio="$(BuildingInsideVisualStudio)"
      SkipExtraFilesOnServer="$(SkipExtraFilesOnServer)"
      SkipRuleItems="@(MsDeploySkipRules)"
      OptimisticParameterDefaultValue="$(EnableOptimisticParameterDefaultValue)"
      SimpleSetParameterItems="@(_VsPublish_MsDeployDeclareParameters)"
      ImportSetParametersItems="$(_VsPublishParametersFile)"
      RetryAttempts="$(RetryAttemptsForDeployment)"
      InvokedByPublish="true"
    >
      <Output TaskParameter="Result" PropertyName="_PublishResult" />
    </VSMSDeploy>

    <!-- FIX: Microsoft.Web.Publishing.targets uses "SetParameterItems", which doens't appear to work. This uses SimpleSetParameterItems instead  -->
    <MSdeploy
          Condition="$(UseMsdeployExe)"
          Verb="sync"
          Source="@(MsDeploySourceProviderSetting)"
          Destination="@(MsDeployDestinationProviderSetting)"
          DisableLink="$(PublishDisableLinks)"
          EnableLink="$(PublishEnableLinks)"
          EnableRule="$(MsDeployDoNotDeleteRule)"
          AllowUntrusted="$(AllowUntrustedCertificate)"
          SkipRuleItems="@(MsDeploySkipRules)"
          OptimisticParameterDefaultValue="$(EnableOptimisticParameterDefaultValue)"
          SimpleSetParameterItems="@(_VsPublish_MsDeployDeclareParameters)"
          ImportSetParametersItems="$(_VsPublishParametersFile)"
          RetryAttempts="$(RetryAttemptsForDeployment)"
          ExePath="$(MSDeployPath)" />
  </Target>

</Project>
于 2012-08-14T01:08:35.690 回答