Azure DevOps运行GitVersion报无法确定开发分支错误如何解决
Azure DevOps 运行 GitVersion 非 master 分支报错排查
问题现象
在Azure DevOps中运行GitVersion时,除master分支外(包含pull request分支等其余所有分支)执行任务均返回报错,已尝试以下两种排查方案均未解决:
- 增加checkout深度
- 通过next-version配置项手动指定下一版本号
报错信息
Gitversion could not determine which branch to treat as the development branch
现有配置
GitVersion.yml 配置内容
mode: Mainline branches: master: regex: master increment: Patch assembly-informational-format: '{MajorMinorPatch}+Branch.{BranchName}{PreReleaseTag}' major-version-bump-message: '\+semver:\s?(breaking|major)' minor-version-bump-message: '\+semver:\s?(feature|minor)' patch-version-bump-message: '\+semver:\s?(fix|patch)' commit-message-incrementing: Enabled tag-prefix: '[vV]' ignore: sha: []
YAML流水线配置片段
- task: gitversion/setup@0 displayName: gitversion/setup inputs: versionSpec: '5.10.3' - task: gitversion/execute@0 displayName: gitversion/execute inputs: useConfigFile: true configFilePath: GitVersion.yml
故障原因
Mainline模式下GitVersion默认会识别名为develop/development的分支作为开发分支,当前配置只定义了master分支的匹配规则,没有配置开发分支、特性分支、PR分支的匹配规则,也没有覆盖默认的开发分支识别配置,导致非master分支运行时找不到对应的开发分支定义,抛出上述错误。
修复方案
修改GitVersion.yml配置,补充缺失的分支规则,根据团队工作流选择对应方案:
方案1:纯主干开发模式适配
如果团队采用纯主干开发、没有独立的develop分支,直接在配置里添加全部分支匹配规则,将master同时识别为开发分支即可:
mode: Mainline branches: master: regex: ^master$ increment: Patch is-mainline: true develop: regex: ^master$ is-source-branch-for: ['feature', 'hotfix', 'pull-request'] feature: regex: ^feature?[/-] increment: Minor hotfix: regex: ^hotfix?[/-] increment: Patch pull-request: regex: ^(pull|pull\-requests|pr)[/-] increment: Inherit assembly-informational-format: '{MajorMinorPatch}+Branch.{BranchName}{PreReleaseTag}' major-version-bump-message: '\+semver:\s?(breaking|major)' minor-version-bump-message: '\+semver:\s?(feature|minor)' patch-version-bump-message: '\+semver:\s?(fix|patch)' commit-message-incrementing: Enabled tag-prefix: '[vV]' ignore: sha: []
方案2:Git Flow工作流适配
如果团队使用标准Git Flow、保留独立develop分支,补全各分支匹配规则即可:
mode: Mainline branches: master: regex: ^master$ increment: Patch is-mainline: true develop: regex: ^dev(elop)?(ment)?$ increment: Minor feature: regex: ^feature?[/-] increment: Minor hotfix: regex: ^hotfix?[/-] increment: Patch pull-request: regex: ^(pull|pull\-requests|pr)[/-] increment: Inherit assembly-informational-format: '{MajorMinorPatch}+Branch.{BranchName}{PreReleaseTag}' major-version-bump-message: '\+semver:\s?(breaking|major)' minor-version-bump-message: '\+semver:\s?(feature|minor)' patch-version-bump-message: '\+semver:\s?(fix|patch)' commit-message-incrementing: Enabled tag-prefix: '[vV]' ignore: sha: []
额外校验项
配置修改后如果仍有问题,检查流水线checkout配置:
- 确认checkout步骤显式设置
fetchDepth: 0,关闭浅克隆拉取全部分支提交记录 - 确认Azure DevOps构建PR的规则中,开启了「构建时拉取源分支完整提交历史」选项
内容的提问来源于stack exchange,提问作者Jake Michael Gosling
相关产品推荐
相关产品推荐

