如何使用 Azure Devops 构建定义增加/更新变量组值? [英] How to Increase/Update Variable Group value using Azure Devops Build Definition?

查看:29
本文介绍了如何使用 Azure Devops 构建定义增加/更新变量组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Azure Devops CI&CD.在这里,我的发布名称必须在使用标签的版本号中.我在变量组的帮助下通过添加标签和值来实现这一点.对于每个版本(如 1.1、1.2、1.3 等),这里将标签值设为常量,就像静态一样.

现在我正在尝试为我的构建定义成功完成后触发的每个新版本动态增加/更新我的标签值,看起来像 1.1,1.2,2.1,2.2,3.1,3.2 等.在静态的帮助下是可能的按变量组,但我们需要手动更新它.

是否可以通过构建定义任务或其他过程来增加/更新变量组中的标签值.如果可能,请建议我如何做到这一点?"

解决方案

您可以使用

<小时>

更新 2:

您可以编写一个 PowerShell 脚本来调用 REST API,然后添加一个 PowerShell 任务来在您的构建管道中运行该脚本:(使用 OAuth 令牌访问 REST API)

以下示例供您参考:

$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/{Variable Group ID}?api-version=5.0-preview.1"写主机 $url函数 CreateJsonBody{$value = @"{"id":2,"type":"Vsts","name":"VG0926","variables":{"TEST0926":{"isSecret":false,"value":"0930"}}}"@返回 $value}$json = CreateJsonBody$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{授权 = "Bearer $env:SYSTEM_ACCESSTOKEN"}写主机新变量值:"$pipeline.variables.TEST0926.value

<小时>

更新 3:

好吧,再次测试,下面的脚本也适用于我.你可以试试,只需要相应地替换参数即可:

# Base64 对个人访问令牌 (PAT) 进行适当编码$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "","PAT here")))$url = "https://dev.azure.com/xxx/Test0924/_apis/distributedtask/variablegroups/1?api-version=5.0-preview.1"$json = '{"id":1,"type":"Vsts","name":"VG0928","variables":{"TEST0928":{"isSecret":false,"value":"0931"}}}'$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}写主机新变量值:"$pipeline.variables.TEST0928.value

Am working on Azure Devops CI&CD. Here, my release name must be in Version number using tags. Am getting this with the help of Variable Groups, by adding tags and value to it. Here am getting the tags value as constant like a static for every release like 1.1,1.2,1.3 etc.

Now am trying to increase/update my tag value dynamically for every new release triggered after completion of my Build Definition successfully which looks like 1.1,1.2,2.1,2.2,3.1,3.2 etc.It is possible with the help of statically by variable group, but manually we need to update it.

Is it possible to Increase/Update the tags value in Variable Group with the Build Definition tasks or other process.If possible, please suggest me to "How to done this?"

解决方案

You can overwrite/update the value of the variables by using the logging command to set the variables again in Azure Devops Build pipleline:

Write-Host "##vso[task.setvariable variable=testvar;]testvalue"

To increase the value dynamically, you need to use the token $(Rev:.r). You can custom the variables based on the $(Build.BuildNumber) or $(Release.ReleaseName)as they will increase the value dynamically...

Just reference this thread to custom the variables:https://github.com/MicrosoftDocs/vsts-docs/issues/666#issuecomment-386769445


UPDATE:

If you just want to update the value of the variables which defined in a specific Variable Group, then you can call REST API in build pipeline to achieve that:

PUT https://{account}.visualstudio.com/{ProjectName or ID}/_apis/distributedtask/variablegroups/{Variable Group ID}?api-version=5.0-preview.1

Content-Type: application/json

Request Body:

{"id":2,"type":"Vsts","name":"VG0926","variables":{"TEST0926":{"isSecret":false,"value":"0930"}}}


UPDATE2:

You can write a PowerShell script to call the REST API, then add a PowerShell task to run the script in your build pipeline: (Use the OAuth token to access the REST API)

Below sample for your reference:

$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/{Variable Group ID}?api-version=5.0-preview.1"
Write-Host $url

function CreateJsonBody
{

    $value = @"

{"id":2,"type":"Vsts","name":"VG0926","variables":{"TEST0926":{"isSecret":false,"value":"0930"}}}

"@

 return $value
}

$json = CreateJsonBody


$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}

Write-Host "New Variable Value:" $pipeline.variables.TEST0926.value


UPDATE3:

Well, tested again, below scripts works for me as well. You can try it, just replace the parameters accordingly:

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "","PAT here")))
$url = "https://dev.azure.com/xxx/Test0924/_apis/distributedtask/variablegroups/1?api-version=5.0-preview.1"

$json = '{"id":1,"type":"Vsts","name":"VG0928","variables":{"TEST0928":{"isSecret":false,"value":"0931"}}}'
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "New Variable Value:" $pipeline.variables.TEST0928.value 

这篇关于如何使用 Azure Devops 构建定义增加/更新变量组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