How to get the variable value in TFS/AzureDevOps from Build to Release Pipeline?

9.5k views Asked by At

I've defined a variable in my TFS/AzureDevops Build definition (say it's time) and assign the value using PowerShell task within my build definition.

Like,

Type: Inline Script.

Inline script:

$date=$(Get-Date -Format g);
Write-Host "##vso[task.setvariable variable=time]$date"

You can refer to this similar example

Now I want to get this value in my release definition pipeline. I configured this build definition as continuous deployment to my release definition.

My Question is

How can I get the value of time in my release definition using some other variable? Is this possible?

3

There are 3 answers

4
jessehouwing On BEST ANSWER

The is no official way to pass variables from Build to Release. The only way to accomplish this is to store the values in a file (json, xml, yaml, what have you) and attach that as a Build Artifact. That way you can read the file in the release and set the variable again.

Martin Hinshelwood seems to have gotten frustrated enough by this problem and turned that functionality into an extension for Azure DevOps Pipelines.

Tasks included

  • Variable Save Task - During your build you can save the variables to a json file stored with your other build assets
  • Variable Load Task - During your Release you can load the saved variables and gain access to them.
0
ccoutinho On

What about using a variable within a variable group?

I managed to set the value of a variable in a variable group to a variable coming from a build pipeline, and then to read that variable in a release pipeline.

For that, it is necessary:

  • To have previously created the variable group, and the variable (its name identified by $(variableName). Let's assume its value would be stored in $(variableValue)).
  • To find the variable group ID (stored in $(variableGroupId)), which can be done by navigating on Azure DevOps to that variable group. The group ID will then be in the URL.
  • A Personal Access Token (PAT) with Read & Write access to group variables (called $(personalAccessToken) )

CI pipeline

- powershell: |
    az pipelines variable-group variable update --group-id $(variableGroupId) --name $(variableName) --value $(variableValue)
  displayName: 'Store the variable in a group variable'
  env: 
    AZURE_DEVOPS_EXT_PAT: $(personalAccessToken)

Then all that's necessary, is to link the variable group to the release pipeline, and the variable will be seeded to that pipeline.

0
JBernhardt On

I found another solution that works even without text files just by using the Azure REST API to get the build parameters.

In my release pipeline I execute this Powershell tasks first to extract any build pipeline parameters:

function Get-ObjectMember {
  [CmdletBinding()]
  Param(
      [Parameter(Mandatory=$True, ValueFromPipeline=$True)]
      [PSCustomObject]$obj
  )
  $obj | Get-Member -MemberType NoteProperty | ForEach-Object {
      $key = $_.Name
      [PSCustomObject]@{Key = $key; Value = $obj."$key"}
  }
}

$url = "https://dev.azure.com/$(account)/$(project)/_apis/build/builds/$(Build.BuildId)?api-version=6.0"
$build = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
$params = $build.templateParameters
if ($params) {
  $params | Get-ObjectMember | foreach {
    Write-Host $_.Key : $_.Value
    echo "##vso[task.setvariable variable=$($_.Key);]$($_.Value)"
  }
}

My build pipeline contained the parameter: RELEASE_VERSION and after I executed the above code, I can use $(RELEASE_VERSION) in my other release tasks.