简体   繁体   English

VSTS/Azure DevOps:包上的自动增量 NuGet 包版本

[英]VSTS/Azure DevOps: Auto-Increment NuGet Package Version on Pack

Running the .NET Core Pack task, how do I get the outputted NuGet package version to auto-increment itself?运行 .NET Core Pack 任务,如何让输出的 NuGet 包版本自动递增?

So, for example, if my current version is 1.0.0 , then the next time I call the Pack task, I would like to see 1.0.1 .因此,例如,如果我当前的版本是1.0.0 ,那么下次我调用 Pack 任务时,我希望看到1.0.1

I'm using environment build variables with Build.BuildNumber and getting outputs at the moment of eg 20180913-.2.0 , etc. I would like to establish to a more traditional versioning system.我正在使用Build.BuildNumber环境构建变量并在例如20180913-.2.0等时刻获取输出。我想建立一个更传统的版本控制系统。

From the docs , the variable Rev:.r is the daily build revision count.文档中,变量Rev:.r每日构建修订计数。 The accepted "solution" would lead to one day finishing having a version of 1.0.12 , then the next day it will be 1.0.1 .接受的“解决方案”将导致有一天完成1.0.12的版本,然后第二天它将是1.0.1

If you want a simple incremental and unique semver, use 1.0.$(BuildID) .如果您想要一个简单的增量且唯一的semver,请使用1.0.$(BuildID)

$(BuildID) is an internal immutable counter for your builds, and thus far cleaner than $(BuildNumber) . $(BuildID)是用于构建的内部不可变计数器,迄今为止比$(BuildNumber)干净得多。


BuildID will always be incrementing - no reset. BuildID 将始终递增 - 不会重置。 Thus after a minor bump, you'd end up having say 1.2.123 becoming 1.3.124 .因此,在一次小碰撞之后,您最终会说1.2.123变成1.3.124

If you want to perform this task well, this can be done using npm version or similar, such as pubspec_version for Dart or Flutter builds.如果您想很好地执行此任务,可以使用npm version或类似版本来完成,例如用于 Dart 或 Flutter 构建的pubspec_version

- script: npm version $RELEASE_TYPE

where $RELEASE_TYPE is a variable you can set based on build (ie: CI, PR etc), having a value of major , minor , patch , prerelease etc.其中$RELEASE_TYPE是您可以根据构建(即:CI、PR 等)设置的变量,其值为majorminorpatchprerelease等。

- script: npm version $RELEASE_TYPE
  condition: startsWith(variables['build.sourceBranch'], 'refs/head/release/')
  env:  
    releaseType: minor

Update: Bump Repo Version and Use In Build (using npm)更新:Bump Repo 版本和在构建中使用(使用 npm)

To have the repo version update, I ended up including npm version as a DevDependency, with it's precommit hook to bump the project version on any commit.为了更新 repo 版本,我最终将npm 版本作为 DevDependency 包含在内,它的 precommit 钩子可以在任何提交时提升项目版本。

This technique can be applied to other project types, placing them in a subfolder - although can lead to complications with server OS requirements.这种技术可以应用于其他项目类型,将它们放在一个子文件夹中——尽管可能会导致服务器操作系统要求的复杂化。

To use this version in your build, add this bash script task , which gets and exports the version as a task variable:要在您的构建中使用此版本,请添加此bash 脚本 task ,它将获取并导出版本作为任务变量:

v=`node -p "const p = require('./package.json'); p.version;"`
echo "##vso[task.setvariable variable=packageVersion]$v"

.Net Core Task only version .Net Core Task 唯一版本

Unfortunately, no repo-bump.不幸的是,没有回购。

Workaround 1: 解决方法 1:
 jobs: - job: versionJob #reads version number from the source file steps: - powershell: | $fv = Get-Content versionFile Write-Host ("##vso[task.setvariable variable=versionFromFile;isOutput=true]$fv") displayName: 'version from file' name: setVersionStep - job: buildJob # consumes version number, calculates incremental number and set version using assemblyinfo.cs dependsOn: versionJob variables: versionFromFile: $[ dependencies.versionJob.outputs['setVersionStep.versionFromFile'] ] # please note that spaces required between $[ and dependencies buildIncrementalNumber: $[ counter(dependencies.versionJob.outputs['setVersionStep.versionFromFile'],1) ] #can't use $versionFromFile here steps: - powershell: | Write-Host ($env:versionFromFile) Write-Host ($env:versionFromFile + '.' + $env:buildIncrementalNumber) displayName: 'version from file output'
Workaround 2: 解决方法 2:

This post describes a couple of others, using version-prefix and automatically applying the BuildNumber as a version-suffix . 这篇文章描述了其他几个,使用version-prefix并自动应用 BuildNumber 作为version-suffix

I may have figured it out.我可能已经想通了。 For anyone tearing their hair out, try this:对于任何撕掉头发的人,试试这个:

Pack Task:打包任务:

  • Automatic Package Versioning: Use an environment variable自动包版本控制:使用环境变量
  • Environment variable: Build.BuildNumber环境变量: Build.BuildNumber

Then, up in the top menu where you have Tasks , Variables , Triggers , Options , click Options and set:然后,在有TasksVariablesTriggersOptions的顶部菜单中,单击Options并设置:

  • Build number format: 1.0$(Rev:.r)内部版本号格式:1.0$(Rev:.r)

Save and queue.保存并排队。 This will produce eg 1.0.1.这将产生例如 1.0.1。

(Please Correct me if I am wrong, or if this does not work long-term.) (如果我错了,或者这不能长期工作,请纠正我。)

If you're just looking to bump the major, minor or revision version number, using counter operator in a variable is a simple and elegant approach.如果您只是想提高主要、次要或修订版本号,在变量中使用counter运算符是一种简单而优雅的方法。 It will automatically add one to the current value.它将自动将当前值加一。

Here's what I use:这是我使用的:

variables:
  major: '1'
  minor: '0'
  revision: $[counter(variables['minor'], 1)] #this will get reset when minor gets bumped. The number after Counter is the seed number (in my case, I started at 1).
  app_version: '$(major).$(minor).$(revision)'

If you would like to see a real-world 4-job pipeline that uses this, I have one here https://github.com/LanceMcCarthy/DevReachCompanion/blob/master/azure-pipelines.yml如果您想查看使用它的真实世界 4 作业管道,我在这里有一个https://github.com/LanceMcCarthy/DevReachCompanion/blob/master/azure-pipelines.yml

For me it's enough to set Build number format on Options tab to对我来说,将“选项”选项卡上的“内部版本号”格式设置为

$(date:yyyy).$(date:MMdd)$(rev:.r) 

and add next build argument:并添加下一个构建参数:

/p:Version=1.$(Build.BuildNumber) /p:AssemblyVersion=1.$(Build.BuildNumber)

In this case we manage major version manually, but minor version and build number will be set automatically.在这种情况下,我们手动管理主要版本,但会自动设置次要版本和内部版本号。 Easy to understand what version you have deployed.易于理解您部署的版本。

I am using the ado pipeline and a yaml build.我正在使用 ado 管道和 yaml 构建。 What I've done is utilized the pipeline variables, a counter function, and an inline powershell function to create the version number.我所做的是利用管道变量、计数器函数和内联 powershell 函数来创建版本号。 It auto-increments and has made the entire build process nice.它会自动递增并使整个构建过程变得很好。

Another SO Post about something similar另一个关于类似内容的 SO 帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM