Skip to content

Commit 0f74b90

Browse files
committed
ci: Use GitVersion instead of custom version number logic
1 parent 4b4bcfa commit 0f74b90

File tree

4 files changed

+94
-161
lines changed

4 files changed

+94
-161
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"Codespace", // GitHub Codespaces
1616
"Codespaces", // GitHub Codespaces
1717
"devcontainer", // VS Code devcontainer
18+
"gittools", // GitHub action author
1819
"Hmmss", // Time format
1920
"jacoco", // Java code coverage
2021
"madrapps", // GitHub action author

.github/workflows/build-and-test-powershell-module.yml

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,48 @@ jobs:
5252
steps:
5353
- name: Checkout the repo source code
5454
uses: actions/checkout@v4
55+
with:
56+
fetch-depth: 0 # Fetch all history so that GitVersion can determine the version number.
57+
58+
- name: Display PowerShell version and OS details in case needed for troubleshooting
59+
shell: pwsh
60+
run: $PSVersionTable
5561

5662
- name: Run spellcheck
5763
uses: streetsidesoftware/cspell-action@v5
5864

59-
- name: Display PowerShell version being used
65+
- name: Install GitVersion
66+
uses: gittools/actions/gitversion/setup@v0
67+
with:
68+
versionSpec: '5.x'
69+
70+
- name: Get git metadata used to determine new version number
71+
id: git-version
72+
uses: gittools/actions/gitversion/execute@v0
73+
74+
- name: Determine the new version number
6075
shell: pwsh
61-
run: $PSVersionTable
76+
run: |
77+
[string] $newVersionNumber = '${{ steps.git-version.outputs.majorMinorPatch }}'
78+
[string] $prereleaseLabel = '${{ steps.git-version.outputs.preReleaseTag }}'
79+
80+
[string] $manuallyProvidedVersionNumber = '${{ inputs.versionNumber }}'
81+
if (-not [string]::IsNullOrWhiteSpace($manuallyProvidedVersionNumber)) {
82+
Write-Output "Using manually provided version number '$manuallyProvidedVersionNumber'."
83+
$newVersionNumber = $manuallyProvidedVersionNumber
84+
}
85+
86+
# The preReleaseTag is empty when building the default branch, so manually create a prerelease version number if needed.
87+
if ([string]::IsNullOrWhiteSpace($prereleaseLabel)) {
88+
[string] $dateTime = (Get-Date -Format 'yyyyMMddTHHmmss')
89+
$prereleaseLabel = 'CI' + $dateTime + 'SHA' + '${{ steps.git-version.outputs.shortSha }}'
90+
}
91+
# PowerShell prerelease labels can only contain the characters 'a-zA-Z0-9', so sanitize it if needed.
92+
$newVersionNumberPrereleaseLabel = $prereleaseLabel -replace '[^a-zA-Z0-9]', ''
93+
94+
Write-Output "Setting new environment variables 'NewVersionNumberMajorMinorPatch=$newVersionNumber' and 'NewVersionNumberPrereleaseLabel=$newVersionNumberPrereleaseLabel'."
95+
"NewVersionNumberMajorMinorPatch=$newVersionNumber" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
96+
"NewVersionNumberPrereleaseLabel=$newVersionNumberPrereleaseLabel" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
6297
6398
- name: Run PowerShell linter with PSScriptAnalyzer
6499
shell: pwsh
@@ -97,45 +132,6 @@ jobs:
97132
min-coverage-overall: 60
98133
min-coverage-changed-files: 60
99134

