-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-gitversion.ps1
111 lines (103 loc) · 3.93 KB
/
azure-gitversion.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Generates a version number from Git tags and/or branch names following GitVersion/GitFlow approach
#
# Init global consts
$TagPrefix = "v-"
$DevelopSuffix = "develop"
$ReleaseSuffix = "preview"
$HotfixSuffix = "hotfix"
# Init global variables
$MajorVersion = 0
$MinorVersion = 0
$PatchVersion = 1
$VersionSuffix = ""
$NumberOfCommits = 0
# RegEx patterns
$GitDescribePattern = "(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(\-(?<commits>0|[1-9]\d*))?"
$GitBranchNamePattern = "(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)"
# Get the current git branch from Azure DevOps
$CurrentBranch = $Env:BUILD_SOURCEBRANCH
$CurrentBranchName = $Env:BUILD_SOURCEBRANCHNAME
# Try to extract version info from Git tag
$Tag = git describe --tags --match "$TagPrefix[0-9]*"
if ($Tag)
{
if ($Tag -match $GitDescribePattern)
{
$MajorVersion = [int]$Matches.major
$MinorVersion = [int]$Matches.minor
$PatchVersion = [int]$Matches.patch
$NumberOfCommits = [int]$Matches.commits
}
else
{
throw "Could not extract version info from Git tag " + $Tag
}
}
else
{
# Git git rev-list --count head
$NumberOfCommits = git rev-list --count head
}
# Try to calculate semantic version based on GitFlow branching
if ($CurrentBranch -match "main")
{
$SemVersion = [string]$MajorVersion + "." + [string]$MinorVersion + "." + [string]$PatchVersion
}
elseif ($CurrentBranch -match "develop")
{
$PatchVersion = $PatchVersion + 1
$VersionSuffix = if ($NumberOfCommits -eq 0) { "-" + $DevelopSuffix } else { "-" + $DevelopSuffix + "." + [string]$NumberOfCommits }
$SemVersion = [string]$MajorVersion + "." + [string]$MinorVersion + "." + [string]$PatchVersion + $VersionSuffix
}
elseif ($CurrentBranch -match "hotfix")
{
if ($CurrentBranchName -match $GitBranchNamePattern)
{
$MajorVersion = [int]$Matches.major
$MinorVersion = [int]$Matches.minor
$PatchVersion = [int]$Matches.patch
$VersionSuffix = if ($NumberOfCommits -eq 0) { "-" + $HotfixSuffix } else { "-" + $HotfixSuffix + "." + [string]$NumberOfCommits }
$SemVersion = [string]$MajorVersion + "." + [string]$MinorVersion + "." + [string]$PatchVersion + $VersionSuffix
}
else
{
throw "Could not extract version info from Git branch name " + $CurrentBranchName
}
}
elseif ($CurrentBranch -match "release")
{
if ($CurrentBranchName -match $GitBranchNamePattern)
{
$MajorVersion = [int]$Matches.major
$MinorVersion = [int]$Matches.minor
$PatchVersion = [int]$Matches.patch
$VersionSuffix = if ($NumberOfCommits -eq 0) { "-" + $ReleaseSuffix } else { "-" + $ReleaseSuffix + "." + [string]$NumberOfCommits }
$SemVersion = [string]$MajorVersion + "." + [string]$MinorVersion + "." + [string]$PatchVersion + $VersionSuffix
}
else
{
throw "Could not extract version info from Git branch name " + $CurrentBranchName
}
}
elseif ($CurrentBranch -match "features")
{
$VersionSuffix = if ($NumberOfCommits -eq 0) { "-" + $CurrentBranchName } else { "-" + $CurrentBranchName + "." + [string]$NumberOfCommits }
$SemVersion = [string]$MajorVersion + "." + [string]$MinorVersion + "." + [string]$PatchVersion + $VersionSuffix
}
# Log output
Write-Host "Branch: $CurrentBranch"
Write-Host "Branch name: $CurrentBranchName"
Write-Host "Git describe: $Tag"
Write-Host "Number of commits: $NumberOfCommits"
Write-Host "Major version: $MajorVersion"
Write-Host "Minor version: $MinorVersion"
Write-Host "Patch version: $PatchVersion"
Write-Host "Version Suffix: $VersionSuffix"
Write-Host "Sem version: $SemVersion"
# Set Azure DevOps variables
Write-Host "##vso[task.setvariable variable=MajorVersion]$MajorVersion"
Write-Host "##vso[task.setvariable variable=MinorVersion]$MinorVersion"
Write-Host "##vso[task.setvariable variable=PatchVersion]$PatchVersion"
Write-Host "##vso[task.setvariable variable=VersionSuffix]$VersionSuffix"
Write-Host "##vso[task.setvariable variable=SemVersion]$SemVersion"