How to Increase/Update Variable Group value using Azure Devops Build Definition?

后端 未结 3 668
既然无缘
既然无缘 2020-12-02 00:32

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

相关标签:
3条回答
  • 2020-12-02 01:08

    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 讨论(0)
  • 2020-12-02 01:12

    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 讨论(0)
  • 2020-12-02 01:24

    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 
    
    0 讨论(0)
提交回复
热议问题