Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

  • Get
  • Update

Is there any other solution to this problem?

like image 778
Michiel Avatar asked Nov 06 '22 18:11

Michiel


1 Answers

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.

like image 170
jessehouwing Avatar answered Nov 15 '22 07:11

jessehouwing