Calling Node.js Script from Azure CI Pipeline

3 minute read

If you want to run node.js script from azure-pipelines.yml and get the value returned from node.js script and use it in your CI next task. Then read this article. Example: I wanted to get new build number as per my package.json next version by running node script and update my CI pipeline build number.


In this article I will show:

  • How can you call build.js file to create new build number using package.json file & Azure CI pipeline build Id.
  • Then update Azure CI pipeline build number with the returned new build number.
  • Our new build number should be like **-** example: **123-main** or **123-feature1**

Creating JavaScript to create new Build Number

Create build.js file and here we will take buildId and branch-name as parameter passed from azure-pipelines.yml file.

Then we will increase the build id by one and add branch name and use that as new Build Number.

const buildId = process.argv[2];
const sourceBranchName = process.argv[3];
const newVersion = `${+buildId + 1}-${sourceBranchName}`;

console.log(newVersion); //Console Log will return the new version to Azure Pipelines.

๐Ÿ‘‰ Remember when you write value using console log in your node.js script. Then that value will be shared to the azure pipelines.yml file.

Therefore, do not write console log in your script which you donโ€™t want to pass to azure pipeline.

Creating Azure Pipelines file

Note: When you are creating a variable that is scoped within a single task. Then

  • use export to set a variable
  • And use $variable syntax to access variable value.

In below example we create num variable and we use $num to read the value of the variable.

  - main

  # calling node.js script
  - script: |
      export num=$(node ./build.js $(Build.BuildId) $(Build.SourceBranchName))
      echo "node.js returned $num"
    displayName: 'Executing Node.js script'

Running Azure CI Pipeline

Returning multiple value from Node.js to Azure pipeline

In order to return one value make sure just do single console.log and you will return exactly that value. Just like we did for build number above.

In order to return multiple values to Azure pipeline from node.js script. Donโ€™t write multiple console.logs.

For example if u want to return build and test command to azure pipeline then use below syntax in your node.js script file.

build: 'npm run build',
test: 'npm test'

Updating the Build Number with new build number given by node.js script

We will use "##vso[build.updatebuildnumber]$newNumber" script to update build number.

Add script to update the build number in the Azure pipelines

- script: |
    echo "old buildnumber ~>$(Build.BuildNumber)"
    export num=$(node ./build.js $(Build.BuildId) $(Build.SourceBranchName))
    echo "node.js returned $num"
    echo "##vso[build.updatebuildnumber]$num"
  displayName: 'Update Build Number'

Add another new script to show the build number.

๐Ÿ’ก Do not show the build number on the same previous task otherwise it will display the old build only. You must create new script to show the updated build number.

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

Updating build number Displaying new build number

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.

Your bright future is awaiting 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
โœ‰๏ธ Email Rupesh