Azure DevOps – Tips and Tricks – 5 – How to pass values between Tasks in a Pipeline using task.setvariable Logging Command

In this article, we are going to learn how to pass values from one task to another within a job, across the jobs and stages.

Introduction

Azure DevOps pipeline is a set of Tasks which can perform a specific task. All these tasks run inside a Virtual Machine which is called an Agent.

When a Task is executing, it will be allocated some resources and after the Task execution is complete, the allocated resources will be de-allocated and the entire allocation / de-allocation process repeats for other tasks available within the pipeline.

It means the Task1 cannot directly communicate with Task2 or any other subsequent Tasks in the pipeline as their scope of execution is completely isolated though they get executed in the same Virtual Machine.

In this article, we are going to learn about the scenario where communication between Tasks is required and how to achieve the same.

Let’s now understand a real-time scenario where communication between Tasks is required.

Scenario

Advertisements

When you plan to implement Infra automation, you may want to implement something shown below.

  1. Create a Storage Account and create a SAS token in one Task.
  2. In subsequent Task, you may need the SAS token to access Storage Account in another Task.
Azure DevOps – Tips and Tricks – 5 – How to pass values from one Task to another Task in a Pipeline – Scenario

The above Tasks may be located in the same Job, different Job with-in a Stage or across Stages. We are going to learn how to achieve the functionality of passing values in all these scenarios.

Solution

Logging command called task.setvariable lets us pass variables across Tasks. Task.setvariable sets the value to a variable which by default can be used in any Task within a Job or across the Jobs of a given Stage.

About task.setvariable

Task.setvariable is a logging command can be used to create a variable that be used across tasks within the pipeline whether they are in same job of a stage or across stages.

Advertisements

SetVariable Properties

Below are the properties of the task.setvariables command

PropertyDescription
VariableIt’s the variable name which should be used using macro syntax $(varname)
IsoutputIsoutput = true lets us to use the variable outside the current Jobs (across stages too)
issecretIssecret=true make the variable as a Secret
isreadonlyIsreadonly = true make it a readonly variable

In order to understand how to use task.setvariable, we will be creating the below,

  1. Two Stages
  2. Two Jobs in each Stage
  3. Two Tasks in each Job.

The structure of the Pipeline would look something as shown below.

Advertisements
Azure DevOps – Tips and Tricks – 5 – How to pass values from one Task to another Task in a Pipeline – Pipeline architecture

Let’s create the skeleton of the above structure using the below Azure DevOps YAML Pipeline code.

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
- stage: Stage1
  jobs:
  - job: Stage1_Job1
    steps:
    - task: PowerShell@2
      displayName: 'Stage1-Job1-Task1'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
    - task: PowerShell@2
      displayName: 'Stage1-Job1-Task2'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
  - job: Stage1_Job2
    steps:
    - task: PowerShell@2
      displayName: 'Stage1-Job2-Task1'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
    - task: PowerShell@2
      displayName: 'Stage1-Job2-Task2'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
- stage: Stage2
  jobs:
  - job: Stage2_Job1
    steps:
    - task: PowerShell@2
      displayName: 'Stage2-Job1-Task1'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
    - task: PowerShell@2
      displayName: 'Stage2-Job1-Task2'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
  - job: Stage2_Job2
    steps:
    - task: PowerShell@2
      displayName: 'Stage2-Job2-Task1'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
    - task: PowerShell@2
      displayName: 'Stage2-Job2-Task2'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
  

                         

Share variables between Tasks within a Job

Let’s now create a new variable in Task1, assign some value to it and access the Variable in next Task.

Azure DevOps – Tips and Tricks – 5 – How to pass values from one Task to another Task in a Pipeline – within the same Job of a Stage

As shown in the above screenshot, implement the below changes.

  1. Create a variable named SASToken using the setvariable syntax, assign it some test value (TestSASTokenValue in this case)
  2. Display the value of the SASToken variable in the next Task.

Now, view the output of the variable of the Stage1-Job1-Task2 as shown below.

Azure DevOps – Tips and Tricks – 5 – How to pass values from one Task to another Task in a Pipeline – within the same Job of a Stage – Output

