0
votes

If I want to change all of my builds and releases to a new agent queue, is there a way to do that all at once?

The Agent Queue API doesn't have anything about changing which queue is used, and the Build API doesn't have a variable for the queue name. In VSTS, in the Agent Pools tab in settings, I can see a list of pools and the builds that were run on them, but I don't see a way of changing them.

Is there a programmatic way to change them all at once, or do I have to go through and change them all manually?

I'm experimenting on a specific build before fully implementing it in on every build. My program flow right now is: get the build definition, get the queue agent from a build that has the right one, set the queue in the old version equal to the new one, and PUT the whole thing back in 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

I'm getting the error

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name:

How do I properly configure the PUT from the GET?

2
Do you solve the issue with my solution? - starian chen-MSFT
I did not, we're going through some maintenance and the build agents have been offline. I think ultimately I'm going to have to change the whole way build steps are made in the near future - user3364161
Feel free to try it if you have time, just need to specify the API urls. - starian chen-MSFT

2 Answers

2
votes

The REST API can only change the agent queue for a certain build definition.

So you need to get all the build definitions for a team project, and then loop to change the agent queue for the build definitions. Details are below:

  1. Get the id for the agent queue you want to apply for the build definitions

    Use the get a list of queues REST API:

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

    In the listed agent queues, find the id of the agent queue you want to use.

    Enter image description here

    Such as if you want to use Default agent, you can use the id as 7 in the following step.

  2. Get build definitions for a project

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

    You can get each build definition’s definition id, revision and definition name.

  3. Loop each build definition and change the agent queue. Updating build definition, it needs build revision and repository information. So you should to get the build definition repository by the get a build definition REST API:

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

    Then you can get repository information like:

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

    Then you can the update build definition agent queue:

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

    Now build definitions in the current project are all changing the agent queues as the default agent (id=7 as in the example). You can use a similar way to change other build/release definitions.

0
votes

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'