首页 > 解决方案 > 使用 Azure DevOps REST API 更新发布定义因旧副本而失败

问题描述

我正在尝试利用 DevOps API 来更新新的发布定义。最终,我将在发布定义中添加一个新环境,但现在我只是想让更新 (PUT) 方法工作。
我已经参考了这篇文章的信息。

下面的代码获取一个现有的发布定义(id=15)​​,修改版本,删除 lastRelease 属性,然后对描述进行更改以更改某些内容。

function getreleasedefinitionrequest($definitionid, $org, $project)
{
 $requestpath = "/_apis/release/definitions/" + $definitionid + "?api-version=6.0-preview.4"
 $tokeninfo = az account get-access-token | convertfrom-json
 $token = $tokeninfo.accessToken
 $uribase = "https://vsrm.dev.azure.com/" + $org + "/" + $project
 $uri = $uribase+$requestpath
 $authheader = "Authorization=Bearer " + $token
 $result = az rest --uri $uri --headers $authheader | convertfrom-json
 return $result
}

function putreleasedefinitionrequest($bodyfile, $org, $project)
{
 $requestpath = "/_apis/release/definitions?api-version=6.0-preview.4"
 $tokeninfo = az account get-access-token | convertfrom-json
 $token = $tokeninfo.accessToken
 $uribase = "https://vsrm.dev.azure.com/" + $org + "/" + $project
 $uri = $uribase+$requestpath
 $authheader = "Authorization=Bearer " + $token
 $result = az rest --method put --uri $uri --headers "Content-Type=application/json" $authheader --body @$bodyfile | convertfrom-json
 return $result
}

$definition15 = getreleasedefinitionrequest "15" {org} {project} | select -Last 1

#bump the revision and delete the lastRelease property
$rev = [int] $definition15.revision
$rev++
$definition15.revision = $rev
$definition15.PSObject.properties.remove('lastRelease')
$definition15.description = "make up a change to the description"

$bodyfile = ".\body.json"

$body = $definition15 | convertto-json -Depth 100 | Set-Content -Path $bodyfile

#upate release definition
$putresult = putreleasedefinitionrequest $bodyfile {org} {project} | select -Last 1

az rest --method put 会引发错误代码,抱怨该版本是旧副本。我从相同版本的 API 中提取了请求,并进行了如上所述的更改。所以我认为这个新版本是管道的全新版本。

az : Bad Request({"$id":"1","innerException":null,"message":"您正在使用发布管道的旧副本。刷新您的副本并重试。","typeName": "Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000})

是否需要进行其他更改才能成功进行更新?

标签: azure-devops-rest-apiazure-cli2

解决方案


删除$rev++,我们没有,也不应该revision手动更改 的值。

注意:如果您仔细阅读帖子,您会看到I set the revision number to be the number it is currently on and it works now. 所以我们实际上不需要改变它,错误You are using an old copy of the release pipeline总是由revision.


推荐阅读