我想以最少的服务器配置将 Windows 服务从我们运行团队城市的构建服务器部署到 Windows Server 2012。
做到这一点的最佳方法之一是什么?
我想以最少的服务器配置将 Windows 服务从我们运行团队城市的构建服务器部署到 Windows Server 2012。
做到这一点的最佳方法之一是什么?
我通常为此使用直接的 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 包:
<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 脚本生成。