Omar Bousbia
Omar Bousbia

Reputation: 123

Share variables across build pipelines in Azure devops

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).

Upvotes: 4

Views: 4666

Answers (2)

Ray Depew
Ray Depew

Reputation: 647

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.

Upvotes: 2

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51133

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.

Upvotes: 4

Related Questions