Azure DevOps: How to set a Pipeline Variable on a Release?

Viewed 54

I am trying to create a simple Release Pipeline that:

  1. sets a value via a script
  2. asks a user to manually check the value, and modify it if needed
  3. goes on with the pipeline, using the value

What is the easiest way to achieve this functionality in Azure DevOps?


I have tried to define a Pipeline Variable called TestVariable, with initial value of 1, where I will store the value. A Pipeline Variable seems a good option to me, because its value can be easily modified by the user, during manual check.

The problem is, I cannot set its value via a script (as per point 1. above). I have tried to set its value to 2 via a Bash Script, but it does not work as you can see. Here is the code, if you want to try yourself:

echo $(TestVariable)
echo "##vso[task.setvariable variable=TestVariable;]2"
echo $(TestVariable)

I searched thoroughly the Azure documentation but I could not find any solution. Is there any way to set a Pipeline Variable through a script, or to achieve the 3 above mentioned points with another strategy?

1 Answers

The method you used to update the value is correct.

Refer to this doc: Set variables in scripts

A script in your pipeline can define a variable so that it can be consumed by one of the subsequent steps in the pipeline.

It will not work on the current task, but the updated value can be used in the next tasks.

Here is an example:

steps:

- bash: |
   echo $(TestVariable)
   echo "##vso[task.setvariable variable=TestVariable;]2"
   
  displayName: 'Bash Script'

- bash: |
   echo $(TestVariable)
   

Result:

enter image description here

Related