generated from ebekker/pwsh-github-action-base
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Step-GitVersionTag.ps1
69 lines (60 loc) · 2.33 KB
/
Step-GitVersionTag.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
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$MajorVerTag,
[string]$Ref=$null,
[switch]$Apply
)
if (-not $Ref) {
Write-Verbose "No Ref specified, resolving latest tag matching SemVer version"
if ($MajorVerTag -notmatch 'v[0-9]+') {
Write-Warning "MajorVerTag does NOT specify a major semver pattern"
Write-Warning "Ref cannot be resolved and MUST be specified"
exit 1
}
$tags = git tag --list
if (-not $tags) {
Write-Warning "Could not find any existing tags"
}
else {
Write-Verbose "Found [$($tags.Length)] tag(s)"
$semverTags = $tags | Where-Object {
$_ -match '^v[0-9]+(\.[0-9]+){1,3}$' -and $_.StartsWith($MajorVerTag)
} | ForEach-Object {
## Drop the leading 'v' and convert to a Version
## instance so we can do properly ordered compare
New-Object -TypeName psobject -Property @{
tag = $_
ver = [version]::new($_.Substring(1))
}
} | Sort-Object -Descending -Property ver
$latestTag = $semverTags | Select-Object -First 1
if (-not $latestTag) {
Write-Warning "Found [$($tags.Length)] tag(s) but no version matching tags"
Write-Warning "Ref cannot be resolved and must be specified"
exit 1
}
$Ref = $latestTag.tag
Write-Verbose "Resolved latest version-matching semver tag as [$($Ref)]"
}
}
if (-not $Apply) {
Write-Warning "Apply switch was not given, here's what I would do (VERBOSE):"
Write-Verbose "Delete existing tag locally and remotely:"
Write-Verbose " git tag -d $MajorVerTag"
Write-Verbose " git push origin --delete $MajorVerTag"
Write-Verbose "(Re-)Creating tag locally and remotely:"
Write-Verbose " git tag $MajorVerTag $Ref"
Write-Verbose " git push origin $MajorVerTag $Ref"
}
else {
Write-Warning "Apply switch was given, applying changes"
Write-Information "Delete existing tag locally and remotely:"
& git tag -d $MajorVerTag
& git push origin --delete $MajorVerTag
## Alternate (less intuitive) way to delete remove tag:
#git push origin :refs/tags/$MajorVerTag
Write-Information "(Re-)Creating tag locally and remotely:"
& git tag $MajorVerTag $Ref
& git push origin $MajorVerTag $Ref
}