Sharing Variables within Tasks in Azure CI Pipeline

3 minute read

Do you want to create one variable in task-1 and read the value of that variable in task-2 in your azure ci pipeline? If your answer is yes then read this article.

I will show you how to get new build number from node.js. Then we will use the new build number in another task to update the azure pipeline build number.

azure devops setvariable task

We will write expression to run inbuilt task from azure pipeline to set new variable. When we run script from azure pipeline then basically it is a command line script you are running in windows command prompt. echo '##vso[task.setvariable variable=one]secondValue'

- steps
 - script : |
     echo "##vso[task.setvariable variable=name]Rupesh"

 - script : echo "My Name is $(name)"  # it will print Rupesh

Notice we are using Macro Syntax to access the variable name. :bulb: Notice I have read the variable name in next task. If I would read the variable on the first task it will print empty. So any task that assigns pipeline variables those are only accessible in next task onwards only.

Creating Pipeline Variable in Azure pipeline Task

Lets create newBuildNumber variable that we are getting from node.js script.

:bulb: As we know when you create a pipeline level variable in a task then within the same task you will not be able to access that pipeline variable within same task. Therefore, always when you create a pipeline variable then try to always create new task to read that variable and do other task. Therefore, we are printing the value using task scoped variable $num on “Create New Variable” task.

In below example I am using build.js file to get new build number. Read “Calling Node.js Script from Azure Devops CI Pipeline” to know how am I returning build id from build.js file.

Create below task and note we are using setvariable task to create newBuildNumber variable.

- script: |
    echo "old buildnumber ~>$(Build.BuildNumber)"
    export num=$(node ./build.js $(Build.BuildId) $(Build.SourceBranchName))
    echo "##vso[task.setvariable variable=newBuildNumber]$num"
    echo "Created new variable 'newBuildNumber' with value $num"
  displayName: 'Creating New Variable'
  enabled: true

Accessing Pipeline Variable in Azure pipeline Task

:bulb: Remember when you are accessing a pipeline variable that is shared across task then you must use Macro Syntax means $(newBuildNumber).

In this example I am using $(newBuildNumber) to access/read pipeline scoped newBuildNumber variable created from previous task.

We are updating the build number of the azure pipeline.

- script: |
    echo "Accessing NewBuild Number Variable $(newBuildNumber) & Updating Build Number of Pipeline"
    echo "##vso[build.updatebuildnumber]$(newBuildNumber)"
  displayName: 'Update Build Number'
  enabled: true

Displaying new Build number of azure pipeline

In order to display updated build number I will use Build.BuildNumber

- script:  echo "new buildnumber ~>$(Build.BuildNumber)"
  displayName: 'Display New Build Number'
  enabled: true


Become full stack developer 💻

If you want to become full stack developer and grow your carrier as new software developer or Lead Developer/Architect. Consider subscribing to our full stack development training programs. We have All-Access Monthly membership plans and you will get unlimited access to all of our video courses, slides, source code & Monthly video calls.

  • Please subscribe to All-Access Membership PRO plan to access current and future angular, node.js and related courses.
  • Please subscribe to All-Access Membership ELITE plan to get everything from PRO plan. Additionally, you will get access to monthly live Q&A video call with Rupesh and you can ask doubts/questions and get more help, tips and tricks.

You bright future is waiting for you so visit today FullstackMaster and allow me to help you to board on your dream software company as a Developer,Architect or Lead Engineer role. 💖 Say 👋 to me!

Rupesh Tiwari
Founder of Fullstack Master

Comments