master
分支有新提交时被触发,并发布软件包。当前版本需要手动设置,我希望自动设置版本。
一开始我的想法是将以下任务添加到管道中:
- 检出
$Build.SourceBranch
- 运行
version patch --force
git push
master
分支有新提交时被触发,并发布软件包。$Build.SourceBranch
version patch --force
git push
我已经按照问题中提到的做法添加了相同的任务,只是做了一些小修改。
显然有一种方法可以跳过管道触发,请参见这里
所以npm version
任务看起来像这样:
version patch -m "Bump version to %s [skip ci]" --force
这会阻止下一次构建被触发。
TIP: 记得授予“作者”(Azure DevOps用户)在推送时绕过策略
的权限,如果有的话。
publish
步骤的依赖,所以如果没有提交,就不会有发布:)。我只能在几个小时后接受它作为答案。 - SagiLow这是我在PowerShell中的实现,完全基于接受答案的步骤。请注意,这也处理了一些git问题,例如与此类方法一起出现的分离头。
steps:
- checkout: self
persistCredentials: true
.
.
.
- task: PowerShell@2
displayName: 'Bump the version'
inputs:
targetType: 'inline'
script: |
$BranchName = "$(Build.SourceBranch)" -replace "refs/heads/"
git checkout $BranchName
git config --global user.email "anymail@anycompany.com"
git config --global user.name "Your name"
npm version prerelease -m "Auto increment pre-release version to %s [skip ci]" --force
git push
git describe --tags
to find the most recent tag with a tagname matching the pattern you use (see below). (I prefer this sequence over calling npm version from-git
because npm will just use the latest tag, which might not be a version number, depending on how much control you have over the branch.) Use string or regular expression operations to extract the major, minor, patch, and whatever pre-* value you might have. This is the previous version that we'll use to compare with what's in the package.json file. Note that you may have to follow these instructions to run a git command. Save it to a pipeline variable.Using a utility command-line task and code or script that you write, run npm version
to get the current major/minor/pre version out of the package.json file and save it to a different pipeline variable. I'm using PowerShell Core, so my command would look something like this to create a "currentPackageVersion" pipeline variable:
& npm.cmd version | ConvertFrom-Json | Select-Object -ExpandProperty {name-of-your-package} | Set-Variable -Name 'packageVersion' | Write-Output "#vso[task.setvariable variable=currentPackageVersion]$packageVersion"
Using a utility command-line task and code or script that you write, compare the previous version's major, minor, and pre-* values to determine whether any of them have changed. Set a new pipeline variable to reflect whether it has changed or not. I'll use the name "restartVersionPatchNumber", which is true if the current major, minor, or pre-* values are different from the previous version's major, minor, or pre-* values.
npm --no-git-tag-version version patch
, which updates package.json in the build agent but does not commit the change, leaving your Working Area modified (dirty) (which might cause issues on subsequent builds if you are using your own build agents instead of hosted agents). The condition expression of the Task uses a custom condition that evaluates to the variable that I just set in the previous step ("restartVersionPatchNumber"). Note that if this task does not run, it should just use the value of the version that is in the package.json file (the current version that is now in the "currentPackageVersion" pipeline variable).npm version
to extract the new version that the npm version command set. Save it to a new pipeline variable; I'll call it "newVersionNumber".git reset --hard
. You'll need to do this even if you're using a hosted build agent, because of the next step.git tag {*tagname*}
. For PowerShell, the syntax would be & git.exe tag $env:newVersionNumber
git push origin {*tagname*}
package.json
文件中,否则用户将无法更新。Git 标签很好,但只适用于自我监控。我能想到的最简单的方法(如果可能的话)是排除 DevOps 用户提交触发管道,但我不确定是否可能。 - SagiLowpackage.json
的版本永远不正确,这很令人困惑。我认为我已经找到了一个更简单的解决方案来解决我的问题,请看下面的答案。非常感谢您的回答! - SagiLow