1

In Azure DevOps pipeline, how to update environment variable in variable group so new value persists, so new value can be used even after build is finished.

For example, I'm trying to save new version number, this does not work:

Write-Host "##vso[task.setvariable variable=currentVersion]$newVersion"
Andrija
  • 14,037
  • 18
  • 60
  • 87
  • Do you have a better result if you use `Write-Output` as opposed to `Write-Host`? I don't see any examples in the documentation where `Write-Host` is used this way. – AdminOfThings Sep 13 '19 at 10:27
  • 1
    You'll need to use the REST API to update the variable group if you want the variable group to be permanently updated with a new value. Refer to the REST API documentation. – Daniel Mann Sep 13 '19 at 14:01
  • @DanielMann is there any way to write persistent value anywhere in any way from CD on Azure DevOps? – Andrija Sep 13 '19 at 14:57
  • @Andrija Not really. Any solution you can come up with to this has the potential for race conditions, as multiple builds and/or releases can be running simultaneously. Needing to persist information like this is usually a sign that you're doing something wrong. – Daniel Mann Sep 13 '19 at 16:24

2 Answers2

1

how to update environment variable in variable group so new value persists, so new value can be used even after build is finished.

I am afraid there is no such way to update environment variable in variable group and keep it persists after build is finished.

When you use the Logging Command to set the variable, which is environment variable and can only work in the current environment.

So the new value can not be used after build is finished.

on the other hand, just like Daniel said, if we write any persistent value, then this value will compete/conflict with the value in the variable group. The compiler will not know which value to choose.

So, if you want to write any persistent value, we have to update the value in the variable group manually or use REST API to update it in the variable group.

Check the How to modify Azure DevOps release definition variable from a release task? for some more details.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Thank you for answer, it explains a lot. I gave up on setting up permanent value and I'm using different technique in deployment. – Andrija Sep 19 '19 at 07:39
0

Yes, you can update during build, but the Write-Host only persists in the pipeline currently running. You could use Azure CLI and call something like this:

echo %AZ_LOGIN_PAT%|az devops login

az pipelines variable-group variable update --group-id variable_id --org https://dev.azure.com/your_org --project your_project --name VariableName --value %NewValue%

The PAT might be able to be secured better, but this is how I do it. This is a Windows inline command task.

allexiusw
  • 1,533
  • 2
  • 16
  • 23