8

In VSTS CI/CD , I am setting some variable's value in a Powershell task in CI. During CD I want to access that variable's value to do something , lets say echo.

Is this possible? If so, how?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Aakash Uniyal
  • 1,519
  • 4
  • 17
  • 32

3 Answers3

6

You could write it out to a json/xml file and include that file in your published artifacts of your build defintion. Then read in that file via PowerShell in your release definition.

ConvertTo-Json | Out-File "file.json"
Get-Content "file.json" | ConvertFrom-Json
Calidus
  • 1,374
  • 2
  • 14
  • 31
3

For VSTS itself, it can not persists variables from build to release.

An workaround is store the variable’s value in Variable Group and link the variable group into your release definition. Detail steps as below:

  • During build, you can Add a variable group with the name group-$(Build.BuildId), and store the variable you want to transfer in the variable group.

  • During release, you can get variable groups firstly, and filter the variable under the variable group-$(Build.BuildId). And delete the group group at the end of the release.

Besides, if artifact type is build for your release definition, you can also store the variable value in a file and then publish the file as build artifacts (as Calidus says).

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • 1
    Couldn't this cause potential issues, if you releasing multiple builds? As the last build will overwrite the variables of the previous build. – Calidus Jun 19 '18 at 12:02
  • Yes, there has possibilities to overwrite the variable before release parse the variable's pervious value. And I updated my answer to create variable group in build definition, and delete it in release definition. Then the value is save to transfer. – Marina Liu Jun 20 '18 at 09:12
  • Can you transfer variables from build to release successful now? – Marina Liu Jun 22 '18 at 06:42
3

Check out the Azure DevOps extension Variable Tools for Azure DevOps Services.

In the "build pipeline" you can create a JSON file using "save variables". This file needs to be published as artifact or with existing artifact.

In the "release pipeline" you can restore the variables using "load variables" from the JSON file.

lg2de
  • 616
  • 4
  • 16