6

I have a build pipeline in Azure DevOps which releases artifact via npm pack for later publishing into Artifacts Feed.

I would like to set my major and minor version via GIT but patch version to tie with build number. E.g.

1.2.20201212.4

where 1 and 2 is major.minor version manually updated via GIT and 20201212.4 is a patch and revision number set by build pipeline

Could someone help me to figure out the required npm version command parameters to preserve minor.major version from source code and update only patch and revision part from $(Build.BuildNumber) variable?

irriss
  • 742
  • 2
  • 11
  • 22
  • Hi @irriss. Feel free to let me know if the latest update could meet your requirements. If it could give you some help, you may consider accepting it as answer. Thanks. – Kevin Lu-MSFT Dec 16 '20 at 01:15

1 Answers1

16

In Azure Devops, you could use the Replace Tokens task from the Replace Tokens Extension.

Then you could add the task before the NPM pack task to replace the variable in Package.json -> Version field.

Here are the steps:

Package.Json file:

{
  "name": "helloword",
  "version": "0.0.#{SetNumber}#",
  "description": "Hello World",
  "main": "server.js",
...
}

Build Pipeline:

  1. Set Variables in Build Pipeline.

enter image description here

  1. Add the Replace Tokens task.

enter image description here

Note: Based on my test, npm package cannot support xx.x.x.x version format(npm publish) in azure devops. It can support the x.x.x-x.

So you can set buildnumber like this:$(Date:yyyyMMdd)-$(Rev:r).

Result:

enter image description here

Update:

You could try to use the Npm Version command.

npm version  0.0.$(build.buildnumber) --no-git-tag-version

enter image description here

Update2:

You could try to use the following powershell script to get the version field in the Package.json. Then update the patch number.

$filePath = "$(Build.sourcesdirectory)\package.json" #filepath
$Jsonfile= Get-Content $filePath | ConvertFrom-Json
$version = $Jsonfile.version

echo $version

$major,$minor,$build = $version.Split('.')    

$build = "$(build.buildnumber)"    

$bumpedVersion = $major,$minor,$build  -join(".")

echo $bumpedVersion

Write-Host "##vso[task.setvariable variable=version]$bumpedVersion"

In Npm version, you could run the following command:

version $(version) --no-git-tag-version

enter image description here

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 2
    Thanks Kevin, that's all good but using "version": "0.1.#{SetNumber}#" in package.json causes > Executing task: npm run build < npm ERR! Invalid version: "0.1.#{SetNumber}#" when I try to build locally. Is there another way or workaround? – irriss Dec 14 '20 at 07:15
  • Hi @irriss. Of course. You could add replace token task in the first step of the pipeline. In other words, run replace token task before executing npm task(npm run build).This task is used to assign a value to the version field. In this case, it should work. – Kevin Lu-MSFT Dec 14 '20 at 07:19
  • 2
    I mean not in the pipeline but in local development process. There is no variable #{SetNumber}# and it causes the issue – irriss Dec 14 '20 at 09:30
  • @irriss. Please refer to the update information. You could use the npm version command in pipeline. I have added the detailed information. You can check if it could work. In this case, you don't need to change the version filed in package.json file. This can avoid the error you mentioned above. – Kevin Lu-MSFT Dec 14 '20 at 09:53
  • thanks for the update Kevin, but if I run npm version 0.0.$(build.buildnumber) --no-git-tag-version it will always overwrite major and minor versions to zeros. I would like to preserve minor/major numbers and update only patch version? that's where I stuck – irriss Dec 14 '20 at 10:00
  • Hi @irriss. Please refer to update2. You could use Powershell to get the version number in the package.json. Then update the patch number and re-create a new number. Feel free to let me know if it can meet your requirements. – Kevin Lu-MSFT Dec 15 '20 at 01:59
  • Is there a way without PowerShell? Some Bash script solution? – sunwarr10r May 03 '21 at 21:26