powershell - MSBuild - 构建后自动部署 Windows 服务

标签 powershell msbuild windows-services continuous-integration

好吧,这就是我想做的 -

我们有一个 Windows 服务,我们希望在每次成功的 CI 构建后自动将该服务部署到测试环境 - 为此,我们想到了以下方法 -

  • 构建 Windows 服务项目(以及依赖项)
  • 停止 Windows 服务
  • 将项目输出从输出文件夹复制到 Windows 服务文件路径
  • 启动服务

但是,目前我遇到了 Windows 服务启动和停止的问题。

这是 MSBuild 代码段 -

<!-- TODO: do this using Powershell instead of normal commandline so that it can be executed on remote machines as well -->
<Exec Command="Net Stop CreditProcessing" />
<!-- Exec Command="$(PowerShellExe) -NoProfile -Noninteractive -command &quot;&amp;{ Stop-Service CreditProcessing }&quot;" /-->

<!-- TODO: The destination needs to come from the env variable or rsp file? -->
<Copy 
    SourceFiles="@(WindowsServiceFilesToDeploy)" 
    DestinationFiles="@(WindowsServiceFilesToDeploy->'D:\WindowsServices\Credit Processing Service%(RecursiveDir)%(Filename)%(Extension)')" />  

<Exec Command="Net Start CreditProcessing" />   
<!--Exec Command="$(PowerShellExe) -NoProfile -Noninteractive -command Start-Service CreditProcessing" /--> 

我尝试使用直接命令行命令和 powershell 命令。

A)这是我尝试使用普通命令运行此构建时遇到的问题 - 服务需要一些时间来启动/停止,并且构建不会等待足够长的时间并报告错误并停止

“...服务无法启动”。 “服务没有报告错误”

或者有时在停止时

“该服务在当前状态下无法控制。”

而如果我们立即检查服务,它将处于“启动”阶段,很快就会进入“启动”阶段。有什么办法可以让构建更加耐心一点吗?如果直接从命令提示符运行该命令则不会执行此操作。

B)如果我使用上面的 powershell 命令(当前在代码片段中注释),这就是我们面临的问题 -

它只是在构建日志中打开 powershell 提示符,然后什么都不做 Build stops at powershell doesn't continue

您能帮助避免这些问题(至少其中一个)吗?我认为如果工作正常的话,更推荐使用 powershell 路线,但我愿意接受建议。

其他几个问题 -

  • 我们想要在远程计算机(除了发生此 CI 构建的位置之外)上部署服务(从而停止并重新启动) - 无论如何这可以完成吗?

  • 有没有办法添加检查服务是否已在运行并仅在服务正在运行时停止?如果它被停止并且我们尝试再次停止,它将给出“服务未启动”错误

最佳答案

您的启动命令采用以下形式:

path\to\powershell.exe
-arg1 -arg2

换行符意味着 PowerShell 永远不会接收参数,因此不知道它必须执行 Start-Service 命令,也不知道您不希望它具有交互性。它可能只是在等待您告诉它要做什么。

检查您的 $(PowerShell) 变量,因为它末尾可能有一个换行符。

关于powershell - MSBuild - 构建后自动部署 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951027/

相关文章:

powershell - 在 PowerShell 中的 foreach 之前,有没有更好的方法来检查集合是否为空?

vb.net - Windows 服务启动然后自动停止

windows-services - 从非管理员用户帐户启动/停止 Windows 服务

msbuild - 如何在不执行构建的情况下检索@(TargetOutputs)

windows-services - win服务中的ninject内核引用

excel - 如何更改 Excel 报表的背景颜色

powershell - 使用 powershell 导出 azure db 时出现错误

c# - 我可以从 PowerShell 访问我的自定义 .NET 类吗?

c# - 如何在 Visual Studio 2017 项目(新的 .csproj 文件格式)中设置 `OutputPath` 而目标框架不会混淆已解析的路径?

c# - 代理背后的 NuGet