简体   繁体   中英

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

I am currently trying to update a pipeline variable at the scope, DEV however, I am having hard time updating that variable. Is it possible to update the variable at a scope other than "Release"? If so, how? Below is the code that I used and the error that I received.

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);
});

This is the error that I am receiving:

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

Scoped variables are defined not on the root level. But on stage level. So you must modify this here:

在此处输入图像描述

Here you have variable SomeVer scoped to Stage 1 . The easiest way to achieve this will be hit endpoint with GET , manipulate on json and hit endpoint with PUT .

And what I noticed you are hiting release/releases whereas you should hit rather specific release release/releases/{releaseId} . Or maybe your goal is to update definition itself?

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

The answer is yes.

The REST API you use is to create a release, if you want to update a release pipeline, using:

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

The request body of the REST API may need detailed information of the release pipeline. Use the following REST API to get it.

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

Then you can modify its response body and use it as the request body of the first REST API.

The property variables doesn't have a property called scope . If you want to update a variable from 'Release' scope to a stage scope, you need to delete the variable's original definition in variables and redefinite it in target environment. Here is an example.

Original script:

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

The modified script:

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

Here is the summary: To change the scope of a variable, just move the variable definition to target scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM