如何正确传递变量&源版本到API 2.0 VNext在TFS 2015中构建

时间:2016-08-03 15:24:32

标签: json powershell tfs tfsbuild tfs2015

我很难找到将定义的变量构建定义参数传递给使用TFS 2015的新API 2.0构建引擎的正确方法。我正在使用 TFS 2015 Update 3内部部署

我用PowerShell触发了一个看起来像这样的POST:

$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

powershell脚本正在成功启动定义。但是,我还没有成功: - 传入我想要运行的特定源版本(例如C12345) - 传递自定义变量值

此外: 如果您知道传递参数的正确方法,例如要从源映射的文件夹(以允许动态选择不同的分支),那么这将有所帮助。

我评估的当前资源:

1 个答案:

答案 0 :(得分:2)

REST API的正文部分应如下所示:

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

然后您可以指定sourceBranch和SourceVersion。

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

一个例子:

$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",
   }
"@