A task to set an Azure DevOps Release Variable in a way that can be shared between pipelines
Out of the box, Azure DevOps does not provide an easy way to shared variables between Jobs in a Release Stage, or between Stages.
Donovan Brown and Stefan Stranger have demonstrated how to use the Azure DevOps REST API to update Release Variables from a Powershell script.
What I have done is to modify their scripts slightly and export an Azure DevOps Task that you can import into your own projects to enable this functionality. By default, exporting a Task Group may result in some of the system variables that your script depends on being created as parameters of the task group.
I followed Dave Lloyd's guide to setting a visibleRule
that is always false that will prevent these required parameters from being shown when you add the task to to your Pipeline. Annoyingly they are still shown in the imported Task Group, but at least we have minimised the risk of them being amended in the Pipeline itself.
To use:
- Go to
Pipelines
\Task Groups
in your project. - Click on
Import a Task Group
thenBrowse
toSetReleaseVariable.json
and clickOK
. - Create a Release Variable you want to amend
- Go to your Project's task groups and Import the Json
- Ensure that your organisation's
Project Collection Build Service
has thePipelines\Manage Build Resources
permission set toAllow
- Add the task to a Release Job.
- Set the ReleaseVariableName and ValueToSet parameters
- From this point on in any job or stage you can access the value of your Release Variable in the normal way.
#To-Do Astute observers will note that this assumes a happy path. Ideally the task should elegantly recover from situations where it can't find or set the variable.