1

I have a TFS2017 build and the first step is to check if a required folder exists on the c:\ drive. That folder is a pre-requisite for the build to finish properly so if the folder doesn't exist I want to display an error message and kill the build. I've tried returning -1 but that doesn't stop the build from continuing. Is there any way to kill a running build programmatically?

Here's my current PS script that checks for the folder. I've substituted xxxx for my actual folder names (to protect the innocent ;-):

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
}
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Ben_G
  • 770
  • 2
  • 8
  • 30
  • Relying on previous builds in this fashion is a really bad practice. If your builds generate down-stream dependencies, use a package manager to create and distribute known-good, versioned packages of dependencies. – Daniel Mann Jun 05 '18 at 16:26
  • Thanks Daniel. I know it's bad practice, but I inherited this whole (huge) build process and re-writing it is taking a lot of time. In the mean-time, just in case I don't get around to re-doing this part of it I'd like to display a warning so that people in my role in the future don't get burned with this pre-req not being there and the build failing for some unknown reason. – Ben_G Jun 05 '18 at 16:29
  • And using a package manager isn't really an option as this is an entire build (a 20-minute process) that needs to happen first. Again, I didn't design this nor do I condone it - I just need to live with it for now. – Ben_G Jun 05 '18 at 16:31

2 Answers2

0

If you have multiple agents and you want to run the build only on the agent that required folder is exists, you can add this folder to the "Capabilities" (Click here to photo), and in the build definition you need to specify on the "Demands" that it's required (Click here to photo).

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
0

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

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Thanks. I was doing all of that except for the exit 1. I was using return -1, but exit 1 seems to do the trick. Thanks! – Ben_G Jun 06 '18 at 15:47