PAYATHON 2026

Azure DevOps AccessToken 返回空代理池但流水线可使用该池

数据小陈

构建服务身份没有权限通过 REST API 读取 agent pool 元数据,这和流水线能否使用这个池没有关系。先到组织/集合级 Agent Pools > 目标池 > Security,给该构建服务身份(或 Project Collection Build Service Accounts 组)至少 Reader/View 权限;只加项目级 Agent Pool 安全设置通常不够。如果只需要查目标池,拿到 poolId 后直接查该池及其 agents,就不用遍历所有池。

原因

_apis/distributedtask/pools 返回的是“当前令牌身份可见”的池。流水线 YAML 里的 pool 授权只说明排队时可以使用该池,不代表构建服务账号能读取池列表。System.AccessToken 是项目构建服务身份的令牌,访问组织/集合级 Agent Pools REST API 时,仍受这个身份在 Agent Pools 安全设置里的权限限制。项目级 Agent Pool -> <pool> -> Security Settings 与集合/组织级 Agent Pools 安全设置属于不同层级;如果 API 走集合/组织级端点,项目级授权未必能让列表接口返回该池。另外,poolName 过滤前也会先按可见性过滤,没有 View 权限时看到的就是 count: 0。

解决步骤

  1. 在 Azure DevOps 的 Organization settings / Collection settings > Agent pools 中选目标池,进入 Security。
  2. 添加流水线实际使用的构建服务身份。常见的是 Project Collection Build Service Accounts 组,或具体项目的 {ProjectName} Build Service ({OrganizationName})。先通过 _apis/connectiondata 确认令牌身份,不要只凭项目名猜。
  3. 至少给 Reader 或 View agent pool / View agent pools 权限。如果要列出所有池,需要确保身份对每个想看到的池都有 View 权限;只想看目标池,就只给目标池授权。
  4. 检查项目侧 Project settings > Pipelines > Agent pools 是否已授权该池给项目;这主要影响排队使用,REST 读取并不依赖它,但建议一并确认。
  5. 在脚本里对 poolName 做 URL 编码,并打印 $who 验证身份。示例:
$collectionUri = "$(System.CollectionUri)".TrimEnd('/')
$headers = @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}

$who = Invoke-RestMethod `
    -Uri "$collectionUri/_apis/connectiondata?api-version=7.1-preview" `
    -Headers $headers

Write-Host "Authenticated identity: $($who.authenticatedUser.providerDisplayName)"
Write-Host "Authenticated id: $($who.authenticatedUser.id)"

$allPools = Invoke-RestMethod `
    -Uri "$collectionUri/_apis/distributedtask/pools?api-version=7.1" `
    -Headers $headers

Write-Host "Pools visible to this identity (count: $($allPools.count)):"
$allPools.value | ForEach-Object {
    Write-Host " id=$($_.id) name='$($_.name)'"
}

$poolName = [uri]::EscapeDataString($env:TARGET_POOL)
$pool = Invoke-RestMethod `
    -Uri "$collectionUri/_apis/distributedtask/pools?poolName=$poolName&api-version=7.1" `
    -Headers $headers

Write-Host "Filtered lookup response: $($pool | ConvertTo-Json -Depth 5 -Compress)"

查询目标池中的代理

如果已经知道 pool ID,可以直接查该池的 agents,并带上 capabilities,不必先列出所有池。示例:

$poolId = 123
$agents = Invoke-RestMethod `
    -Uri "$collectionUri/_apis/distributedtask/pools/$poolId/agents?includeCapabilities=true&api-version=7.1" `
    -Headers $headers

$agents.value | ForEach-Object {
    Write-Host "agent=$($_.name) enabled=$($_.enabled) status=$($_.status)"
    $_.systemCapabilities | ConvertTo-Json -Depth 5 -Compress | Write-Host
    $_.userCapabilities | ConvertTo-Json -Depth 5 -Compress | Write-Host
}

看返回的 enabled、status 和 capabilities,可以判断有没有在线且满足条件的自托管代理。列 agents 仍需要目标池上的 View 权限;没有权限时,接口可能返回空结果或报错。

注意事项

  • 用 PAT 测试时,需要给 PAT 勾选 Agent Pools 的读取权限,并且 PAT 所属用户也要在目标池安全设置中有 View 权限;PAT 能读到而 System.AccessToken 读不到,通常说明构建服务身份权限不足。
  • 本场景只要通过环境变量注入 System.AccessToken 即可,调用这个 REST API 不需要额外开启 persistCredentials。
  • 如果 System.CollectionUri 末尾已经带 /,拼接时别产生双斜杠;上面的示例用 TrimEnd('/') 统一处理。
  • 目标池名包含空格或特殊字符时,poolName 必须做 URL 编码。不过它通常不是 count: 0 的主因;遇到 count: 0,还是先检查组织/集合级 Agent Pools 的 Security。

备注:内容仅供参考。