ldragicevic
ldragicevic

Reputation: 711

Azure Devops Pipeline: Define variable and use it as task input on another step

I have Azure DevOps release pipeline job having 4 tasks inside it.

I would like to set the environment variable in the first task and to use that value as input to the second task for parameter: Display name.

We can assume all 4 steps are PowerShell scripts.

Task 1 Powershell:

Write-Host "##vso[task.setvariable variable=myvariable;]abcdefg"

Task 2 Powershell: task2

PowerShell inside task 2:

Write-Host "$(myvariable)"

How could I set a variable in task 1 and access it as an input variable to task2?

My output was:

Task2 - $(myvariable) as display name

but PowerShell script itself output was:

abcdefg

Upvotes: 1

Views: 2353

Answers (1)

Mengdi Liang
Mengdi Liang

Reputation: 19026

Write-Host "##vso[task.setvariable variable=myvariable;]abcdefg"

That's because the variable this script created is not a pre-defined variable.

As the execution logic, after you queue the pipeline, you can see that the pipeline name, task name are displayed firstly, even though the script does not be executed. So, if these names are using variable to define them, the variable just can get the value from the pre-defined variable. Because the compile of variable value get in pipeline/task name are always firstly than the script executed.

In addition, the script in task just create a script variable, and this script variable only live for the lifetime of the Phase and are destroyed after execution.

How could I set a variable in task 1 and access it as an input variable to task2?

As you what want, if get it in another script, just use $(xxx) or $env:xxx. But, for name, $() just can get the pre-defined variable value, instead of the script variable value.

Upvotes: 2

Related Questions