DevOps | Scripts | Automation

Azure DevOpsPowershell

How to Pass variables between tasks in Azure Pipeline?

Passing variable values between tasks is not like passing a variable value from one function to another function because the tasks run in a separate namespace.

Consider the example below, we are using the Windows image to run our tasks and $vmName is the variable name that we need to pass its value from one task to another task.

trigger:
- none

pool:
  vmImage: windows-latest

steps:
  - task: PowerShell@2
    displayName: 'Task-1 : Store the Variable Value'
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Adding values to variable"
        $vmName = "TestVM"
  
  - task: PowerShell@2
    displayName: 'Task-2 : Display the Variable Value'
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "VM Name: $vmName"
Null variable output

We get nothing here because $vmName is not recognized in Task-2.

Using Environment Variable

The one easy way we can think of is to store the value as an environment variable because we are using a single VM image and it runs throughout the Pipeline operation. Let’s try that.

trigger:
- none

pool:
  vmImage: windows-latest

steps:
  - task: PowerShell@2
    displayName: 'Task-1 : Store the Variable Value'
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Adding values to variable"
        $env:vmName = "TestVM"
  
  - task: PowerShell@2
    displayName: 'Task-2 : Display the Variable Value'
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "VM Name: $($env:vmName)"
No output

The above code is also not working as per our expectations.

Using Task Variable

To solve this problem we can store the variable dynamically in the variable library and from there we can retrieve the variable value. To store variables using the PowerShell in the Azure Pipeline library we need to use task.setvariable command as shown below.

 Write-Host "##vso[task.setvariable variable=vmName]TestVM"

The above command will set the value “TestVM” to the variable vmName in task-1 and display that value in task-2, simply call the variable using the $(VariableName) method. Here, it would be, “$(vmName)”.

The entire pipeline will be,

trigger:
- none

pool:
  vmImage: windows-latest

steps:
  - task: PowerShell@2
    displayName: 'Task-1 : Store the Variable Value'
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Adding values to variable"
        Write-Host "##vso[task.setvariable variable=vmName]TestVM"
  
  - task: PowerShell@2
    displayName: 'Task-2 : Display the Variable Value'
    inputs:
      targetType: 'inline'
      script: |
        "VM Name: $(vmName)"
  
The output of the task variable.

This variable is also stored in the environment variable for that particular stage. To display its value using environment variable use $($env:vmName).

You can also store variable value first in another variable and then pass that variable to the task. For example,

 $vm = "TestVM"
 Write-Host "##vso[task.setvariable variable=vmName]$vm"

Please Note: This task variable only pertains to that stage. In the new stage, you can’t use the task variable value directly.

You can find the code in the GitHub repository as well.

https://github.com/chiragce17/MyPublicRepo/blob/main/AzurePipeline/PassVariableBetweenTasks.yml

One thought on “How to Pass variables between tasks in Azure Pipeline?

Comments are closed.