SheldonH SheldonH - 3 months ago 51
JSON Question

How to correctly pass variables & source version to API 2.0 VNext Build in TFS 2015

I'm having difficulty finding how the correct way to pass defined variables and build definition arguments to the new API 2.0 build engine with TFS 2015. I'm using TFS 2015 Update 3 on-premise .

I've triggered a POST with powershell that looks like this:

$Build_Definition_ID = 1234
$TFSInstanceURL = 'http://tfsservername:port/tfs'
$ProjectCollection = 'CollectionName'
$TeamProject = 'ProjectName'
$Changeset = "12345"
$UserName = "$env:USERDOMAIN\$env:USERNAME"
$UserNamePartial = $env:USERNAME


$body = @"
{
"definition": {
"id": "$Build_Definition_ID"
}
}
"@

$baseUri = $TFSInstanceURL+"/"+$ProjectCollection+"/"+$TeamProject+"/_apis/build"
$postUri = $baseUri+"/builds?api-version=2.0"

##Create a new PSCredential based on username/password
$User = 'foo\bar'
$Password = 'examplepass'
$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($User, $securePassword)

### Queue a build ###
##Call the REST API for TFS that does a POST request to queue a build with the body of the request to be the build definition
$buildResponse = Invoke-RestMethod -Method POST -Credential $credential -ContentType application/json -Uri $postUri -Body $body

#Write-Host (ConvertTo-Json $buildResponse)
#ConvertTo-Json $buildResponse | out-file -FilePath $Changeset-ResponseJson.json -Force


The powershell script is successfully launching the definition. However, I'm still not successfully:
- Passing in the specific source version I want to run against (example C12345)
- Passing in the custom variable values

Additionally:
If you know of the proper way to pass in the arguments such as the folder to map from source (to allow dynamically choosing different branches) then this would help.

Current resources I've evaluated:


Answer

The body part for the REST API should look like:

{
  "definition": {
    "id": 28
  },
  "sourceBranch": "$/xxxx/xxxx",
  "SourceVersion": "Cxxxx",
}

Then you can specify the sourceBranch and SourceVersion.

===================================================================

An example:

$Build_Definition_ID = '28'
$TFSInstanceURL = 'http://tfsservername:port/tfs'
$ProjectCollection = 'DefaultCollection'
$TeamProject = 'TestCase'
$Changeset = "C139"
$sourceBranch = "$/TestCase/TestCaseProject-branch"

$body = @"
   {
       "definition": {
           "id": "$Build_Definition_ID"
       },
       "sourceBranch": "$sourceBranch",
       "SourceVersion": "$Changeset",
   }
"@