100-
- name: Determine new version from Git tag and set environment variables
101-
shell: pwsh
102-
run: |
103-
[string] $ciVersionTagPrefix = $Env:ciVersionTagPrefix
104-
[string] $ciTagSearchPattern = $ciVersionTagPrefix + '*'
105-
106-
Write-Output "Fetching all git tags, in case they were not included with the git checkout."
107-
& git fetch --tags origin
108-
109-
Write-Output "Searching for CI version git tag by using search pattern '$ciTagSearchPattern'."
110-
[string] $ciVersionTag = (& git tag --list $ciTagSearchPattern)
111-
Write-Output "CI version git tag is '$ciVersionTag'."
112-
113-
if ([string]::IsNullOrWhiteSpace($ciVersionTag))
114-
{
115-
Write-Output "No CI version git tag was found. Assuming this is the first CI build and setting the version number to 0.0.0."
116-
$ciVersionTag = $ciVersionTagPrefix + '0.0.0'
117-
}
118-
119-
[string] $previousVersionNumber = $ciVersionTag -replace $ciVersionTagPrefix, ''
120-
121-
Write-Output "Previous version number was '$previousVersionNumber'. Determining what the new version number should be."
122-
[string] $newVersionNumber = '${{ inputs.versionNumber }}'
123-
if ([string]::IsNullOrWhiteSpace($newVersionNumber))
124-
{
125-
Write-Output "No version number was provided. Incrementing the previous version number."
126-
$currentVersion = [System.Version]::new($previousVersionNumber)
127-
$newVersion = [System.Version]::new($currentVersion.Major, $currentVersion.Minor, $currentVersion.Build + 1)
128-
$newVersionNumber = $newVersion.ToString()
129-
}
130-
else
131-
{
132-
Write-Output "Version number '$newVersionNumber' was provided. Using it as the new version number."
133-
}
134-
135-
Write-Output "Setting new environment variables 'NewVersionNumber=$newVersionNumber' and 'PreviousCiVersionTag=$ciVersionTag'."
136-
"NewVersionNumber=$newVersionNumber" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
137-
"PreviousCiVersionTag=$ciVersionTag" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
138-
139135
- name: Create Stable and Prerelease module artifacts
140136
shell: pwsh
141137
run: |
@@ -145,9 +141,8 @@ jobs:
145141
[string] $moduleManifestFileName = $moduleName + '.psd1'
146142
[string] $prereleaseArtifactModuleDirectoryPath = Join-Path -Path $Env:prereleaseModuleArtifactDirectoryPath -ChildPath $moduleName
147143
[string] $stableArtifactModuleDirectoryPath = Join-Path -Path $Env:stableModuleArtifactDirectoryPath -ChildPath $moduleName
148-
149-
Write-Output "Reading in dynamic environment variables."
150-
[string] $newVersionNumber = $Env:NewVersionNumber
144+
[string] $newVersionNumber = $Env:NewVersionNumberMajorMinorPatch
145+
[string] $newVersionNumberPrereleaseLabel = $Env:NewVersionNumberPrereleaseLabel
151146
152147
Write-Output "Copying the module files to the Prerelease artifact directory '$prereleaseArtifactModuleDirectoryPath'."
153148
Copy-Item -Path $moduleDirectoryPath -Destination $prereleaseArtifactModuleDirectoryPath -Exclude '*.Tests.ps1' -Recurse -Force
@@ -160,14 +155,8 @@ jobs:
160155
[string] $prereleaseManifestFilePath = Join-Path -Path $prereleaseArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
161156
[string] $stableManifestFilePath = Join-Path -Path $stableArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
162157
163-
Write-Output "Determining prerelease version number."
164-
# Prerelease version numbers can only contain 'a-zA-Z0-9' characters.
165-
[string] $dateTime = (Get-Date -Format 'yyyyMMddTHHmmss')
166-
[string] $truncatedCommitSha = $Env:GITHUB_SHA.Substring(0, 7)
167-
[string] $prereleaseVersionNumberPostfix = 'ci' + $dateTime + 'SHA' + $truncatedCommitSha
168-
169-
Write-Output "Updating the prerelease manifest's version number to '$newVersionNumber$prereleaseVersionNumberPostfix'."
170-
Update-ModuleManifest -Path $prereleaseManifestFilePath -ModuleVersion $newVersionNumber -Prerelease $prereleaseVersionNumberPostfix
158+
Write-Output "Updating the prerelease manifest's version number to '$newVersionNumber-$newVersionNumberPrereleaseLabel'."
159+
Update-ModuleManifest -Path $prereleaseManifestFilePath -ModuleVersion $newVersionNumber -Prerelease $newVersionNumberPrereleaseLabel
171160
172161
Write-Output "Updating the stable manifest's version number to '$newVersionNumber'."
173162
Update-ModuleManifest -Path $stableManifestFilePath -ModuleVersion $newVersionNumber
@@ -187,28 +176,18 @@ jobs:
187176
Write-Output "Copying the deployment files '$deployFilesDirectoryPath' to the deployment artifact directory '$deployFilesArtifactDirectoryPath'."
188177
Copy-Item -Path $deployFilesDirectoryPath -Destination $deployFilesArtifactDirectoryPath -Recurse -Force
189178
190-
- name: Update CI version tag and set new version tag
179+
- name: Set the new version tag
191180
# Only run this step if we are doing a push (not a PR) to the default branch (e.g. main).
192181
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
193182
shell: pwsh
194183
run: |
195-
[string] $previousCiVersionTag = $Env:PreviousCiVersionTag
196-
[string] $newVersionNumber = $Env:NewVersionNumber
197-
[string] $newCiVersionTag = $Env:ciVersionTagPrefix + $newVersionNumber
184+
[string] $newVersionNumber = $Env:NewVersionNumberMajorMinorPatch
198185
[string] $newVersionTag = "v$newVersionNumber"
199186
200187
# To avoid a 403 error on 'git push', ensure you have granted your GitHub Actions workflow read/write permission.
201188
# In your GitHub repo: Settings > Actions > General > Workflow permissions > Read and write permissions
202189
# https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#configuring-the-default-github_token-permissions
203190
204-
Write-Output "Removing the previous CI version tag '$previousCiVersionTag'."
205-
& git tag -d $previousCiVersionTag
206-
& git push origin --delete $previousCiVersionTag
207-
208-
Write-Output "Creating new CI version tag '$newCiVersionTag'."
209-
& git tag $newCiVersionTag
210-
& git push origin $newCiVersionTag
211-
212191
Write-Output "Tagging commit with new version tag '$newVersionTag'."
213192
& git tag $newVersionTag
214193
& git push origin $newVersionTag

src/ScriptModuleRepositoryTemplate/TemplateRepoFiles/.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"Codespace", // GitHub Codespaces
1616
"Codespaces", // GitHub Codespaces
1717
"devcontainer", // VS Code devcontainer
18+
"gittools", // GitHub action author
1819
"Hmmss", // Time format
1920
"jacoco", // Java code coverage
2021
"madrapps", // GitHub action author

0 commit comments

Comments
 (0)