Azure DevOps 利用rest api设置variable group

我们在Azure DevOps中设置参数的时候,可以使用build,release各自的variables,但是各自的变量不能共用。此时我们需要使用variable group,它允许跨Build和Release来共享公共变量。

我们常规是做法是手动创建一个variable group,然后手动添加name和value。如果变量很多的时候,这种手动操作繁琐并且有一定的风险。下面我会演示三个过程:

我的主要参考:

https://www.danielstocker.net/updating-a-vsts-variable-group-from-the-command-line/

https://docs.microsoft.com/en-us/rest/api/azure/devops/distributedtask/variablegroups/get%20variable%20groups?view=azure-devops-server-rest-5.0

  1. 如何获取这个variable group
  2. 如何进行单个数据的更改操作
  3. 如何进行批量variable的添加

1.获取variable group中的值

获取variable group的前提是需要去申请一个personal access token,有了token后,注意自己azure devops 的account name  ,project name,填入如下脚本中 

$personalAccessToken = "YOUR_PAT_TOKEN_GOES_HERE"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$personalAccessToken)))
$accountname = "YOUR_ACCOUNT_NAME_GOES_HERE"
$variableGroupName = "YOUR_VARIABLE_NAME_GOES_HERE"
$projectName = "YOUR_PROJECT_NAME_GOES_HERE"
$vstsUri = "https://" + $accountname + ".visualstudio.com/"
# GET https://{accountName}.visualstudio.com/{project}/_apis/distributedtask/variablegroups?api-version=4.1-preview.1
# get variable groups and find our one
$call = $vstsUri + $projectName + "/_apis/distributedtask/variablegroups?api-version=5.0-preview.1"
$result = Invoke-RestMethod -Uri $call -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$groupId = (-1)
foreach($group in $result.value){
  if($group.name.Equals($variableGroupName)){
  $groupId = $group.id
  break;
  }
}
# if we can't find the group, throw an error
if($groupId -lt 0){
  throw("Couldn't find group")
}
# get full json for our group
$call = $vstsUri + $projectName + "/_apis/distributedtask/variablegroups/" + $groupId + "?api-version=5.0-preview.1"
$group = Invoke-RestMethod -Uri $call -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

就我的参数设置对应我获取的结果:

$group.variables
wb2ip       : @{value=}
wbcross     : @{value=hellodevops}
wbtest      : @{value=hello 123}

2. 对单个值进行update

下面演示如何对单个值进行修改,比如修改wbtest的值为hello 456

$group.variables.wbtest = @{"value" = "hello 456"}
$call = $vstsUri + $projectName + "/_apis/distributedtask/variablegroups/" + $groupId + "?api-version=5.0-preview.1"
$result = Invoke-RestMethod -Uri $call -Method Put -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body (ConvertTo-Json $group -Depth 10)

运行完如上,我再次get,可以发现我的wbtest的值已经发生了修改

$group.variables
wb2ip : @{value=} wbcross : @{value=hellodevops} wbtest : @{value=hello 456}

 3. 批量添加variables的操作,我们可以先将变量顶一个json字符串中,当然也可以是一个json文件。

$vgJson = @'
{
  "variables": {
    "test1": {
      "value": "hello"
    },
    "test2": {
      "value": "world"
    }
  },
  "type": "Vsts",
  "name": "test",
  "description": "Updated variable group"
}
$result = Invoke-RestMethod -Uri $call -Method Put -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $vgJson
原文地址:https://www.cnblogs.com/yangwenbo214/p/11250509.html