##[错误]在以下环境变量中找不到版本号数据:BUILD_BUILDNUMBER

##[error]Could not find version number data in the following environment variable: BUILD_BUILDNUMBER

我正在使用下面的 yaml 生成我的 NuGet pkg。我想为我的包裹设置一个带有日期的唯一 ID。但我一直收到一条错误消息,说无法在以下环境变量中找到版本号数据:BUILD_BUILDNUMBER

##[error]Could not find version number data in the following environment variable: BUILD_BUILDNUMBER. The value of the variable should contain a substring with or are positive integers.

我尝试使用名称:$(Build.DefinitionName)-$(date:yyyyMMdd)$(rev:.r) # byBuildNumber verisonScheme nuget pack 需要这个。

还尝试将 BUILD_BUILDNUMBER 设置为 yaml 文件中的变量。

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

name: $(Build.DefinitionName)-$(date:yyyyMMdd)$(rev:.r) # need this for byBuildNumber verisonScheme nuget pack
# the build will trigger on any changes to the master branch
trigger:
- master

# the build will run on a Microsoft hosted agent, using the lastest Windows VM Image
pool:
  vmImage: 'windows-latest'

# these variables are available throughout the build file
# just the build configuration is defined, in this case we are building Release packages
variables:
  buildConfiguration: 'Release'


#The build has 3 seperate tasks run under 1 step
steps:
# The first task is the dotnet command build, pointing to our csproj file
- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    command: 'build'
    arguments: '--configuration $(buildConfiguration)'
    projects: 'src/Repository.sln'

# The second task is dotnet pack command again pointing to the csproj file
# The nobuild means the project will not be compiled before running pack, because its already built in above step
- task: DotNetCoreCLI@2
  displayName: "dotnet pack"
  inputs:
    command: 'pack'
    arguments: '--configuration $(buildConfiguration)'
    packagesToPack: 'src/Common.Core/Common.Core.csproj'
    nobuild: true
    includeSymbols: true
    versioningScheme: 'byBuildNumber'


# The last task is a nuget command, nuget push
# This will push any .nupkg files to the 'Nuget' artifact feed
# allowPackageConflicts allows us to build the same version and not throw an error when trying to push
# instead it just ingores the latest package unless the version changes
- task: NuGetCommand@2
  displayName: 'nuget push'
  inputs:
    command: 'push'
    feedsToUse: 'select'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'MyNuget'
    allowPackageConflicts: true


我希望它生成具有正确版本号的 nuget 而不会在 nuget pack 步骤上失败。

当您选择 NuGet 包版本时,您不能将此值 $(Build.DefinitionName)-$(date:yyyyMMdd)$(rev:.r) 赋给内部版本号。

应该是这样的格式:

$(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r).

您可以在经典编辑器中看到此信息:

您可以在 docs 中找到更多信息:

对于 byPrereleaseNumber,版本将设置为您为主要版本、次要版本和补丁程序选择的任何版本,加上格式为 yyyymmdd-hhmmss 的日期和时间。

对于 byEnvVar,版本将被设置为任何环境变量,例如MyVersion(没有 $,只是环境变量名称),您提供。确保环境变量设置为正确的 SemVer,例如1.2.3 或 1.2.3-beta1.

对于 byBuildNumber,版本将设置为内部版本号,确保您的内部版本号是正确的 SemVer,例如1.0.$(修订版:r)。如果您 select byBuildNumber,任务将提取带点的版本 1.2.3.4 并仅使用它,删除任何标签。要按原样使用内部版本号,您应该如上所述使用 byEnvVar,并将环境变量设置为 BUILD_BUILDNUMBER.