【问题标题】:TFS Build vNext - start process after successful buildTFS Build vNext - 成功构建后启动进程
【发布时间】:2016-08-25 04:05:24
【问题描述】:

在我们的构建服务器上,我们有一个正在运行的进程,除其他外,它是运行集成测试所必需的,以获取对数据库的访问权限。

这个软件可以在我们的存储库中更改,因此我创建了一个 CI 构建来构建更改。然后我想做的是在成功构建后使用新编译的版本重新启动该过程,但这部分我似乎无法开始工作。

我没有问题终止正在运行的进程,并将结果部署到构建服务器上的特定位置,但似乎无论我尝试什么,只要正在运行的构建结束,我生成的进程就会被终止。

我尝试了以下方法:

使用 PowerShell

Start-Process <path to file>

使用 CMD

使用 'cmd' 作为工具和以下参数:

<path to file>
start <path to file>
/c start <path to file>
cmd <path to file>
cmd start <path to file>
cmd /c start <path to file>

我也尝试过简单地提供 .exe 路径作为工具名称,没有参数,也没有运气。

效果如何?

好吧,对于上述大多数方法,使用 PS 命令Get-Process &lt;exe name&gt;* 的额外步骤我得到了进程正在运行的结果。在停止该过程的步骤之后,相同的步骤没有产生任何结果,因此可以启动一个新的更新的步骤。所以我很肯定它可以工作,vNext 构建只是在构建结束后将其全部杀死。

其他解决方案

我还剩下 2 个我认为应该可行的解决方案,但是这两个解决方案对我来说都太复杂了,因为我在构建过程中引入了相当复杂的东西,然后可能会出错,但是这里是:

  1. 将构建服务器设置为有效的部署目标。然后我猜我可以使用“目标机器上的 PowerShell”步骤,即使我的目标是自己。我会假设它将作为单独的进程。不过,这需要各种配置才能完成,还要为该任务编写远程 PowerShell 脚本。
  2. 编写一个可调用的小型 Windows 服务,例如REST,然后可以启动进程,因此新的 Windows 服务成为拥有线程。 - 这只是引入了一个可能也需要更新的新层。然后可能会手动更新而不是自动更新。

同样,如果存在更好的解决方案,我宁愿不使用这两种解决方案中的任何一种。 :)

【问题讨论】:

    标签: powershell tfs tfsbuild


    【解决方案1】:

    目前我通过安装AutoIt 来修复它,并将这个简单的脚本添加到“deploy”文件夹中:

    While 1 ; Opens up a WHILE loop, with 1 as a constant, so it is infinite
    If Not ProcessExists("DelphiDebug.exe") Then Run("DelphiDebug.exe") ; if the process of DelphiDebug.exe doesn't exist, it starts it
    Sleep (10) ; Puts the script to sleep for 10 milliseconds so it doesn't chew CPU power
    WEnd ; Closes the loop, tells it to go back to the beginning
    

    Credit goes to this forum entry 制作记事本示例的地方

    然后我制作了一个 PowerShell 脚本来重命名当前版本,将新构建的版本复制到“deploy”文件夹,停止正在运行的实例,并删除重命名的版本:

    # CONSTANTS AND CALCULATED VARIABLES
    [string]$deploymentFolder = 'C:\Deployment-folder-on-local-machine'
    [string]$deploymentPath = "$deploymentFolder\my-program.exe"
    [string]$searchPathExistingVersion = $deploymentPath + '*' # The star is important so Get-Item does not throw an error
    [string]$toDeleteExeName = 'please-delete.exe'
    [string]$newCompiledVersionFullPath = "$env:BUILD_SOURCESDIRECTORY\sub-path-to-bin-folder\my-program.exe"
    [string]$processName = 'my-program'
    [string]$searchPathToDeleteVersion = "$deploymentFolder\$toDeleteExeName*" # The star is important so Get-Item does not throw an error
    
    # EXECUTION
    Write-Verbose "Search path: $searchPathExistingVersion"
    $existingVersion = Get-Item $searchPathExistingVersion;
    if ($existingVersion) {
        Write-Debug "Match found: $existingVersion"
        Rename-Item $existingVersion.FullName $toDeleteExeName
    } else {
        Write-Debug 'No existing file found'
    }
    Write-Verbose "Copy new version from path: $newCompiledVersionFullPath"
    Copy-Item $newCompiledVersionFullPath $deploymentPath
    Write-Verbose 'Stopping running processes'
    Get-Process ($processName + '*') | Stop-Process # The new version is auto started with a running AutoIt script in the deployment folder.
    Write-Verbose "Deleting old versions with search path: $searchPathToDeleteVersion"
    $toDeleteVersion = Get-Item $searchPathToDeleteVersion
    if ($toDeleteVersion) {
      Write-Debug "Match found: $toDeleteVersion"
      Remove-Item $toDeleteVersion -ErrorAction SilentlyContinue # Deletion is not critical. Next time the build runs, it will attempt another cleanup.
    } else {
      Write-Debug 'No file found to delete'
    }
    

    同样,这个解决方案给服务器增加了另一个复杂性,所以我将这个问题保留几天,看看是否有更简单的解决方案。 :)

    【讨论】:

      【解决方案2】:

      我认为您需要将该进程安装为 Windows 服务并启动它,而不是在构建期间启动进程。有意义的是,您在构建上下文中启动的任何过程都会在构建完成时停止。您可以使用命令行安装/更新/启动 Windows 服务:

      sc create [service name] [binPath= ] 
      

      https://ozansafi.wordpress.com/2009/01/14/create-delete-start-stop-a-service-from-command-line/

      【讨论】:

      • 这是有道理的,但是能够绕过它也很有意义。 :) - 该进程在上下文菜单中有一个通知图标,其中包含许多调试工具,我们也需要访问该图标,因此在这种特定情况下将其作为服务运行是不可行的。 :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2016-11-13
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多