So far, it’s looks pretty simple. However, it’s not possible to access the same variable in another Task located in another job. Let’s learn how to achieve that in the next section.

Share variables between Tasks across the Jobs (of the same Stage)

Advertisements

We need to use the isOutput=true flag when you desire to use the variable in another Task located in another Job.

Let’s implement it now by making the below changes

Azure DevOps – Tips and Tricks – 5 – How to pass values from one Task to another Task in a Pipeline – within the same Job of a Stage – Set IsOutput to True
  1. Navigate to Stage1_Job1_Task1 and add isoutput = true flag to the Logging Command which let’s us to access the value outside the Job.
  2. The Job in which you want to access the variable must be dependent on the other Job which produces the output. Add dependsOn: Stage1_Job1 in the Stage1_Job2.
  3. In the Stage1_Job2, Create a new variable named NewSASToken and set it’s values to $[dependencies.Stage1_Job1.outputs[‘Stage1_Job1_Task1.SASToken’]]. This will help to access the variable value which is available in another dependent job. You can’ access this expression directly in the script. It’s mandatory to map the expression into the value of another variable.
  4. Finally, access the new variable in your script.
  5. Once the isoutput=true is added, it’s important to access the variable by prefixing the Task name. Otherwise, it wouldn’t work.

Below is the output of the updated code where the Job2 can access the output of Job1.

Share variables between Tasks across the Stages

Use the below code to share the variables in a Task located in another stage.

Important Note: As shown in the below screen captures, I didn’t specify dependency (using dependsOn) between Stages as Stage1 and Stage2 are one after the other. In case if you would like to access Stage1’s variable in Stage3 then the Stage2 must depend on Stage1.

Azure DevOps – Tips and Tricks – 5 – How to pass values from one Task to another Task in a Pipeline – Access variables Across Stages

Below is the output of the above code.

Azure DevOps – Tips and Tricks – 5 – How to pass values from one Task to another Task in a Pipeline – Access variables Across Stages – Output

Code Blocks

Below is the complete code of this Tip.

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
- stage: Stage1
  jobs:
  - job: Stage1_Job1
    steps:
    - task: PowerShell@2
      displayName: 'Stage1-Job1-Task1'
      name: 'Stage1_Job1_Task1'
      inputs:
        targetType: 'inline'
        script: |
         Write-Host "##vso[task.setvariable variable=SASToken;isoutput=true;]TestSASTokenValue"
    -  task: PowerShell@2
       displayName: 'Stage1-Job1-Task2'
       continueOnError: true
       inputs:
          targetType: 'inline'
          script: |
           Write-Host "The value of SASToken: $(Stage1_Job1_Task1.SASToken)"
  - job: Stage1_Job2
    dependsOn: Stage1_Job1
    variables:
    - name: NewSASToken
      value: $[dependencies.Stage1_Job1.outputs['Stage1_Job1_Task1.SASToken']]
    steps:
    - task: PowerShell@2
      displayName: 'Stage1-Job2-Task1'
      inputs:
        targetType: 'inline'
        script: |
           'Write-Host "The value of SASToken is $(NewSASToken)"'
    - task: PowerShell@2
      displayName: 'Stage1-Job2-Task2'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
- stage: Stage2
  variables:
    - name: NewSASToken
      value: $[stageDependencies.Stage1.Stage1_Job1.outputs['Stage1_Job1_Task1.SASToken']]
  jobs:
  - job: Stage2_Job1
    steps:
    - task: PowerShell@2
      displayName: 'Stage2-Job1-Task1'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "The value of SASToken in Stage2: $(NewSASToken)"'
    - task: PowerShell@2
      displayName: 'Stage2-Job1-Task2'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
  - job: Stage2_Job2
    steps:
    - task: PowerShell@2
      displayName: 'Stage2-Job2-Task1'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
    - task: PowerShell@2
      displayName: 'Stage2-Job2-Task2'
      inputs:
        targetType: 'inline'
        script: 'Write-Host "Hello World"'
  

Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups

WhatsApp

Telegram

Advertisements
Advertisements

Leave a comment