Created
September 8, 2017 15:32
-
-
Save ukcoderj/f5613f059cfd6f6e98a730393c044524 to your computer and use it in GitHub Desktop.
Start a TFS/ VSTS Build And Wait For Completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-PSSnapin Microsoft.TeamFoundation.PowerShell | |
#Add-Type -Path "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll" | |
# *VSTS Load Above* Load the TFS powershell | |
# NOTE: Must allow build to access tokens in the options | |
Clear-Host | |
$buildToStart = "My-Build-Name e.g. Test - CLI" | |
$newLine = [System.Environment]::NewLine | |
#$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = "https://mytenancy.visualstudio.com/" | |
#$env:SYSTEM_TEAMPROJECTID = "MySharedApps" | |
"*************************************************************************************************************************************************" | |
"Get All Builds in the project" | |
# e.g. https://mytenancy.visualstudio.com/MySharedApps/_apis/build/definitions?api-version=2.0 | |
$allBuildsUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0" | |
$allBuildsUrl | |
$allBuilds = Invoke-RestMethod -Uri $allBuildsUrl -Headers @{ | |
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" | |
} | |
"Builds Found" | |
"-------------------------------------------------------------------------------------------------" | |
$allBuilds | |
"-------------------------------------------------------------------------------------------------" | |
$buildWeWant = {} | |
foreach($item in $allBuilds.value) | |
{ | |
"Build - $item.name" | |
if($item.name -like $buildToStart) | |
{ | |
"YES! Got Build $item.name" | |
$buildWeWant = $item | |
break | |
} | |
} | |
$buildId = $buildWeWant.id | |
$buildName = $buildWeWant.name | |
"************************************ Queue Build $buildName ($buildId) ********************************************************" | |
# Kick off a build | |
$json = "definition: { id:$buildId }" | |
$json = "{ $json }" | |
"JSON: $json" | |
$buildRequest = Invoke-RestMethod -Method Post -ContentType application/json -Uri "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/builds?api-version=2.0" -Body $json -Headers @{ | |
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" | |
} | |
$requestedBuildId = $buildRequest.id | |
"-------------------------------------------------------------------------------------------------" | |
"Build Request ID: $requestedBuildId" | |
"-------------------------------------------------------------------------------------------------" | |
$i = 1 | |
do{ | |
"$newLine $newLine" | |
"Check: $i" | |
# e.g. https://mytenancy.visualstudio.com/MySharedApps/_apis/build/builds/226?api-version=2.0 | |
$checkBuildUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/builds/$($requestedBuildId)?api-version=2.0" | |
$checkBuildUrl | |
$buildStatus = Invoke-RestMethod -Uri $checkBuildUrl -Headers @{ | |
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" | |
} | |
$currentBuildStatus = $buildStatus.status | |
if($currentBuildStatus -eq "completed") | |
{ | |
if($buildStatus.result -ne "succeeded") | |
{ | |
$buildResult = $buildStatus.result | |
throw [Exception] "BUILD FAILED: $buildToStart. Result: $buildResult" | |
} | |
else | |
{ | |
"BUILD COMPLETE: $buildToStart" | |
} | |
} | |
else{ | |
"Build Status: $currentBuildStatus" | |
} | |
sleep 60 #wait 60 seconds | |
$i++ | |
} | |
while ($i -le 120) #keep checking for 120 minutes | |
"*************************************************************************************************************************************************" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment