Azure DevOps保留期PATCH请求失败问题排查求助
Azure DevOps PATCH请求更新保留租期失败问题
已成功实现Azure DevOps多阶段构建与发布流水线的变量共享,但调用PATCH API更新构建保留租期时失败,错误提示保留租期更新内容为null,请问这是Bug吗?我的请求格式是否正确?
执行的PowerShell任务代码
- task: PowerShell@2 condition: and(succeeded(), not(canceled())) name: RetainOnSuccess displayName: Retain on Success inputs: failOnStderr: true targetType: 'inline' script: | $contentType = "application/json"; $headers = @{ Authorization = 'Bearer $(System.AccessToken)' }; $rawRequest = @{ daysValid = 100; protectPipeline = false; }; $request = ConvertTo-Json @($rawRequest); $uri = "https://dev.azure.com/testing-ci/$(System.TeamProject)/_apis/build/retention/leases/$(myVarfromStage)?api-version=7.1-preview.2"; Invoke-RestMethod -uri $uri -method PATCH -Headers $headers -ContentType $contentType -Body $request;
DevOps返回的错误信息
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\6ee1c3fd-e406-417e-bf9f-d860a21d1647.ps1'" ##[error]Invoke-RestMethod : {"$id":"1","innerException":null,"message":"The retention lease update is null.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RetentionLeaseUpdateIsNull, Microsoft.TeamFoundation.Build2.WebApi","typeKey":"RetentionLeaseUpdateIsNull","errorCode":0,"eventId":3000} At D:\a\_temp\6ee1c3fd-e406-417e-bf9f-d860a21d1647.ps1:8 char:1 + Invoke-RestMethod -uri $uri -method PATCH -Headers $headers -ContentT ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand ##[error]PowerShell exited with code '1'.
问题分析与解决方案
这不是Bug,是请求体格式不符合API要求导致的。
错误提示The retention lease update is null说明API无法识别传入的请求内容,问题出在代码中将请求对象包装成了数组:
$request = ConvertTo-Json @($rawRequest);
Azure DevOps的Retention Lease PATCH API期望接收单个RetentionLeaseUpdate对象,而非数组。只需去掉数组包装,直接将单个对象转为JSON即可:
修正后的代码片段:
$rawRequest = @{ daysValid = 100; protectPipeline = false; }; $request = ConvertTo-Json $rawRequest; # 移除@()数组包装
修改后重新执行任务,PATCH请求就能正确识别更新内容,解决该错误。
内容的提问来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

