Azure Pipelines 和 Nuget 包 - 将包版本设置为标记版本

Azure Pipelines & Nuget Packages - Set pack version to tag version

当我将标签推送到存储库时,我设法触发了我的管道。标签始终采用“v(Major).(Minor).(Release)(-alpha)”格式。

如何从标签中获取该信息并将其设置为软件包版本?

编辑 1:
命令 git tag -a 1.0.1 将创建将触发管道的标签,我希望块包版本为“1.0.1”,如标签

在管道内部,您有一个名为标签格式的选项,您可以在其中查看包版本。

请看下面的截图:

您可能必须进行一些解析和错误处理,以便仅针对标签运行,但您可以从 Build.SourceBranch 变量中获取和解析值,如 documentation 所示:

The branch of the triggering repo the build was queued for. Some examples:

  • Git repo branch: refs/heads/master
  • Git repo pull request: refs/pull/1/merge
  • TFVC repo branch: $/teamproject/main
  • TFVC repo gated check-in: Gated_2016-06-06_05.20.51.4369;username@live.com
  • TFVC repo shelveset build: myshelveset;username@live.com
  • When your pipeline is triggered by a tag: refs/tags/your-tag-name

When you use this variable in your build number format, the forward slash characters (/) are replaced with underscore characters _).

Note: In TFVC, if you are running a gated check-in build or manually building a shelveset, you cannot use this variable in your build number format.

这可以在不使用以下 YAML 进行解析和错误处理的情况下完成:

trigger:
  tags:
    include:
    - 0.*
    - 1.*
    - 2.*
    - 3.*
    - 4.*
    - 5.*
    - 6.*
    - 7.*
    - 8.*
    - 9.*

pool: 'LocalWindows'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:

- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
   
- task: NuGetCommand@2
  inputs:
    command: 'pack'
    packagesToPack: '**/Source/*.vbproj'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'BUILD_SOURCEBRANCHNAME'
    includeSymbols: true

特别的是,你必须有一个只监听标签的触发器,然后你可以使用 envvar BUILD_SOURCEBRANCHNAME。

您可以根据需要调整标签包含。