0

I am working on Azure DevOps Release pipeline. The first task in the release pipeline is a Powershell task. This task has a Powershell Script Inline. The below is the content of the task:

steps:
- powershell: |
$repo = '$(Release.TriggeringArtifact.Alias)'

switch ( $repo )
{
   _repo-health { $result = 'Health'    }

}

$result
$Repo_Name = $result

Write-Output "$Repo_Name"

displayName: 'PowerShell Script'

So, from the above task via Powershell script I am trying to fetch the Repository name using predefined variables and assign it to a variable.

The second task in the pipeline is a Powershell task with a Powershell script with the below content -

# Write your PowerShell commands here.

Write-Output "$Repo_Name"

So, when I am trying to print "$Repo_Name" in the same task it is printing, but if I am trying to print or fetch the variable value in the another task or stage in the same pipeline I am not able to print it. So need help here.

1 Answer 1

1

You use special formatted output from your first powershell script. That will tell the build agent that you want to set a variable to a given value:

Write-Host "##vso[task.setvariable variable=Repo_Name;]$Repo_Name"

From then on, the variable "Repo_Name" will have the value of the powershell variable "$Repo_Name".

In following scripts/tasks you can then reference the variable:

Write-Host "I got the value of $(Repo_Name) for the Repo_Name variable."

More details here, especially review the isoutput=true modifier which you might need to add depending on the "scope" that your variable should have.

For example, if you need to read the variable in the next stage, you have to set it like this:

Write-Host "##vso[task.setvariable variable=Repo_Name;isoutput=true]$Repo_Name"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for time saving, yes its working fine between the tasks of a one stage. But I am looking for it to work between different Stages, like I have described this in one stage and this value should be available in the next stage also. When I tried now using in different stage I got like this output I got the value of for the Repo_Name variable. Value is missing, its not able to read it in the next stage.
Have you actually read the linked document, especially about the "isoutput" parameter? Note it says: "To use the variable in the next stage, set the isoutput property to true. To reference a variable with the isoutput set to true, you'll include the task name. For example, $(TaskName.myVar).". FWIW, I have updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.