使用 Azure DevOps REST 在给定范围内更新管道变量 api

Updating pipeline variables at a given scope using Azure DevOps REST api

我目前正在尝试更新范围内的管道变量,DEV 但是,我很难更新该变量。是否可以在“发布”以外的范围内更新变量?如果是这样,如何?下面是我使用的代码和我收到的错误。

let reqLink = ' https://vsrm.dev.azure.com/'+ organization +'/'+project+'/_apis/release/releases?api-version=5.1';

let reqBody = {
    "definitionId": definitionId,
    "variables": {
      "someVar": 
        { 
          "value": "foo",
          "scope": "DEV"
        }
    }
  };
  
sendHttpRequest('POST',reqLink,reqBody).then(response => {
  let data = JSON.parse(response);
  console.log(data);
});

这是我收到的错误:

{"$id":"1","innerException":null,"message":"Variable(s) someVar do not exist in the release pipeline at scope: Release

作用域变量不是在根级别上定义的。但在舞台层面。所以你必须在这里修改:

此处变量 SomeVer 的范围为 Stage 1。实现此目的的最简单方法是使用 GET 命中端点,在 json 上操作并使用 PUT.

命中端点

我注意到你正在点击 release/releases 而你应该点击相当具体的版本 release/releases/{releaseId}。或者您的目标可能是更新 definition 本身?

Is it possible to update the variable at a scope other than "Release"? If so, how?

答案是肯定的。

您使用的 REST API 用于创建发布,如果您想更新发布管道,请使用:

PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=6.0-preview.4

REST 的请求正文API 可能需要发布管道的详细信息。使用下面的 REST API 来获取它。

GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=6.0-preview.4

然后可以修改它的response body作为第一个REST的request body API.

属性 variables 没有一个名为 scope 的 属性。如果要将变量从 'Release' 范围更新到阶段范围,则需要删除变量在 variables 中的原始定义并在目标环境中重新定义它。这是一个例子。

原文:

{
    ...
    "variables": {
        "somevar": {
            "value": "foo"
        }
    },
    ...
};

修改后的脚本:

{
    ...
    "environments": [
        {
            "id": {stage id},
            "name": DEV
            ...
            "variables": {
                "somevar": {
                    "value": "foo",
                },
            ...
        }
    ],
    ...
    "variables": {},
    ...
};

总结如下:要更改变量的范围,只需将变量定义移动到目标范围即可。