简体   繁体   English

如何在 VS2010 中创建发布前和发布后脚本

[英]How to create pre-publish and post-publish scripts in VS2010

我想创建一个执行某些操作的脚本,然后将该站点发布到生产环境,然后在 Visual Studio 2010 中运行另一个脚本。

Of course it's possible.当然有可能。 I thing that you need MSBuild .我认为你需要MSBuild It already have a lot default tasks like copying, deleting, etc. Also there are a lot third party tasks for MSBuild like SDCTasks or Community Tasks它已经有很多默认任务,如复制、删除等。 MSBuild 也有很多第三方任务,如SDCTasksCommunity Tasks

For example here the one of various ways to deploy web site (using SDCTasks).例如这里部署网站的各种方法之一(使用 SDCTasks)。 In this example both web site solution and web services solution will be builded and in case of success they will be published to some remote server and properly configured with release version configuration files.在此示例中,将构建网站解决方案和 Web 服务解决方案,如果成功,它们将发布到某个远程服务器并使用发布版本配置文件进行正确配置。

   <Import Project="$(MSBuildExtensionsPath)\SDCTasks\Microsoft.Sdc.CommonWOBizTalk.tasks"/>
    ...
    <ItemGroup>
        <SolutionToBuild Include="$(BuildProjectFolderPath)/../../website.sln">
            <Targets></Targets>
            <Properties></Properties>
        </SolutionToBuild>

        <SolutionToBuild Include="$(BuildProjectFolderPath)/../../services.sln">
            <Targets></Targets>
            <Properties></Properties>
        </SolutionToBuild>

    </ItemGroup>

  <PropertyGroup>
    <PublishFolder>\\myservername\deployto</PublishFolder>
  </PropertyGroup>

  <Target Name="AfterCompile" DependsOnTargets="PublishWebSite;PublishServices;SetConfiguration"/>

  <Target Name="PublishWebSite">
    <Folder.CleanFolder Path="$(PublishFolder)" Force="True" />
    <Folder.CopyFolder Source="$(OutDir)_PublishedWebsites\MyWebSite" Destination="$(PublishFolder)" />
  </Target> 

  <Target Name="PublishServices">
    <MakeDir Directories="$(PublishFolder)\Services"/>
    <Folder.CopyFolder Source="$(OutDir)_PublishedWebsites\MyService" Destination= "$(PublishFolder)\ Services" />
  </Target>

  <Target Name="SetConfiguration">
      <Copy SourceFiles="$(OutDir)_PublishedWebsites\MyWebSite\WebRelease.config" DestinationFiles="$(PublishFolder)\web.config" />
      <Copy SourceFiles="$(OutDir)_PublishedWebsites\MyService\WebRelease.config" DestinationFiles="$(PublishFolder)\Services\web.config" />
  </Target>

I think that what your looking for would be a macro that would perform the actions before calling publish, then perform some additional actions.我认为您要寻找的是一个,它可以在调用发布之前执行操作,然后执行一些其他操作。 If you're using TFS 2010 as well then you could customize a build definition to do what you're looking for as well.如果您也使用 TFS 2010,那么您也可以自定义构建定义来执行您正在寻找的操作。

Late answer covering the "pre-publish" part (tested within VS2017, but I think this should work in previous versions as well):涵盖“预发布”部分的后期答案(在 VS2017 中测试,但我认为这也适用于以前的版本):

  1. Open Web publishing configuration file (eg Test.pubxml)打开 Web 发布配置文件(例如 Test.pubxml)

  2. Define a target that executes a custom scripts located somewhere within your project (eg some Powershell script within an inner folder).定义一个目标来执行位于项目中某处的自定义脚本(例如,内部文件夹中的某些 Powershell 脚本)。 This code should be put within the Project tag, after the first PropertyGroup :这段代码应该放在Project标签中,在第一个PropertyGroup

     <PropertyGroup> <PipelineDependsOn> ExtBuild; $(PipelineDependsOn); </PipelineDependsOn> </PropertyGroup> <Target Name="ExtBuild"> <Message Text="Building something else" Importance="high"/> <Exec Command="powershell.exe -file &quot;$(ProjectDir)\\Publish\\publish.ps1&quot;" /> </Target>

Note: the current directory is $(ProjectDir) , so be careful when including other ps1 files from the target one注意:当前目录是$(ProjectDir) ,所以在包含目标文件中的其他 ps1 文件时要小心

For pre-publish, at least for ClickOnce, you may add:对于预发布,至少对于 ClickOnce,您可以添加:

<Target Name="ClearPrevPulish" BeforeTargets="PublishOnly">
  <RemoveDir Directories="$(PublishUrl)" />
</Target>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM