首页 > 解决方案 > 有什么方法可以杀死(停止)从代码构建的 TFS 2017 团队?

问题描述

我有一个 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
}

标签: tfsbuild

解决方案


Instead of using return -1, you should use Logging Commands and exit code to fail a build task, then fail the entire build.

 Write-Error "Some error"
 exit 1

Add a powershell task to catch if the folder doesn't exist and judge if you have to fail the task or not.

More details about how to fail a vNext build please refer this question: How to fail the build from a PowerShell task in TFS 2015

Also remember to check the Fail on Standard Error option in your powershell task and also select the condition Only when all previous tasks have succeeded to Run this task for the build task which next to powershell in your build pipeline. This will kill the build process.

enter image description here


推荐阅读