You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多阶段Pipeline更新Lease时遇PATCH方法不支持错误求助

多阶段Pipeline更新Lease报PATCH方法不支持的解决方案

核心问题定位

你遇到的{"Message":"The requested resource does not support http method 'PATCH'."}错误,90%的概率是更新Lease的API端点URL写错了——创建Lease和更新Lease的接口URL结构完全不同,很多人会直接复用创建时的URL导致报错。

创建Lease的接口是不带ID的:

https://dev.azure.com/{org}/{proj}/_apis/build/leases?api-version=7.2

但更新Lease必须指定具体的Lease ID,正确的端点格式是:

https://dev.azure.com/{org}/{proj}/_apis/build/leases/{leaseId}?api-version=7.2

排查与修复步骤

  1. 检查更新脚本的URL拼接
    确保你在Build阶段创建Lease后,已经把返回的leaseId保存为输出变量,在更新阶段正确带入URL:

    # Build阶段创建Lease并保存ID
    $createResponse = Invoke-RestMethod -Uri $createUrl -Method Post -Headers $headers -Body $body
    Write-Host "##vso[task.setvariable variable=leaseId;isOutput=true]$($createResponse.id)"
    
    # 更新阶段使用保存的leaseId拼接URL
    $updateUrl = "https://dev.azure.com/{org}/{proj}/_apis/build/leases/$(leaseId)?api-version=7.2"
    Invoke-RestMethod -Uri $updateUrl -Method Patch -Headers $headers -Body $updateBody
    
  2. 验证权限与Token配置

    • 确认Pipeline服务账号拥有Build (Edit)权限(项目设置→权限→找到对应服务账号,配置Build权限)
    • YAML Pipeline需开启"Allow scripts to access the OAuth token"(编辑Pipeline→右上角设置→勾选该选项)
  3. 核对API版本一致性
    确保更新请求的api-version和创建时一致,且为支持PATCH的版本(7.2及以上)

可直接复用的完整示例

stages:
- stage: Build
  jobs:
  - job: CreateRetentionLease
    steps:
    - powershell: |
        $orgUri = "$(System.TeamFoundationCollectionUri)"
        $projName = "$(System.TeamProject)"
        $buildId = "$(Build.BuildId)"
        $headers = @{ Authorization = "Bearer $(System.AccessToken)" }
        
        # 创建Lease的请求体
        $createBody = @{
          definitionId = $(Build.DefinitionId)
          runId = $buildId
          ownerId = "PipelineRetentionLease"
          daysValid = 7
          retainBuild = $true
        } | ConvertTo-Json

        $createUrl = "$orgUri$projName/_apis/build/leases?api-version=7.2"
        $response = Invoke-RestMethod -Uri $createUrl -Method Post -Headers $headers -Body $createBody -ContentType "application/json"
        # 输出leaseId供后续阶段使用
        Write-Host "##vso[task.setvariable variable=leaseId;isOutput=true]$($response.id)"
      name: CreateLeaseTask
      displayName: 创建构建保留Lease

- stage: UpdateRetentionLease
  dependsOn: Build
  jobs:
  - job: UpdateLease
    variables:
      # 引用Build阶段输出的leaseId
      leaseId: $[ stageDependencies.Build.CreateRetentionLease.outputs['CreateLeaseTask.leaseId'] ]
    steps:
    - powershell: |
        $orgUri = "$(System.TeamFoundationCollectionUri)"
        $projName = "$(System.TeamProject)"
        $headers = @{ Authorization = "Bearer $(System.AccessToken)" }
        
        # 更新Lease的请求体
        $updateBody = @{
          daysValid = 30
          retainBuild = $true
        } | ConvertTo-Json

        $updateUrl = "$orgUri$projName/_apis/build/leases/$(leaseId)?api-version=7.2"
        Invoke-RestMethod -Uri $updateUrl -Method Patch -Headers $headers -Body $updateBody -ContentType "application/json"
      displayName: 更新Lease保留时长

额外测试方案

如果脚本还是报错,建议用Postman直接调用API验证:

  1. 调用创建Lease接口拿到leaseId
  2. 用PATCH请求访问带leaseId的端点,确认接口本身可用,排除Pipeline脚本的问题

内容的提问来源于stack exchange,提问作者thepaulmacca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 18:15:05