8
votes

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?"

5

5 Answers

10
votes

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"}}}

enter image description here


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 
2
votes

I used this task to update the value of my variables inside my group.

Shared variable updater (preview)

Dont forget to set those settings :

  1. Requires 'Allow scripts to access the OAuth token' in agent job additional options

  2. Set administrator role to 'Project Collection Build Service' in the variable group.

In case of using a YAML pipeline

When using a YAML pipeline, the OAuth token is automatically added (no need for step 1 above), but requires a bit of work to make accessible for the powershell script. Use the guidance here to be able to use the token.

0
votes

You can overwrite variables using the REST API with a PowerShell task without needing to create a PAT.

  1. You'll need to first going into the agent job and set "Allow scripts to access OAuth token".
  2. You'll need too go to your variable group and add the 'Project Collection Build Service' as an administrator.
  3. Now you can call the Rest API using the OAuth Bearer token. Code for reference:
$id = <variable group id>

# This is using some environment variables provided by the pipeline to build the URL
$url = ("$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/{0}?api-version=5.0-preview" -f $id)

# You might find it useful to us a GET method to grab the variable group, update it and then convert it to this json string rather than doing this here
$json = '{"id":$id,"type":"Vsts","name":"<Variable Group Name>","<Variable Name":{"ThisIsMyVariable":{"isSecret":false,"value":"20"}}}'

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

I made this task to manage variable groups from Pipelines: ManageVariableGroupTask

Using this you can Create/Update/Delete variable groups and do the same operations on variables contained in them.

Hope that's helpfull!

0
votes

If you want to update a variable group's value, use the REST.API methods. Rather than constructing the PUT request body manually, use a GET first to get the original JSON, update only what you need, then replay it as a PUT. I used to keep the variable group id also as a variable in that group to avoid hard-coding.

variables:
  - group: MyVarGroup

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/$(VariableGroupId)?api-version=6.0-preview.1"
      $header = @{
        "Authorization" = "Bearer $(System.AccessToken)"
      }
      $def = Invoke-RestMethod -Uri $url -Headers $header

      $def.variables.MyTestVar.value = "NewValue"
      $body = @($def) | ConvertTo-Json -Depth 100 -Compress
      $def = Invoke-RestMethod -Method 'Put' -Uri $url -ContentType 'application/json' -Headers $header -Body $body