在 Azure Devops 中,您可以使用Replace Tokens Extension 中的替换令牌任务。
然后你可以在 NPM pack 任务之前添加任务来替换 Package.json -> Version field. 中的变量
步骤如下:
Package.Json 文件:
{
"name": "helloword",
"version": "0.0.#{SetNumber}#",
"description": "Hello World",
"main": "server.js",
...
}
构建管道:
- 在构建管道中设置变量。
- 添加替换标记任务。
注意:根据我的测试,npm 包在 azure devops 中不支持 xx.x.x.x 版本格式(npm publish)。它可以支持x.x.x-x。
所以你可以这样设置buildnumber:$(Date:yyyyMMdd)-$(Rev:r).
结果:
更新:
您可以尝试使用 Npm Version 命令。
npm version 0.0.$(build.buildnumber) --no-git-tag-version
更新2:
您可以尝试使用以下 powershell 脚本来获取 Package.json 中的版本字段。然后更新补丁号。
$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"
在 Npm 版本中,您可以运行以下命令:
version $(version) --no-git-tag-version