Thursday, 11 April 2019

Setting a Release Variable to Make It Available to Other Stages in Azure DevOps Release Pipeline

You can easily set a variable in a build or release pipeline by executing “##vso[task.setvariable variable=variablename;]variablevalue” . This sets the variable for the scope of a given stage in a release pipeline. However, it does not allow you to set a variable in the scope of a release. That means if you want to set a variable value in stage and then use it in subsequent stages it is not possible to do it with  ##vso[task.setvariable . Let’s look at achieving this need using Azure DevOps REST API.
The script available here can be used for this purpose. A slightly modified version of the script to support release pipeline and enable sending any variable name and value for updating a value of a given variable can be found here.
Steps of the Script
1. Take in two parameters – VariableName and VariableValue
2. Create header to access REST API with System.AcessToken. You have to enable Allow Scripts to access the OAuth token in each deployment or agent group phase that you intend to use this script.


3. Get the current release using the ReleaseID.
4. Update the variable value.
5. Update the release using REST API.

You can use this script and execute it with a PowerShell task in your pipeline. Or you can setup a Task group so that you can reuse it as a task in your release pipelines.
In the above example Run Inline PowerShell task available with extension Inline PowerShell is used. Notice the usage of Variable name and Variable value surrounded with ‘ when passing parameters to avoid argument errors you may encounter if you use them without ‘, specially for variable value.
-VariableName '$(VariableName)' -VariableValue '$(VariableValue)'
Task group can be used as shown below to set a variable value so that it is available to other stages in the release pipeline.

No comments:

Popular Posts