3

I have 2 build pipelines in my azure devops project, one for building source code and the other one is for making the setup. I want the build number generated by the first pipeline that compiles code to be passed to the next pipeline which creates the setup file because i want the setup file to take the same version, so I added a variable group with a variable called sharedBuildCounter.

But when I set sharedBuildCounter the build number in the first pipeline using logging command like this(used inside PowerShell task):

Write-Host "##vso[task.setvariable variable=variable_name;]new_value"

The variable indeed takes the new value and I am able to output the new value using another PowerShell task with one line:

Write-Host $(SharedBuildCounter)

And when I run the next pipeline that builds the setup, I find that sharedBuildCounter is being re-set to the default empty value.

Notice: I found threads that suggests using API rest calls to change variable values, but it don't seem to include a specific pipeline name in case of using pipeline variables(not variable groups).

Omar Bousbia
  • 103
  • 3
  • 9

2 Answers2

2

Variable groups will help to share static values across builds and releases pipeline.

What you need is a way to pass variables from one pipeline to another. I'm afraid to say the is no official way to do this.

As a workaround you could update the value of your variables inside your variable group. There are multiple ways to handle this, Rest API, powershell, 3rd-party extension. Detail ways please refer answers in this question: How to Increase/Update Variable Group value using Azure Devops Build Definition?

If you want to get the value of variable in the pipeline. Since you have used logging command to update that variable.

You need to use Rest API to get that particular build log to fetch related info.

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • _Variable groups will help to share static values across builds and releases pipeline._ Do you mean that variable groups can only be changed manually in the UI? – Omar Bousbia Jun 17 '20 at 07:45
  • 1
    @OmarBousbia No, that's not my mean. Sorry for not make it more clear. You can not change a variable in a variable group with the logging command task.setvariable (the logging command can change only for a specific run). It's not dynamically. In other words, even you change the variable value, but it will not effect the original variable value in Variable Group. You need to use Rest API to directly update Variable Group, which means not related to a specific build/release pipeline. Hope this is more clearly. – PatrickLu-MSFT Jun 17 '20 at 08:19
  • 1
    As for how to use Rest API to update variable group, please have a look at this question: https://stackoverflow.com/questions/56558143/is-there-a-way-to-set-variables-in-variable-groups/56558502#56558502 – PatrickLu-MSFT Jun 17 '20 at 08:20
  • Thank you @Patricklu-msft calling the REST api is the solution, but for the variable group id is there a way to get it, because i tested the rest call with a newly created project & pipeline with only one variable group so i set the variable group id to 1 in the url? – Omar Bousbia Jun 17 '20 at 08:37
  • 1
    Hi Omar, you could get variable group id by using this rest api -- [Get variable groups](https://learn.microsoft.com/en-us/rest/api/azure/devops/distributedtask/variablegroups/get%20variable%20groups?view=azure-devops-rest-5.1). And you will see all variable group info such as group name, ID,variables .. in the project from the JSON response – PatrickLu-MSFT Jun 17 '20 at 08:48
1

You can use Azure Artifacts to pass information between pipelines. In one pipeline, you write the values to a file and publish the file to an artifact. In the other pipeline, you download the artifact and read the file.

There may be other ways to do it. Azure DevOps allows for free and infinite use of Azure Artifacts in this fashion.

See How to get variable values from pipeline resources in azure pipelines.

Ray Depew
  • 573
  • 1
  • 9
  • 22