--- layout: post title: Sharing Variables within Tasks in Azure CI Pipeline date: 2021-01-30 00:00 +0000 subtitle: Learn how to create custom variable and share among Azure CI Pipeline tasks description: Learn how to create custom variable and share among Azure CI Pipeline tasks cover-img: https://i.imgur.com/hiQZ0BG.png cover_image: https://i.imgur.com/U0TG9Td.png thumbnail-img: https://i.imgur.com/U0TG9Td.png share-img: https://i.imgur.com/U0TG9Td.png tags: 'devops,ci,tutorial,beginners' last_modified_at: published: true sitemap: true comments: true social-share: true excerpt_separator: "<!--more-->" --- # Sharing Variables within Tasks in Azure CI Pipeline ![](https://i.imgur.com/3vFNOtc.png) 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](https://hackmd.io/QiDKOk2RTKee1Y822ceSYA). 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'` ```yml= - 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](https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#macro-syntax-variables) 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](https://hackmd.io/QiDKOk2RTKee1Y822ceSYA)" 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. ```yaml= - 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 ``` ![](https://i.imgur.com/87SqLYm.png) ## 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](https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#macro-syntax-variables) 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. ```yaml= - script: | echo "Accessing NewBuild Number Variable $(newBuildNumber) & Updating Build Number of Pipeline" echo "##vso[build.updatebuildnumber]$(newBuildNumber)" displayName: 'Update Build Number' enabled: true ``` ![](https://i.imgur.com/wL5xJkE.png) ## Displaying new Build number of azure pipeline In order to display updated build number I will use `Build.BuildNumber` ```yaml= - script: echo "new buildnumber ~>$(Build.BuildNumber)" displayName: 'Display New Build Number' enabled: true ``` ![](https://i.imgur.com/kMxUfSa.png) --- ## Do You Want to become full stack developer? :computer: If you want to become full stack developer and grow your carrier as Lead Developer/Architect. Consider subscribing to our full stack development training programs. We have monthly membership plans and you will get unlimited access to all of our video courses, slides, source code & Monthly video calls. - Please visit www.fullstackmaster.net/pro to subscribe to All Access PRO membership. - Please visit www.fullstackmaster.net/elite to subscribe to All Access ELITE membership. You will get everything from PRO plan. Additionally you will get access to monthly live Q&A video call with Rupesh and ask doubts and get more tips and tricks. >You bright future is waiting for you so visit today www.fullstackmaster.net and allow me to help you to board on your dream software architect/lead role. --- ### :sparkling_heart: Contact Details: Say :wave: to me! * Rupesh Tiwari * www.rupeshtiwari.com * :email: <fullstackmaster1@gmail.com> * Founder of www.fullstackmaster.net :mortar_board: * [<img src="https://i.imgur.com/9OCLciM.png" width="295" height="65">](http://www.fullstackmaster.net)