在VSTS中,我可以批量更改代理队列吗?

时间:2017-10-09 15:35:00

标签: powershell build azure-devops

如果我想将所有构建版本和版本更改为新的代理程序队列,是否有办法一次性完成所有操作?

Agent Queue API没有关于更改使用哪个队列的任何内容,Build API没有队列名称的变量。在VSTS中,在设置中的代理池选项卡中,我可以看到池的列表以及在它们上运行的构建,但我没有看到更改它们的方法。

是否有一种以编程方式同时更改所有内容,或者我是否必须手动更改它们?

我正在尝试一个特定的构建,然后在每个构建中完全实现它。我现在的程序流程是:获取构建定义,从具有正确版本的构建中获取队列代理,将旧版本中的队列设置为等于新版本,然后将整个事件放回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?

2 个答案:

答案 0 :(得分:2)

REST API只能更改某个构建定义的代理队列。

因此,您需要获取团队项目的所有构建定义,然后循环以更改构建定义的代理队列。详情如下:

  1. 获取要应用于构建定义的代理队列的ID

    使用get a list of queues REST API:

    GET https://account.visualstudio.com/DefaultCollection/project/_apis/distributedtask/queues?api-version=3.0-preview.1
    

    在列出的座席队列中,找到您要使用的座席队列的id

    Enter image description here

    例如,如果您要使用Default代理,则可以在以下步骤中将id用作7

  2. 项目的
  3. Get build definitions

    GET ttps://account.visualstudio.com/DefaultCollection/project/_apis/build/definitions?api-version=2.0
    

    您可以获取每个构建定义的定义idrevision和定义name

  4. 循环每个构建定义并更改代理队列。更新构建定义,它需要构建版本和存储库信息。因此,您应该通过get a build definition REST API

    获取构建定义存储库
    GET https://account.visualstudio.com/DefaultCollection/project/_apis/build/definitions/definitionID?api-version=2.0
    

    然后您可以获取存储库信息,如:

    "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
    }
    

    然后你可以update build definition代理队列:

    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,如示例所示)。您可以使用类似的方法更改其他构建/发布定义。

答案 1 :(得分:0)

Try to refer this code:

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
}

Arguments:-uri 'https://XXX.visualstudio.com/DefaultCollection/[project]/_apis/build/definitions?api-version=2.0&type=build' -uri3 'https://XXX.visualstudio.com/DefaultCollection/[project]/_apis/distributedtask/queues/[queueid]?api-version=3.0-preview.1'