通过提交增加 Azure Pipelines 版本号

问题描述 投票:0回答:1

我有一个引用两个存储库的 Azure DevOps 管道:

  • code 存储库,其中包含我的应用程序代码
  • config 存储库,其中包含我的环境变量配置文件。

我的想法是,我应该能够进行简单的配置更改并重新部署,而无需更改应用程序的版本号(因为代码没有更改)。但是,我仍然希望能够在版本号中看到配置在运行之间发生了变化。

因此,我正在考虑使用以下格式的版本号:

{major}.{minor}.{codeVersion}.{configVersion}

...具有以下限制:

  • {major}
    {minor}
    由开发者手动设置。
  • 只要
  • {codeVersion}
     没有更改,每次提交到 
    code 存储库时,
    {major}.{minor}
    都应该自动递增。
  • 只要
  • {configVersion}
     没有更改,每次提交到 
    config 存储库时,
    {major}.{minor}.{codeVersion}
    都应该自动递增。

注意:我知道连续快速运行管道两次会导致两次运行的版本号相同。这是可以接受的,因为两者之间没有对任何存储库进行任何更改。 举个例子,我可能期望看到一堆具有以下名称的管道运行:

    MyApp 0.1.0.0
  • 初始管道运行
  • MyApp 0.1.1.0
  • 更改应用程序代码
  • MyApp 0.1.1.1
  • 更改环境配置
  • MyApp 0.1.2.0
  • 更改应用程序代码
  • MyApp 0.1.2.0
  • 代码和配置均未更改
    ...
  • 我已经查看了
counter

函数,但它似乎没有达到我的需要。我的第一次尝试是这样的: name: '$(Build.DefinitionName)-$(major).$(minor).$(codeVersion).$(configVersion)' variables: - major: 0 - minor: 1 - codeVersion: $[counter(format('{0}.{1}', variables['major'], variables['minor']), 0)] - configVersion: $[counter(format('{0}.{1}.{2}', variables['major'], variables['minor'], variables['codeVersion']), 0)]

不幸的是,使用此代码,即使我没有对 
code

存储库进行任何提交,

codeVersion
always 在每次运行时都会发生变化。然后,结果,configVersion 值始终为零。
我尝试了上述方法的一些变体,其中我尝试引用 

$(Build.SourceVersion)

变量来获取 git 提交 ID,但我还没有找到可行的解决方案。

根本问题似乎是,当提供的值

没有

更改时,计数器函数会递增,而我希望它在code存储库has的提交ID更改时递增。

编辑:修正拼写错误

azure-devops azure-pipelines increment versioning azure-pipelines-yaml
1个回答
0
投票

GitVersion工具结合counter

功能自动生成期望的版本号。

  1. GitTools 扩展安装到 Azure DevOps 组织。它提供了安装和执行用于语义版本控制的GitVersion工具的管道任务。

  2. 在应用程序代码存储库的根目录中,添加引用以下示例的
  3. GitVersion.yml

     文件。
    # GitVersion.yml mode: mainline tag-prefix: '[vV]?' assembly-informational-format: '{Major}.{Minor}.{Patch}' major-version-bump-message: '\+semver:\s?(major)' minor-version-bump-message: '\+semver:\s?(minor)' patch-version-bump-message: '\+semver:\s?(code|app)' no-bump-message: '\+semver:\s?(none|skip)' commit-message-incrementing: Enabled update-build-number: false

    • {major}
       是您版本号中的 
      {major}
    • {minor}
       是您版本号中的 
      {minor}
    • {Patch}
       是您版本号中的 
      {codeVersion}
  4. 在应用程序代码存储库的根目录中,添加引用以下示例的管道主 YAML 文件。
  5. # azure-pipelines.yml # Set the CI trigger. trigger: branches: include: - main # Set the environment config repository as a repository resource. # And enable resource trigger. resources: repositories: - repository: config type: git name: envConfig ref: main trigger: branches: include: - main # Generate the part "{configVersion}" of the app version number. variables: configVersion: $[ counter(resources.repositories.self.version, 0) ] steps: - checkout: self fetchDepth: 0 - task: gitversion/setup@3 displayName: 'Install GitVersion' inputs: versionSpec: '5.x' preferLatestVersion: true # Generate the part "{major}.{minor}.{codeVersion}" of the app version number. # The task sets the variable "$(GitVersion.FullSemVer)" to pass the generated value. - task: gitversion/execute@3 displayName: 'Generate Version' # Combine the generated values to get the full app version number. # Update the pipeline build/run number with the full app version number. - bash: | echo "##vso[task.setvariable variable=APP_VERSION;]$(GitVersion.FullSemVer).$(configVersion)" echo "##vso[build.updatebuildnumber]$(GitVersion.FullSemVer).$(configVersion)" displayName: 'Set App Version' - bash: | echo "APP_VERSION = $(APP_VERSION)" displayName: 'Print App Version' . . .

  6. 在应用程序代码存储库中,为最新提交创建标签“
  7. v0.1.0.0

    ”(请参阅“
    从提交视图创建标签”)。

  8. 使用上面的
  9. azure-pipelines.yml

    文件为应用程序代码存储库创建管道。

具有以上配置:

    当您第一次触发管道运行具有标签“
  • v0.1.0.0

    ”的最新提交时,生成的应用程序版本号是“
    0.1.0.0
    ”。

  • 当您将新提交推送到应用程序代码存储库的
  • main

    分支时,CI 触发器会自动触发管道。 ‘{codeVersion}

    +1的数量。例如,完整的应用程序版本号从“v0.1.0.0
    ”更改为“
    v0.1.1.0
    ”。

  • 当您将新提交推送到环境配置存储库的
  • main

    分支时,资源触发器会自动触发管道。 ‘{configVersion}

    +1的数量。例如,完整的应用程序版本号从“v0.1.1.0
    ”更改为“
    v0.1.1.1
    ”。

  • 当您将另一个新提交推送到应用程序代码存储库的
  • main

    分支时,CI 触发器会自动触发管道。 ‘{codeVersion}

    ’的数量
    +1,‘{configVersion}
    ’的数量回到
    0
    。例如,完整的应用程序版本号从“
    v0.1.1.1
    ”更改为“
    v0.1.2.0
    ”。

  • 如果没有提交提交到应用程序代码存储库和环境配置存储库,则管道将不会自动触发。因此,没有生成新的版本号。

enter image description here


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.