1: param()
2:
3: #This is how we do parameters in a custom build task
4: Trace-VstsEnteringInvocation $MyInvocation
5: try {
6: Import-VstsLocStrings "$PSScriptRoot\Task.json"
7: [int32]$timeout = Get-VstsInput -Name TimeOut
8: [string]$WorkingFolder = Get-VstsInput -Name WorkingFolder
9: }
10: finally
11: {
12: Trace-VstsLeavingInvocation $MyInvocation
13: }
14:
15: #Start the Timer at zero
16: $timesofar = 0
17: #This is the file we are looking for to know the package is complete
18: $BuildComplete = "$WorkingFolder\BuildComplete.txt"
19: #Clean the Working Folder
20: Remove-Item "$WorkingFolder\*"
21: #Create file containing Build number to trigger the build
22: New-Item -path "$WorkingFolder" -name 'Build.txt' -type 'file' -value "$env:BUILD_BUILDNUMBER"
23:
24: Write-Host "Triggered Build"
25: #While BuildComplete.txt does not exist and timer has not reached the timeout specified
26: while (!(Test-Path $BuildComplete) -and !($timesofar -eq $timeout))
27: {
28: #Wait for 10 seconds
29: Start-Sleep 10
30: #Add 10 seconds to the time So Far
31: $timesofar = $timesofar + 10
32: }
33:
34: #Either the File has been dropped by the other system or the timeout was reached
35: if (!(Test-Path $BuildComplete))
36: {
37: #If the file was not dropped it must be the timeout so fail the build write to the console
38: Write-Error "Build Timed out after $timeout seconds."
39: }
40: else
41: {
42: #If we got here the file was dropped
43: #Get the content of the file
44: $BuildResult = Get-Content $BuildComplete -Raw
45:
46: #Check if it says successful
47: if ($BuildResult.StartsWith("successful"))
48: {
49: #On Success say so
50: Write-Host "Successful Build"
51: #write the contents out in case the calling system wants to tell us something
52: write-Host "$BuildResult"
53: }
54: else
55: {
56: #On Error Faile the build and say so
57: Write-Error "Build Error"
58: #write the contents out so we can see why it failed
59: Write-Error "$BuildResult"
60: }
61: }