TFS2015 REST API构建定义更新 [英] TFS2015 REST API Build definition update
问题描述
我正在尝试使用PowerShell通过REST API更新构建定义.
I'm trying to update a build definition through REST API with PowerShell.
使用的脚本是:
$url = "http://tfs:8080/tfs/collection/project/_apis/build/definitions/$($buildId)?api-version=2.0"
$obj = Invoke-RestMethod -Uri $url2 -Method Get -ContentType "application/json" -UseDefaultCredentials
$json = ConvertTo-Json $obj
Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -UseDefaultCredentials
首先,我尝试使用新的空定义,但出现以下错误:
First I've tried with a new empty definition and I've got the below error:
该集合必须至少包含一个元素.参数名称: definition.Options.Inputs
The collection must contain at least one element.Parameter name: definition.Options.Inputs
因此,我添加了一个额外的代码以从返回的json中删除选项"部分:
So I added an extra code to remove the "options" part from the returned json:
if($obj.options -ne $null){
$obj.options = $null }
,更新就可以了.但是,当我在生产环境中使用真实的"现有构建定义上的代码时,会出现另一个错误:
and the update worked. But when I'm using the code on an "real" existing build definition that is in production then I get another error:
该集合必须至少包含一个元素. 参数名称:define.RetentionRules.Rule.Branches.Filter
The collection must contain at least one element. Parameter name: definition.RetentionRules.Rule.Branches.Filter
我正在使用TFS2015 Update 3.
I'm using TFS2015 Update 3.
为什么不通过REST API对构建定义进行简单的更新(未经任何修改)?
Why is not working a simple update (without any modification) of a build definition via REST API?
推荐答案
需要更改行$json = ConvertTo-Json $obj
以包含最小值为3
的-Depth
自变量.默认值为2,由于嵌套,将值从对象转换为Json时会丢失.更具体地说,发生的是将值从数组转换为简单的字符串.
The line $json = ConvertTo-Json $obj
needs changed to include the -Depth
argument with a minimum value of 3
. The default is 2 and because of the nesting the values are lost when converted from an object to Json. More specifically what happens is the values get converted from an array to a simple string.
"retentionRules": [
{
"branches": "+refs/heads/*",
"artifacts": "build.SourceLabel",
"daysToKeep": 10,
"minimumToKeep": 1,
"deleteBuildRecord": true,
"deleteTestResults": true
}
]
使用depth参数
"retentionRules": [
{
"branches": [
"+refs/heads/*"
],
"artifacts": [
"build.SourceLabel"
],
"daysToKeep": 10,
"minimumToKeep": 1,
"deleteBuildRecord": true,
"deleteTestResults": true
}
]
您会看到branches
和artifacts
值从字符串更改为具有适当深度值的数组.
You will see that the the branches
and artifacts
values change from a string to an array with the proper depth value.
$url = "http://tfs:8080/tfs/collection/project/_apis/build/definitions/$($buildId)?api-version=2.0"
$obj = Invoke-RestMethod -Uri $url2 -Method Get -ContentType "application/json" -UseDefaultCredentials
$json = ConvertTo-Json $obj -Depth 3
Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -UseDefaultCredentials
这篇关于TFS2015 REST API构建定义更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!