8

The case I have is as follow: I've created an Azure DevOps Pipeline with a Pipeline variable, let's say 'variable A'. The value of 'variable A' is 1. During the build, I change the value of 'variable A' to 2.

When the build runs for the second time I want the value of these 'variable A' but this is back 1 but I want that the value is 2 because on the previous build I set the value of 'variable A' to 2.

These are the methods I tried without success:

Method 1:

Write-Host "##vso[task.setvariable variable=A;]2"

Method 2:

$env:A = 2

The only thing that works but I don't think this is the way to go is to get the whole build definition via the rest api and put it back with the value of the variable changed.

Is there any other solution to this problem?

riQQ
  • 9,878
  • 7
  • 49
  • 66
Michiel
  • 93
  • 1
  • 1
  • 6

3 Answers3

1

If you're specifically looking at an increasing number, then you can also use counters. These only woork in YAML based build definitions.

The format is as follows:

You can use any of the supported expressions for setting a variable. Here is an example of setting a variable to act as a counter that starts at 100, gets incremented by 1 for every run, and gets reset to 100 every day.

yaml

jobs:
- job:
  variables:
    a: $[counter(format('{0:yyyyMMdd}', pipeline.startTime), 100)]
  steps:
    - bash: echo $(a)

For more information about counters and other expressions, see expressions.

The counter is stored for the pipeline and is based on the prefix you provide in the counterr expression. The above expression uses the yyyymmdd to generate a prefix which is unique every day.


For UI driven build definitions, then indeed using the REST api to update the whole definiton would work, though it's really hard to work around all possibilities concerning paralelism.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • It's not for a counter I would like to use this but for a version and check if the version of the c# project of the previous build is higher than the current build. But it seems to be that I need to use the REST api for this. – Michiel Apr 18 '19 at 12:01
  • 1
    Or you can use the BuildNumber variable to be leading. It will be ever upgrading and you can inject it into the AssemblyVersion etc, instead of making the code leading in this regard. If not, either using the REST API to fetch the current value, or you can dig in the history of the code through `git` or `tf` to just compare the actual code files instead. – jessehouwing Apr 18 '19 at 12:05
0

How to change pipeline variables for usage in the next build in Azure DevOps

I am afraid you have to use the rest api to change the value of that pipeline variables.

That because when you use the script `"##vso[task.setvariable variable=testvar;]testvalue" to overwrite it, the overwrite value only work for current build pipeline.

When you use the execute the build again, it will still pull the value from pipeline variable value.

So, we have to update the value of that variables on the web portal. Then we need use the need the REST API (Definitions - Update) to update the value of the build pipeline definition variable from a build task:

Similar thread: How to modify Azure DevOps release definition variable from a release task?

Note:Change the API to the build definitions:

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
0

I have found the easiest way to update variable values during a pipeline execution is to use the Azure CLI having also tried other methods with little or no success.

In a YAML pipeline, this may look something like so:

jobs:
  - job: Update_Version
    steps:
    - task: AzureCLI@2
      inputs:
        azureSubscription: [your_subscription_id]
        scriptType: 'pscore'
        scriptLocation: 'inlineScript'
        inlineScript: |
          # set environment variable for current process
          $env:AZURE_DEVOPS_EXT_PAT = $env:SYSTEM_ACCESSTOKEN

          $oldVersionNumber = $(version-number)
          $newVersionNumber = $oldVersionNumber + 1
          az pipelines variable-group variable update --group-id [your_variable_group_id] --name version-number --organization [your_organization_url] --project [your_project_name] --value $newVersionNumber 
      env:
        SYSTEM_ACCESSTOKEN: $(System.AccessToken)

The pipeline build service may also need permission to execute this command. To check, go to Pipelines -> Library -> Variable groups then edit the variable group containing your variable. Click on the Security button and make sure the user Project Collection Build Service has the Administrator role.

More information on the Azure CLI command can be found here. There is also another form of the command used to update variables that are not in a variable group, as described here.

Matthew
  • 155
  • 1
  • 11