• 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
  • 相关阅读:
    I第五章:(1)Hystrix 断路器
    第四章—Netty:(1)Netty概述
    I第四章:(1)Feign 负载均衡
    第三章—Java NIO编程:(9)AIO及三种IO对比
    I第六章:(1)Zuul路由网关
    第五章—Netty高性能架构设计:(2)Reactor线程模式
    Pytest动态为用例添加mark标记
    Postman使用简介
    Fiddler抓包使用简介
    使用正则解析Python赋值及函数调用表达式
  • 原文地址:https://www.cnblogs.com/yangwenbo214/p/11250509.html
Copyright © 2020-2023  润新知