Passing Values Between Tasks in Azure DevOps Pipeline

Introduction

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

When a Task is executed, 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 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

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 Tasks, you may need the SAS token to access the Storage Account in another Task.
    Sas

The above Tasks may be located in the same Job, different Jobs within 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 that can be used to create a variable that is used across tasks within the pipeline, whether they are in the same job of a stage or across stages.

SetVariable Properties

Below are the properties of the task.setvariables command

Property Description
Variable It’s the variable name that should be used using the macro syntax $(var name)
Isoutput Isoutput = true lets us use the variable outside the current Jobs (across stages, too)
issecret Issecret=true make the variable a Secret
read-only Isreadonly = true make it a read-only 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 Pipeline would look something as shown below.

Pipeline

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

<<YAML Code here>>

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 the next Task.

New variable

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

  1. Create a variable named SASToken using the set variable syntax, and 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.

Stage 1

So far, it 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)

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

Implement

  1. Navigate to Stage1_Job1_Task1 and add the output = true flag to the Logging Command, which lets us access the value outside the Job.
  2. The Job in which you want to access the variable must be dependent on the other Job that produces the output. Add dependsOn: Stage1_Job1 in the Stage1_Job2.
  3. In Stage1_Job2, Create a new variable named NewSASToken and set its 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 Job2 can access the output of Job1.

Output

Share variables between Tasks across the Stages

Use the code below 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 you would like to access Stage1’s variable in Stage3 then Stage2 must depend on Stage1.

Stage3

Below is the output of the above code.

Jobs in run

Summary

In this article, we have learned how to use Logging commands to share the variables across Tasks located

  • Same Job
  • Across Jobs of a Stage
  • Across Jobs of different Stages