gitversion使用

作用

在采用Git版本管理的项目中自动生成语义化版本号。

会读取Git项目的中分支情况和历史记录,根据配置文件生成所需版本号。

安装

下载二进制文件,添加到环境变量中即可使用。

配置

GitVersion Configuration

运行gitversion init后,会在当前目录下建立GitVersion.yml文件。

next-version: 1.0
assembly-versioning-scheme: MajorMinorPatch
assembly-file-versioning-scheme: MajorMinorPatch
assembly-informational-format: '{InformationalVersion}'
mode: ContinuousDelivery
increment: Inherit
continuous-delivery-fallback-tag: ci
tag-prefix: '[vV]'
major-version-bump-message: '\+semver:\s?(breaking|major)'
minor-version-bump-message: '\+semver:\s?(feature|minor)'
patch-version-bump-message: '\+semver:\s?(fix|patch)'
no-bump-message: '\+semver:\s?(none|skip)'
legacy-semver-padding: 4
build-metadata-padding: 4
commits-since-version-source-padding: 4
tag-pre-release-weight: 60000
commit-message-incrementing: Enabled
ignore:
  sha: []
  commits-before: yyyy-MM-ddTHH:mm:ss
merge-message-formats: {}
update-build-number: true
# use a variable if non-null or a fallback value otherwise
assembly-file-versioning-format: '{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber ?? 0}'

# use an environment variable or raise an error if not available
assembly-file-versioning-format: '{Major}.{Minor}.{Patch}.{env:BUILD_NUMBER}'

# use an environment variable if available or a fallback value otherwise
assembly-file-versioning-format: '{Major}.{Minor}.{Patch}.{env:BUILD_NUMBER ?? 42}'

{env:BUILD_NUMBER}表示环境变量,{env:BUILD_NUMBER ?? 42}表示找不到变量后使用该默认值

分支配置

branches:
  main:
    regex:
{ #master}
$|^main$
    mode: ContinuousDelivery
    tag: ''
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: true
    pre-release-weight: 55000
  develop:
    regex:
{ #dev}
(elop)?(ment)?$
    mode: ContinuousDeployment
    label: alpha
    increment: Minor
    prevent-increment-of-merged-branch-version: false
    track-merge-target: true
    tracks-release-branches: true
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 0
  release:
    regex:
{ #releases}
?[/-]
    mode: ContinuousDelivery
    label: beta
    increment: None
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    source-branches: [ 'develop', 'main', 'support', 'release' ]
    tracks-release-branches: false
    is-release-branch: true
    is-mainline: false
    pre-release-weight: 30000
  feature:
    regex:
{ #features}
?[/-]
    mode: ContinuousDelivery
    label: useBranchName
    increment: Inherit
    prevent-increment-of-merged-branch-version: false
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 30000
  pull-request:
    regex: ^(pull|pull\-requests|pr)[/-]
    mode: ContinuousDelivery
    label: PullRequest
    increment: Inherit
    prevent-increment-of-merged-branch-version: false
    tag-number-pattern: '[/-](?<number>\d+)[-/]'
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 30000
  hotfix:
    regex:
{ #hotfix}
(es)?[/-]
    mode: ContinuousDelivery
    label: beta
    increment: Patch
    prevent-increment-of-merged-branch-version: false
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 30000
  support:
    regex:
{ #support}
[/-]
    mode: ContinuousDelivery
    label: ''
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: true
    pre-release-weight: 55000

官方文档对应的是5.12版,最新6.0.0版有区别

  • branch中移除source-branches
  • branch中tag修改为label

自用配置

merge-message-formats: {}
update-build-number: true
semantic-version-format: Strict
mode: ContinuousDeployment
increment: Inherit
tag-prefix: '[vV]'
commit-date-format: 'yyy-MM-ddTHH:mm:ss'

branches:
  main:
    regex:
{ #master}
$|^main$
    mode: ContinuousDelivery
    label: ''
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: true
    pre-release-weight: 55000
  develop:
    regex:
{ #dev}
(elop)?(ment)?$
    mode: ContinuousDeployment
    label: alpha
    increment: Minor
    prevent-increment-of-merged-branch-version: false
    track-merge-target: true
    tracks-release-branches: true
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 0
  release:
    regex:
{ #releases}
?[/-]
    mode: ContinuousDelivery
    label: beta
    increment: None
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: true
    is-mainline: false
    pre-release-weight: 30000
  feature:
    regex:
{ #features}
?[/-]
    mode: ContinuousDelivery
    label: useBranchName
    increment: Inherit
    prevent-increment-of-merged-branch-version: false
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false
    is-mainline: false
    pre-release-weight: 30000

输出变量

{
    "Major": 3,
    "Minor": 22,
    "Patch": 11,
    "PreReleaseTag": "beta.99",
    "PreReleaseTagWithDash": "-beta.99",
    "PreReleaseLabel": "beta",
    "PreReleaseLabelWithDash": "-beta",
    "PreReleaseNumber": 99,
    "WeightedPreReleaseNumber": 1099,
    "BuildMetaData": 88,
    "BuildMetaDataPadded": "0088",
    "FullBuildMetaData": "99.Branch.release/3.22.11.Sha.28c853159a46b5a87e6cc9c4f6e940c59d6bc68a",
    "MajorMinorPatch": "3.22.11",
    "SemVer": "3.22.11-beta.99",
    "LegacySemVer": "3.22.11-beta99",
    "LegacySemVerPadded": "3.22.11-beta0099",
    "AssemblySemVer": "3.22.11.0",
    "AssemblySemFileVer": "3.22.11.0",
    "InformationalVersion": "3.22.11-beta.99+88.Branch.release/3.022.011.Sha.28c853159a46b5a87e6cc9c4f6e940c59d6bc68a",
    "FullSemVer": "3.22.11-beta.99+88",
    "BranchName": "release/3.022.011",
    "EscapedBranchName": "release-3.022.011",
    "Sha": "28c853159a46b5a87e6cc9c4f6e940c59d6bc68a",
    "ShortSha": "28c8531",
    "NuGetVersionV2": "3.22.11-beta0099",
    "NuGetVersion": "3.22.11-beta0099",
    "NuGetPreReleaseTagV2": "beta0099",
    "NuGetPreReleaseTag": "beta0099",
    "VersionSourceSha": "28c853159a46b5a87e6cc9c4f6e940c59d6bc68a",
    "CommitsSinceVersionSource": 7,
    "CommitsSinceVersionSourcePadded": "0007",
    "CommitDate": "2021-12-31",
    "UncommittedChanges": 0
}

命令行使用

visual studio使用

在项目中使用nuget安装GitVersion.MsBuild包,并移除AssemblyInfo.cs文件中的Assembly*Version属性。子任务GitVersion.MsBuild.UpdateAssemblyInfo会添加相关版本信息。