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

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

问题描述

正在研究Azure Devops CI& CD.在这里,我的发行名称必须在使用标签的版本号中.在变量组的帮助下,通过向其添加标签和值来获取此信息.对于每个版本(如1.1、1.2、1.3等),这里都获得了标签值的常量(如静态值).

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.

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

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...

只需引用此线程即可自定义变量: https://github .com/MicrosoftDocs/vsts-docs/issues/666#issuecomment-386769445

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

更新:

如果您只想更新在特定变量组中定义的变量的值,则可以在构建管道中调用REST API来实现:

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:

您可以编写PowerShell脚本以调用REST API,然后添加PowerShell任务以在构建管道中运行脚本:(

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)

以下示例供您参考:

$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天全站免登陆