Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using build number from package.json

I have a Node project in Azure devops and would like to set the build number to whatever is in the current package.json, appended with a number. So if my package.json says the version is 0.0.1-beta, the build number would be like 0.0.1-beta+20190215.1.

It is easy to get the version string using npm: npm view <package-name> version, but I can't figure out where in the pipeline to store and inject it to use it as the build number. From what I can tell the build number can only be set as a hard coded value or through variables that have been set "manually", and not in a dynamic way like using an output of a command.

I tried using variables, first a custom one that didn't work. I also found a variable named Build.Buildname but it seems this can't be written to.

Any ideas?

This is the relevant section of my base pipeline.yaml that I'm experimenting with:

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '8.x'
  displayName: 'Install Node.js'

- script: |
    npm install
    npmVersionString=$(npm view <package-name> version) 
    echo ##vso[task.setvariable variable=build.buildnumber]$npmVersionString
    npm run build
  displayName: 'npm install, set buildnumber and build'
like image 448
roqvist Avatar asked Sep 08 '26 02:09

roqvist


1 Answers

To update the build number during the build you can't just update the variable Build.BuildNumber like each variable, there is a special command to do it:

##vso[build.updatebuildnumber]{build number}

So in your case replace this line

echo ##vso[task.setvariable variable=build.buildnumber]$npmVersionString

With this line

    echo "##vso[build.updatebuildnumber]$npmVersionString"

(Don't forget to append the build number if you want it).

Example

- script: |
    npmVersionString=$(node -p "require('./package.json').version") 
    echo "##vso[build.updatebuildnumber]$npmVersionString"
  displayName: 'set build number'
like image 60
Shayki Abramczyk Avatar answered Sep 11 '26 22:09

Shayki Abramczyk