NPM with NuGet package auto versioning

277 Views Asked by At

Our Goal is to have Auto versioning for npm and NuGet packages in release definition of Azure Pipelines

As of now we are using the tokenization task based on the rev value we replacing the version numbers in both nuspec and package.json files. So we getting the version numbers like 1.0.1, 1.0.2….. like this and auto versioning achieved in release definition

But the problem is whenever the release fails we are losing those numbers as our version numbers because rev value is increasing

Example: If my published version of artifacts is 1.0.1.. Next version to be published for me is 1.0.2

But if the 1.0.2 release is failed and if 1.0.3 is succeeded we are getting published number as 1.0.3 here for end user 1.0.2 is missing

Now we need the help to increment my version number based on last successful release or to reset the rev value based on the successful release or to get the published version of artifacts and increase that number

Or any other best practices to accomplish this task will be helpful.

Thank you in advance.

1

There are 1 best solutions below

0
On

NPM with NuGet package auto versioning

I am afraid there is no such way to accomplish this task directly. Because $(Rev:.r) is the build number on this day. Use $(Rev:.r) to ensure that every completed build has a unique name. When a build is completed, if nothing else in the build number has changed, the Rev integer value is incremented by one. This value stores in database.

As test, I have created a Inline Powershell task to modify the value when build/release task fails.

I use the powershell script Write-Host "##vso[build.updatebuildnumber]$newVersionNumber" to update the build.updatebuildnumber (I use build.updatebuildnumber as nuget package version.) and set the option Run this task as Only when a previous task has failed:

enter image description here

To see which variables you can use for build and release pipelines, check these pages: - Build variables - Release variables.

Following is the powershell scripts to modify the build.updatebuildnumber:

$vstsCurrentVersionNumber = $Env:BUILD_BUILDNUMBER
$currentVersionNumber = $vstsCurrentVersionNumber.Split(".")
$revisionNumber = $currentVersionNumber[3]
$newRevisionNumber = [int]$revisionNumber -1
$newVersionNumber = $currentVersionNumber[0] + "." + 
$currentVersionNumber[1] + "." + $currentVersionNumber[2] + "." + 
$newRevisionNumber
$env:VersionNumber = $newVersionNumber

 Write-Host "Update Build Number To: $newVersionNumber"
 Write-Host "##vso[build.updatebuildnumber]$newVersionNumber"

Indeed, if the build/release task failed, the build number will be modified at this building.

However, when we execute the build/release next time, the buildnumber/$(Rev:.r) still increase based on the last failed build result:

enter image description here

As workaround, we could set a value for the nuget package version, like 1.0.0. And add a Inline Powershell task to increase the value of the version by one each time, when we successfully build/release. Do not execute Inline Powershell task when the build/release fails.

Hope this helps.