如何在 AppVeyor 中使用 git 分支作为构建编号

How to use the git branch as build nr in AppVeyor

在 AppVeyor 中,我想使用 Github 中的版本号设置内部版本号。这将用于 AssemblyVersion 补丁。

我正在使用 GitFlow,并且有一个发布分支。在 SourceTree 中,我创建了一个新版本,例如 v1.2,它创建了分支 release/v1.2。 我想在 AppVeyor 中使用的 1.2 部分。

喜欢,构建版本格式:{gitRelease}.{build}

要实现这一点,

  1. 我需要检索 gitHub 分支名称,
  2. 从中提取版本号,
  3. 将其放入变量 {gitRelease}
  4. 运行 这在 assemblyVersion 修补之前

但我找不到如何做到这一点。

对于构建本身,我有一个构建脚本,用于将输出(nuget 包)发送到 Octopus Deploy,这意味着没有 Before build 脚本部分。

您可以从环境变量中获取版本号,处理它并使用 AppVeyor build worker 发回 API:

$version = $env:appveyor_build_version
# ... do something with it
Update-AppveyorBuild -Version $version

在 AppVeyor 支持人员的帮助下,我成功了。 1733-how-to-call-the-assemblyversion-patch-from-the-build-script

我必须使用 appveyor.yml 中的初始化部分。 我为 UpdateBuild - 版本使用了单独的 cmd 行(我在 qoutes 上遇到了一些问题)。

init:
- cmd: "set appVeyorBuildVersion=%appveyor_build_version%\necho appVeyorBuildVersion:%appVeyorBuildVersion% \n\nset branch=%APPVEYOR_REPO_BRANCH%\necho branch:%branch%\n\nset gitVersion=%branch:~-3%\necho gitversion:%gitVersion%\n\nset newVersion=%gitVersion%.%APPVEYOR_BUILD_NUMBER%\necho %newVersion%\n\n"
- cmd: appveyor UpdateBuild -Version "%newVersion%"

assembly_info:
  patch: true
  file: '**\AssemblyInfo.*'
  assembly_version: '{version}'
  assembly_file_version: '{version}'
  assembly_informational_version: '{version}'
build_script:
- cmd: "echo Building version:%appveyor_build_version%"
- cmd: "nuget restore\nmsbuild MySolution.sln /t:build /p:Configuration=Release"

命令行部分(可读性更好):

echo repo branch:%APPVEYOR_REPO_BRANCH%

set branch=%APPVEYOR_REPO_BRANCH%
echo branch:%branch%

set gitVersion=%branch:~-4%
echo gitversion:%gitVersion%


set newVersion=%gitVersion%.%APPVEYOR_BUILD_NUMBER%
echo %newVersion%

appveyor UpdateBuild -Version "%newVersion%"