有没有办法从代码中终止(停止)TFS 2017 团队构建?

Is there any way to kill (stop) a TFS 2017 team build from code?

我有一个 TFS2017 版本,第一步是检查 c:\ 驱动器上是否存在所需的文件夹。该文件夹是正确完成构建的先决条件,因此如果该文件夹不存在,我想显示一条错误消息并终止构建。我试过返回 -1 但这并没有阻止构建继续进行。有没有办法以编程方式终止 运行 构建?

这是我当前的 PS 脚本,用于检查文件夹。我用 xxxx 代替了我的实际文件夹名称(以保护无辜者;-):

param([string]$DirSuffix="x")
<#
This script is called by the xxxx build to make sure that the GVB folder 
exists on c:\ before the build is run. It should be called by passing in the 
directory suffix (e.g. \xxxx 2.3.1). I can't figure out how to 
kill the build 
if the folder doesn't exist so I'm just going to write multiple errors to 
the console and hope that the builder see them and cancels the build.
#>

[string] $WholeDirectory='C:\XXXX' + $DirSuffix
if (-NOT [IO.Directory]::Exists($WholeDirectory)) 
{
  Write-Host Directory $WholeDirectory does not exist - please make sure that the xxxx build has run first!!!
  Write-Host "##vso[task.logissue type=error;] Directory $WholeDirectory does not exist - please make sure that the xxxx build has run 
  return -1
}

如果您有多个代理并且您希望 运行 仅在所需文件夹存在的代理上构建,您可以将此文件夹添加到 "Capabilities" (Click here to photo), 在构建定义中,您需要在 "Demands" 上指定它是必需的 (Click here to photo)。

不应使用 return -1,而应使用 Logging Commands 退出代码 使构建任务失败,然后使整个构建失败。

 Write-Error "Some error"
 exit 1

添加一个powershell任务来捕获文件夹不存在并判断是否必须让任务失败。

有关如何使 vNext 构建失败的更多详细信息,请参阅此问题:

还请记得检查 powershell 任务中的 标准错误失败 选项以及 select 条件 仅当所有先前的任务都成功时 到 运行 此任务用于构建管道中 powershell 旁边的构建任务。这将终止构建过程。