3
votes

Question: Is there any way to define a custom-variable in a build pipeline in azure-devops, which then can be used in/exposed to the release pipeline in any way?

Scenario: We are not using a variable group, because we need to dynamically set the variable in the build pipeline and then consume it in the release pipeline - it is not a static super global.

Checked the docs at release variables and build variables but could not find any helping information or a hint, that this is possible.

What I tried

  1. Define a variable in variables ( in the build pipeline ) and try to access it in the release pipeline using $(name) or checking if it is in env.

Extras - Motivation The motivation behind this is

  1. read the latest git-tag used in a build-pipeline step and expose this into the pipeline variable VERSION ( actually, we bump patch during that )
  2. Releasing builds is a manual step.
  3. If a build is released, the azure-devops gui shows us all the variables of the release-pipeline, which are "settable during release - this includes the version we want to release this package with
  4. we want this "VERSION" to be prefilled with the version of the build-pipeline as a suggestion for the next version
  5. In the release pipeline we checkout the repo, add the VERSION as a tag and package/publish the artifact with this version
2

2 Answers

1
votes

How to use a custom variable from build-pipeline in a release pipeline

You could try to use the REST API Release Definitions - Update to update the default variable in the release pipeline to use the value defined in a build pipeline.

PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=5.1

Details:

Define a custom-variable in a build pipeline, like TestValue, the value is 123:

enter image description here

Also define the same custom-variable in a Release pipeline with default value 123:

enter image description here

Then add a inline powershell scripts to invoke the REST API Definitions - Update to update the default value in the release pipeline:

$url = "https://vsrm.dev.azure.com/<OrganizationName>/<ProjectName>/_apis/release/definitions/<DefinitionId>?api-version=5.1"

Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Method Get -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

# Update an existing variable named TestValue to its new value 987
$pipeline.variables.TestValue.value = "$(TestValue)"

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99


Write-Host "URL: $json "

$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe 'TestValue' is updated to" $updatedef.variables.TestValue.value

In this case, we could dynamically set the variable when we queue the build pipeline, and this value will overwrite the default value in the release pipeline, so that we could use it release pipeline.

Hope this helps.

1
votes

This is impossible by default, but you can use 2 extensions from the marketplace:

1) Shared variable updater - Create a variable group and during the build update there the variables dynamically with this task. you can also do it with your script, see the answers here.

2) Variable Kit for Azure DevOps Services - During a Build, save variables to a json file stored along with your build assets. During a Release, load the saved variables and use them within the release definition.