Powershell 在VSTS中,我可以批量更改代理队列吗?
如果我想将所有构建和发布更改为一个新的代理队列,有没有一种方法可以一次性完成 没有任何关于更改使用哪个队列的信息,也没有队列名称的变量。在VSTS中,在“设置”中的“代理池”选项卡中,我可以看到池的列表以及在这些池上运行的构建,但我看不到更改它们的方法 是否有一种程序化的方法可以一次全部更改它们,或者我必须手动完成并更改它们 我正在一个特定的构建上进行实验,然后在每个构建上完全实现它。我现在的程序流程是:获取构建定义,从具有正确版本的构建中获取队列代理,将旧版本中的队列设置为与新版本相同的队列,然后将整个内容放回VSTS中Powershell 在VSTS中,我可以批量更改代理队列吗?,powershell,build,azure-devops,Powershell,Build,Azure Devops,如果我想将所有构建和发布更改为一个新的代理队列,有没有一种方法可以一次性完成 没有任何关于更改使用哪个队列的信息,也没有队列名称的变量。在VSTS中,在“设置”中的“代理池”选项卡中,我可以看到池的列表以及在这些池上运行的构建,但我看不到更改它们的方法 是否有一种程序化的方法可以一次全部更改它们,或者我必须手动完成并更改它们 我正在一个特定的构建上进行实验,然后在每个构建上完全实现它。我现在的程序流程是:获取构建定义,从具有正确版本的构建中获取队列代理,将旧版本中的队列设置为与新版本相同的队列,
$result = Invoke-RestMethod -Method GET -Uri $uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$result3 = Invoke-RestMethod -Method GET -Uri $uri3 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$q = $result3.queue
$result.queue = $q
$result | ConvertTo-JSON
Invoke-RestMethod -Method PUT $uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $result
我发现了错误
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name:
如何从GET正确配置PUT?REST API只能更改特定生成定义的代理队列 因此,您需要获取团队项目的所有生成定义,然后循环更改生成定义的代理队列。详情如下:
GET https://account.visualstudio.com/DefaultCollection/project/_apis/distributedtask/queues?api-version=3.0-preview.1
在列出的代理队列中,找到要使用的代理队列的id
例如,如果要使用Default
代理,可以在以下步骤中将id
用作7
GET ttps://account.visualstudio.com/DefaultCollection/project/_apis/build/definitions?api-version=2.0
您可以获得每个生成定义的定义id
、revision
和定义name
"repository": {
"properties": {
"cleanOptions": "0",
"labelSources": "0",
"labelSourcesFormat": "$(build.buildNumber)",
"reportBuildStatus": "true",
"gitLfsSupport": "false",
"skipSyncSource": "false",
"checkoutNestedSubmodules": "false",
"fetchDepth": "0"
},
"id": "e89075b8-d7bd-4c3f-b24c-23276d89e8ec",
"type": "TfsGit",
"name": "vs2017",
"url": "https://marinaliu.visualstudio.com/Git2/_git/vs2017",
"defaultBranch": "refs/heads/master",
"clean": "true",
"checkoutSubmodules": false
}
然后,您可以访问代理队列:
PUT https://account.visualstudio.com/DefaultCollection/project/_apis/build/definitions/definitionID?api-version=2.0
Application/json as:
{
"id": 6,
"revision": 356,
"queue": {
"id": 7
},
"repository": {
"properties": {
"cleanOptions": "0",
"labelSources": "0",
"labelSourcesFormat": "$(build.buildNumber)",
"reportBuildStatus": "true",
"gitLfsSupport": "false",
"skipSyncSource": "false",
"checkoutNestedSubmodules": "false",
"fetchDepth": "0"
},
"id": "e89075b8-d7bd-4c3f-b24c-23276d89e8ec",
"type": "TfsGit",
"name": "vs2017",
"url": "https://marinaliu.visualstudio.com/Git2/_git/vs2017",
"defaultBranch": "refs/heads/master",
"clean": "true",
"checkoutSubmodules": false
}
}
现在,当前项目中的构建定义都将代理队列更改为默认代理(id
=7
,如示例所示)。您可以使用类似的方法更改其他生成/发布定义RESTAPI只能更改特定生成定义的代理队列 因此,您需要获取团队项目的所有生成定义,然后循环更改生成定义的代理队列。详情如下:
GET https://account.visualstudio.com/DefaultCollection/project/_apis/distributedtask/queues?api-version=3.0-preview.1
在列出的代理队列中,找到要使用的代理队列的id
例如,如果要使用Default
代理,可以在以下步骤中将id
用作7
GET ttps://account.visualstudio.com/DefaultCollection/project/_apis/build/definitions?api-version=2.0
您可以获得每个生成定义的定义id
、revision
和定义name
"repository": {
"properties": {
"cleanOptions": "0",
"labelSources": "0",
"labelSourcesFormat": "$(build.buildNumber)",
"reportBuildStatus": "true",
"gitLfsSupport": "false",
"skipSyncSource": "false",
"checkoutNestedSubmodules": "false",
"fetchDepth": "0"
},
"id": "e89075b8-d7bd-4c3f-b24c-23276d89e8ec",
"type": "TfsGit",
"name": "vs2017",
"url": "https://marinaliu.visualstudio.com/Git2/_git/vs2017",
"defaultBranch": "refs/heads/master",
"clean": "true",
"checkoutSubmodules": false
}
然后,您可以访问代理队列:
PUT https://account.visualstudio.com/DefaultCollection/project/_apis/build/definitions/definitionID?api-version=2.0
Application/json as:
{
"id": 6,
"revision": 356,
"queue": {
"id": 7
},
"repository": {
"properties": {
"cleanOptions": "0",
"labelSources": "0",
"labelSourcesFormat": "$(build.buildNumber)",
"reportBuildStatus": "true",
"gitLfsSupport": "false",
"skipSyncSource": "false",
"checkoutNestedSubmodules": "false",
"fetchDepth": "0"
},
"id": "e89075b8-d7bd-4c3f-b24c-23276d89e8ec",
"type": "TfsGit",
"name": "vs2017",
"url": "https://marinaliu.visualstudio.com/Git2/_git/vs2017",
"defaultBranch": "refs/heads/master",
"clean": "true",
"checkoutSubmodules": false
}
}
现在,当前项目中的构建定义都将代理队列更改为默认代理(id
=7
,如示例所示)。您可以使用类似的方法更改其他生成/发布定义请尝试引用此代码:
param(
[string]$uri,
[string]$uri3
)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f 'test','XXX')))
$result = Invoke-RestMethod -Method GET -Uri $uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$result3 = Invoke-RestMethod -Method GET -Uri $uri3 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
foreach($bd in $result.value){
$detailbuild=Invoke-RestMethod -Method GET -Uri "$($bd.url)?api-version=2.0" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$detailbuild.queue=$result3
$bdJson=$detailbuild| ConvertTo-JSON -Depth 20
$updateDefUrl="$($bd.url)?api-version=2.0"
Write-Host $bdJson
$resultUpdaet= Invoke-RestMethod -Method PUT -Uri $updateDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $bdJson
}
参数:
-uri'https://XXX.visualstudio.com/DefaultCollection/[项目]/_api/build/definitions?api版本=2.0&type=build'-uri3'https://XXX.visualstudio.com/DefaultCollection/[project]/\u api/distributedtask/queues/[queueid]?api版本=3.0-preview.1'
请尝试引用以下代码:
param(
[string]$uri,
[string]$uri3
)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f 'test','XXX')))
$result = Invoke-RestMethod -Method GET -Uri $uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$result3 = Invoke-RestMethod -Method GET -Uri $uri3 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
foreach($bd in $result.value){
$detailbuild=Invoke-RestMethod -Method GET -Uri "$($bd.url)?api-version=2.0" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$detailbuild.queue=$result3
$bdJson=$detailbuild| ConvertTo-JSON -Depth 20
$updateDefUrl="$($bd.url)?api-version=2.0"
Write-Host $bdJson
$resultUpdaet= Invoke-RestMethod -Method PUT -Uri $updateDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $bdJson
}
参数:
-uri'https://XXX.visualstudio.com/DefaultCollection/[项目]/_api/build/definitions?api版本=2.0&type=build'-uri3'https://XXX.visualstudio.com/DefaultCollection/[project]/\u api/distributedtask/queues/[queueid]?api版本=3.0-preview.1'
我用一些脚本更新了原始帖子。我试图按照您的建议或多或少地完成这项工作,但我无法理解JSON更新构建的语法。我也一直在使用补丁而不是PUT。你最好用PUT rest api主体的内容定义一个变量。然后从GetRESTAPI中获取部分来替换body变量。所以每次我都必须创建整个构建并重新加载它?仅仅改变一个变量就需要花费大量的时间和精力。难道没有办法只运行一个补丁并更新一个变量吗?如果我每次都要创建整个构建,那么将每个构建的数据保存在存储库中,然后我可以单独更新它们可能是有意义的。不,补丁方法不适用于此rest api。您应该使用PUT方法。这目前还不够。实际上,我只是写了一些东西来实现你的建议(在阅读本文之前),但在更新时,我丢失了一些以前的属性,因为个人get无法获得构建的所有细节。我被留下来手动更新构建队列。我用一些脚本更新了原始帖子。我试图按照您的建议或多或少地完成这项工作,但我无法理解JSON更新构建的语法。我也一直在使用补丁而不是PUT。你最好用PUT rest api主体的内容定义一个变量。然后从GetRESTAPI中获取部分来替换body变量。所以每次我都必须创建整个构建并重新加载它?仅仅改变一个变量就需要花费大量的时间和精力。难道没有办法只运行一个补丁并更新一个变量吗?如果每次我都要创建整个构建,那么将每个构建的数据保存在存储库中,然后我将