【问题标题】:Deploy windows services from team city从团队城市部署 Windows 服务
【发布时间】:2013-11-02 23:16:28
【问题描述】:

我想以最少的服务器配置将 Windows 服务从运行团队城市的构建服务器部署到 Windows Server 2012。

最好的方法之一是什么?

【问题讨论】:

    标签: msbuild windows-services teamcity msdeploy windows-server-2012


    【解决方案1】:

    我通常为此使用直接的 powershell 或 msbuild。我尽量避免易碎的 msdeploy。 因此,在 msbuild 中,您可以使用非常方便的 msbuild 扩展包,因此假设您可以将文件复制到目标位置(Robocopy 很方便),剩下的就可以完成了:

    <Project ToolsVersion="4.0" DefaultTargets="InstallService" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <UsingTask AssemblyFile="..\packages\MSBuild.Extension.Pack.1.2.0\lib\net40\MSBuild.ExtensionPack.dll"
    TaskName="MSBuild.ExtensionPack.Computer.WindowsService"/>
    
      <PropertyGroup>
        <MachineName Condition="$(MachineName)==''"></MachineName>
        <ServiceName Condition="$(ServiceName)==''"></ServiceName>
        <ServicePath Condition="$(ServicePath)==''"></ServicePath>
        <User Condition="$(User)==''"></User>
        <Password Condition="$(Password)==''"></Password>
        <SuppressStart Condition="$(SuppressStart)==''"></SuppressStart>
      </PropertyGroup>
    
      <Target Name="CheckProperties" BeforeTargets="StopService">
        <Message Text="MachineName: $(MachineName)"/>
        <Message Text="ServiceName: $(ServiceName)"/>
        <Message Text="ServicePath: $(ServicePath)"/>
        <Message Text="User : $(User)"/>
        <Message Text="SuppressStart : $(SuppressStart)"/>
      </Target>
    
      <Target Name="StopService" BeforeTargets="InstallService">
    
        <MSBuild.ExtensionPack.Computer.WindowsService
          TaskAction="CheckExists"
          ServiceName="$(ServiceName)"
          MachineName="$(MachineName)">
          <Output TaskParameter="Exists" PropertyName="DoesExist"/>
        </MSBuild.ExtensionPack.Computer.WindowsService>
    
        <MSBuild.ExtensionPack.Computer.WindowsService
          TaskAction="Stop"
          ServiceName="$(ServiceName)"
          MachineName="$(MachineName)"
          Condition="$(DoesExist)=='True'">
        </MSBuild.ExtensionPack.Computer.WindowsService>
      </Target>
    
      <Target Name="StartService" AfterTargets="InstallService">
    
        <MSBuild.ExtensionPack.Computer.WindowsService
        TaskAction="CheckExists"
        ServiceName="$(ServiceName)"
        MachineName="$(MachineName)">
          <Output TaskParameter="Exists" PropertyName="DoesExist"/>
    
        </MSBuild.ExtensionPack.Computer.WindowsService>
    
        <MSBuild.ExtensionPack.Computer.WindowsService
          TaskAction="Start"
          ServiceName="$(ServiceName)"
          MachineName="$(MachineName)"
          Condition="$(DoesExist)=='True' And $(SuppressStart)=='False'"
          RetryAttempts="20">
        </MSBuild.ExtensionPack.Computer.WindowsService>
        <Message Text="Service $(ServiceName) set not to start" Condition="$(SuppressStart)=='True'" Importance="High" />
      </Target>
    
      <Target Name="InstallService">
    
        <PropertyGroup>
          <ServiceExeExists Condition="Exists('$(ServicePath)')">True</ServiceExeExists>
        </PropertyGroup>
    
        <Message Text="Installing $(ServicePath) %(ServiceName.Identity)" Importance="high"/>
        <MSBuild.ExtensionPack.Computer.WindowsService
          TaskAction="CheckExists"
          ServiceName="$(ServiceName)"
          MachineName="$(MachineName)">
          <Output TaskParameter="Exists" PropertyName="DoesExist"/>
    
        </MSBuild.ExtensionPack.Computer.WindowsService>
    
        <Message Text="Installed $(ServiceName) $(ServicePath) for $(User) and $(Password)" Importance="high"/>
        <MSBuild.ExtensionPack.Computer.WindowsService
          TaskAction="Install"
          ServiceName="$(ServiceName)"
          User="$(User)"
          Password="$(Password)"
          ServicePath="$(ServicePath)"
          MachineName="$(MachineName)"
          Condition="!$(DoesExist)"/>
    
        <MSBuild.ExtensionPack.Computer.WindowsService
          TaskAction="SetAutomatic"
          ServiceName="$(ServiceName)"
          MachineName="$(MachineName)"
          Condition="!$(DoesExist)"/>
        <Warning Text="%(ServiceName.Identity) service already exists" Condition="$(DoesExist)"/>
    
      </Target>
    
    </Project>
    

    【讨论】:

    • 没有太多使用 msdeploy 的经验(使用过几次,但从未真正理解它是如何“在幕后”工作的),我必须承认我也觉得它有点不稳定。然而,这只是意见,但我最终使用了带有复制命令和几个 bat 文件的 msbuild。您的解决方案似乎更强大,但正如您所说,需要某种文件共享访问权限。在我们的例子中,我们做到了!因此,此解决方案有效,我将其标记为答案。你知道“卸载服务”或“停止服务”会等待服务优雅停止多久吗?永远?
    • 嗨,Marko 很高兴我能帮上忙,在线文档msbuildextensionpack.com/help/4.0.8.0/index.html 说 retryattempts 属性默认为 60。
    【解决方案2】:

    我们正在使用这样定义的 msdeploy 包:

    <sitemanifest>
      <runCommand path='presync.cmd' waitInterval='30000'/>
      <dirPath path='$winSvc' />
      <runCommand path='postsync.cmd' waitInterval='30000'/>
    </sitemanifest>
    

    presync.cmd:

    net stop Svc
    installUtil /u /name=Svc $destPath\Svc.exe
    

    postsync.cmd:

    installUtil /name=Svc $destPath\Svc.exe
    net start Svc
    

    所有文件均由powershell脚本生成。

    【讨论】:

    • 如果等待间隔不够长怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多