首页 > 解决方案 > 我可以批量更新 VSTS 构建管道定义吗?

问题描述

我正在从本地 TFS 实例迁移到 VSTS。我有很多迁移到 VSTS 的构建管道(vNext 构建定义),但现在我必须更新它们以使用特定的代理。

UI 和命令行客户端中都没有可用的选项。

我是否错过了一个可用的选项,以便我可以一次更新它们?

标签: migrationazure-devopsazure-pipelines

解决方案


根据我对 Manuel 所做的迁移工作(参考 Jesse 提到的帖子),我提供了一些可用的脚本来获取 TFS 队列,然后使用它来更新 VSTS 构建定义。

  • 读取队列FromTfs.ps1
  • 修复-BuildDefinitions.ps1

这两个脚本都需要一个参数 PersonalAccesToken - 一个是您所针对的 VSTS 帐户的 PAT,另一个是针对 TFS 环境的 PAT。

第一个脚本可帮助您获取包含所有 TFS 队列的 queues.json 文件。第二个脚本迭代您要更新构建定义的 VSTS 项目。脚本应该是不言自明的。

# Get all queues and based on previous names get the id's
    (Invoke-RestMethod `
            -Uri "https://$account.visualstudio.com/$_/_apis/distributedtask/queues" `
            -Headers @{Authorization = "Basic $auth"; Accept = "application/json; api-version=3.2-preview" } `
            -Method Get `
            -ContentType "application/json" -Verbose).value | % { $vstsqueues[$_.name] = $_.id }

    # get all the builds
    $builds = (Invoke-RestMethod `
            -Uri "https://$account.visualstudio.com/$_/_apis/build/definitions" `
            -Headers @{Authorization = "Basic $auth"; Accept = "application/json; api-version=4.1-preview.6" } `
            -Method Get `
            -ContentType "application/json").value

        # get the full build definition
        $build = Invoke-RestMethod `
            -Uri $_.url `
            -Headers @{Authorization = "Basic $auth"; Accept = "application/json; api-version=4.1-preview.6" } `
            -Method Get `
            -ContentType "application/json" 

        # get queue
        $queuename = $tfsqueues[$_.queue.id]
        Write-Output "    queue name: $queuename"

        # update build
        $build.queue = @{ id = $vstsqueues[$queuename] }

        # post changes
        Invoke-RestMethod `
            -Uri $_.url `
            -Headers @{Authorization = "Basic $auth"; Accept = "application/json; api-version=4.1-preview.6" } `
            -Method Put `
            -ContentType "application/json" `
            -Body ($build | ConvertTo-Json -Depth 100 -Compress) | Out-Null
    }
}

在这个文件中描述。https://github.com/JasperGilhuis/VSTS-RestAPI/blob/master/README.md#update-vsts-build-definitions-based-on-tfs-queues

查看存储库中的 Builds 文件夹https://github.com/JasperGilhuis/VSTS-RestAPI/tree/master/Builds


推荐阅读