在 VSTS 构建过程中使用“dotnet pack”将构建号添加到包版本

Add build number to package version with `dotnet pack` in VSTS Build process

使用 .NET Framework 库,您可以使用通配符指定版本,当 运行 VSTS 中的 NUGET 构建任务时,NUGET pack 命令会自动附加构建日期和版本。

[assembly: AssemblyVersion("1.0.*")]

NUGET PACK 会生成一个 NUPKG 文件,其版本如 1.0.6604.1234 附加日期编号和构建 ID。

NET 标准问题

在 .NET Core 和 .NET 标准中,新的 .csproj 格式不支持这种通配符格式。

我们不能用 Nuget.exe 打包(原因:this issue)但我们可以使用 dotnet pack 除非我需要自动增加内部版本号。 dotnet VSTS 中的构建任务允许我完全替换版本号,但我想保留 csproj 文件中的版本,并且只附加一个内部版本号(就像我以前那样)。

我发现在 csproj 文件中使用 <VersionPrefix>x.y</VersionPrefix> 可以与 nuget pack 一起使用,然后我可以将附加参数 VersionSuffix=$(Build.BuildNumber) 添加到 pack 任务。

在第一个开发人员在项目属性对话框中更新项目版本之前,一切看起来都很好。 Visual Studio 忽略了 VersionPrefix 并设置了 <Version> 标签 - 内部版本号修复被忽略,因为存在 Version 标签。

有没有办法从 csproj 中读取 Version?如果是这样,我可以将构建 属性 设置为 Version=$(ProjectVersion).$(Build.BuildNumber) ?

或者是否有其他方法可以在打包时处理自动递增构建版本?

首先你可以 select Use an environment variable 用于 自动包版本控制 ,使用你定义的变量,例如 temp ($(build.buildNumber) ) 作为环境变量.

更多细节看这个link:Dotnet pack automatic package versioning build number clarification

另一种方法是在 dotnet CLI 任务中使用 "arguments" 字段,您可以将其他参数传递给 dotnet cli。

Using --version-suffix $(Build.BuildNumber) will pass the build number as version suffix. Make sure you don't have a <version> element set in your csproj, but rather a <versionprefix> element. The built version will look like versionprefix-versionsuffix, so for example, if you have <versionprefix>1.2.3</versionprefix> and build number 201805002, the built version will be 1.2.3-201805002. In this case do not select the automatic package versioning.

感谢@patricklu-msft 的建议。

似乎没有内置的方法来模拟我们之前使用 NUGET packdotnet pack 的通配符行为,也没有办法将 <Version> 标记从项目文件。

所以我创建了一个新的 VSTS 构建任务来执行此操作:VersionTaskReader 在 MarketPlace 中。

此扩展可以指向 .csproj.vbproj 并将设置一个环境变量 VERSION 和附加了 BUILDIDVERSION_BUILD .如果需要,您可以选择添加前缀以使每个实例不同。

例如,如果您的项目包含 <Version>1.1</Version>,那么 VERSION_BUILD 将类似于 1.1.8680

然后 dotnet pack 任务可以在版本控制选项屏幕中使用环境变量 VERSION_BUILD,以便内部版本号自动递增。