Last active
September 22, 2017 07:56
-
-
Save ukcoderj/c1cb4e543806c16cab6e2c2322f5d830 to your computer and use it in GitHub Desktop.
VSTS Clone Build With Powershell
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
Clear-Host | |
$buildToCloneName = "Build 1" | |
$newBuildName = "Build 1 - Clone" | |
$user = "[email protected]" | |
$accessToken="4df31252fqt...PAT...." | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$accessToken))) | |
$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = "https://mycompany.visualstudio.com/" | |
$env:SYSTEM_TEAMPROJECTID = "MyProject" | |
"Getting all bulid definitions" | |
$allSuitesBuildUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0" | |
$allSuitedBuilds = Invoke-RestMethod -Uri $allSuitesBuildUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} | |
"Builds Found - Finding Match for $buildToCloneName" | |
$buildWeWant = {} | |
foreach($buildDetails in $allSuitedBuilds.value) | |
{ | |
"Checking Build" | |
$buildDetails."name" | |
if($buildDetails."name" -like $buildToCloneName) | |
{ | |
$xoutputname = $buildDetails."name" | |
"YES! Got Build $xoutputname" | |
$buildWeWant = $buildDetails | |
break | |
} | |
else | |
{ | |
"Nope!" | |
} | |
} | |
$buildId = $buildWeWant."id" | |
[int]$buildIdTest = $null | |
if(![int]::TryParse($buildId, [ref]$buildIdTest)) | |
{ | |
throw [Exception] "ERROR: NO BUILD ID FOUND" | |
} | |
"Getting the exact definition for the build" | |
# You can see this in the browser using https://mycompany.visualstudio.com/ProjectWhereBuildIs/_apis/build/definitions/{BuildNumber}?api-version=2.0 | |
$thisBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions/" + $buildId + "?api-version=2.0" | |
$thisBuildDefUrl | |
$thisBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} | |
"Assigning a new name" | |
$thisBuildDef.Name = $newBuildName | |
"Creating a clone build with name $newBuildName" | |
$defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100 | |
$newBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0" | |
$newBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop | |
$newBuildDefAsJson = $newBuildDef | ConvertTo-Json -Depth 100 | |
$newBuildDefAsJson | |
"New Build Created" | |
$newBuildDef.Name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment