Skip to content

Commit 9f52d4e

Browse files
Adding Workflow Step TagModuleVersion
1 parent c328daf commit 9f52d4e

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

GitHub/Steps/TagModuleVersion.ps1

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
param(
2+
[string]
3+
$ModulePath,
4+
5+
# The user email associated with a git commit.
6+
[string]
7+
$UserEmail,
8+
9+
# The user name associated with a git commit.
10+
[string]
11+
$UserName,
12+
13+
# The tag version format (default value: 'v$(imported.Version)')
14+
# This can expand variables. $imported will contain the imported module.
15+
[string]
16+
$TagVersionFormat = 'v$($imported.Version)',
17+
18+
# The tag version format (default value: '$($imported.Name) $(imported.Version)')
19+
# This can expand variables. $imported will contain the imported module.
20+
[string]
21+
$TagAnnotationFormat = '$($imported.Name) $($imported.Version)'
22+
)
23+
24+
25+
$gitHubEvent = if ($env:GITHUB_EVENT_PATH) {
26+
[IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json
27+
} else { $null }
28+
29+
30+
@"
31+
::group::GitHubEvent
32+
$($gitHubEvent | ConvertTo-Json -Depth 100)
33+
::endgroup::
34+
"@ | Out-Host
35+
36+
if (-not ($gitHubEvent.head_commit.message -match "Merge Pull Request #(?<PRNumber>\d+)") -and
37+
(-not $gitHubEvent.psobject.properties['inputs'])) {
38+
"::warning::Pull Request has not merged, skipping" | Out-Host
39+
return
40+
}
41+
42+
43+
44+
$imported =
45+
if (-not $ModulePath) {
46+
$orgName, $moduleName = $env:GITHUB_REPOSITORY -split "/"
47+
Import-Module ".\$moduleName.psd1" -Force -PassThru -Global
48+
} else {
49+
Import-Module $modulePath -Force -PassThru -Global
50+
}
51+
52+
if (-not $imported) { return }
53+
54+
$targetVersion =$ExecutionContext.InvokeCommand.ExpandString($TagVersionFormat)
55+
$existingTags = git tag --list
56+
57+
@"
58+
Target Version: $targetVersion
59+
60+
Existing Tags:
61+
$($existingTags -join [Environment]::NewLine)
62+
"@ | Out-Host
63+
64+
$versionTagExists = $existingTags | Where-Object { $_ -match $targetVersion }
65+
66+
if ($versionTagExists) {
67+
"::warning::Version $($versionTagExists)"
68+
return
69+
}
70+
71+
if (-not $UserName) { $UserName = $env:GITHUB_ACTOR }
72+
if (-not $UserEmail) { $UserEmail = "$UserName@github.com" }
73+
git config --global user.email $UserEmail
74+
git config --global user.name $UserName
75+
76+
git tag -a $targetVersion -m $ExecutionContext.InvokeCommand.ExpandString($TagAnnotationFormat)
77+
git push origin --tags
78+
79+
if ($env:GITHUB_ACTOR) {
80+
exit 0
81+
}

0 commit comments

Comments
 (0)