Skip to content

Commit 3ca4e18

Browse files
Address Copilot review: validate version format, gate cleanup on stable release, fix archive path and WhatIf, cleanup zip after upload
1 parent fedb9fd commit 3ca4e18

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ This makes the tested artifact identical to the published artifact (see
4242
| Name | Description | Required | Default |
4343
| -------------------------- | ------------------------------------------------------------------------------------------ | -------- | ---------------- |
4444
| `Name` | Name of the module. Defaults to the repository name. | No | Repository name |
45-
| `ModulePath` | Path to the downloaded module folder. | No | `outputs/module` |
45+
| `ModulePath` | Path to the folder that contains the `<Name>/` module subdirectory (e.g. `outputs/module` must contain `outputs/module/<Name>/`). | No | `outputs/module` |
4646
| `APIKey` | PowerShell Gallery API key. | Yes | |
4747
| `AutoCleanup` | Delete prerelease tags matching the PR branch after a stable release. | No | `true` |
4848
| `WhatIf` | Log the changes that would be made without publishing, creating, or deleting anything. | No | `false` |

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ runs:
7676
run: ${{ github.action_path }}/src/publish.ps1
7777

7878
- name: Cleanup Prereleases
79-
if: inputs.AutoCleanup == 'true' || inputs.WhatIf == 'true'
79+
if: env.PSMODULE_PUBLISH_PSMODULE_CONTEXT_IsPrerelease != 'true' && (inputs.AutoCleanup == 'true' || inputs.WhatIf == 'true')
8080
shell: pwsh
8181
working-directory: ${{ inputs.WorkingDirectory }}
8282
env:
8383
PSMODULE_PUBLISH_PSMODULE_INPUT_WhatIf: ${{ inputs.WhatIf }}
84+
PSMODULE_PUBLISH_PSMODULE_CONTEXT_ReleaseTag: ${{ env.PSMODULE_PUBLISH_PSMODULE_CONTEXT_ReleaseTag }}
8485
run: ${{ github.action_path }}/src/cleanup.ps1

src/cleanup.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ LogGroup 'Load inputs' {
2626
Write-Host "PR head ref: [$prHeadRef]"
2727
Write-Host "Prerelease name: [$prereleaseName]"
2828
Write-Host "WhatIf: [$whatIf]"
29+
30+
$publishedReleaseTag = $env:PSMODULE_PUBLISH_PSMODULE_CONTEXT_ReleaseTag
31+
if (-not [string]::IsNullOrWhiteSpace($publishedReleaseTag)) {
32+
Write-Host "Published tag: [$publishedReleaseTag] (excluded from cleanup)"
33+
}
2934
}
3035
#endregion Load inputs
3136

@@ -37,7 +42,7 @@ LogGroup "Find prereleases to cleanup for [$prereleaseName]" {
3742
exit $LASTEXITCODE
3843
}
3944

40-
$prereleasesToCleanup = $releases | Where-Object { $_.tagName -like "*$prereleaseName*" }
45+
$prereleasesToCleanup = $releases | Where-Object { $_.tagName -like "*$prereleaseName*" -and $_.tagName -ne $publishedReleaseTag }
4146
$tagsToDelete = @($prereleasesToCleanup | ForEach-Object { $_.tagName } | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
4247

4348
if ($tagsToDelete.Count -eq 0) {

src/publish.ps1

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ LogGroup 'Resolve version from manifest' {
7171
Show-FileContent -Path $manifestFilePath
7272

7373
$manifest = Test-ModuleManifest -Path $manifestFilePath -ErrorAction Stop
74-
$moduleVersion = "$($manifest.Version.Major).$($manifest.Version.Minor).$($manifest.Version.Build)"
7574
$manifestData = Import-PowerShellDataFile -Path $manifestFilePath
75+
$moduleVersion = $manifestData.ModuleVersion
76+
if (-not ($moduleVersion -match '^\d+\.\d+\.\d+$')) {
77+
Write-Error "ModuleVersion [$moduleVersion] must be in Major.Minor.Patch format. Ensure Build-PSModule has stamped the artifact with a final version."
78+
exit 1
79+
}
7680
$prerelease = $manifestData.PrivateData.PSData.Prerelease
7781
if ([string]::IsNullOrWhiteSpace($prerelease)) {
7882
$prerelease = ''
@@ -91,6 +95,10 @@ LogGroup 'Resolve version from manifest' {
9195
PRNumber = $prNumber
9296
PRHeadRef = $prHeadRef
9397
} | Format-List | Out-String
98+
99+
# Expose publish context to subsequent steps so the cleanup step can gate on release type.
100+
"PSMODULE_PUBLISH_PSMODULE_CONTEXT_IsPrerelease=$($createPrerelease.ToString().ToLower())" | Out-File -Path $env:GITHUB_ENV -Append -Encoding utf8
101+
"PSMODULE_PUBLISH_PSMODULE_CONTEXT_ReleaseTag=$releaseTag" | Out-File -Path $env:GITHUB_ENV -Append -Encoding utf8
94102
}
95103
#endregion Resolve version from manifest
96104

@@ -194,17 +202,19 @@ LogGroup 'Create GitHub release' {
194202
if (Test-Path -Path $zipPath) {
195203
Remove-Item -Path $zipPath -Force
196204
}
197-
Write-Host "Compressing module to [$zipPath]"
198-
Compress-Archive -Path (Join-Path $modulePath '*') -DestinationPath $zipPath -Force
199-
200205
if ($whatIf) {
206+
Write-Host "WhatIf: Compress-Archive -Path $modulePath -DestinationPath $zipPath -Force"
201207
Write-Host "WhatIf: gh release upload $releaseTag $zipPath --clobber"
202208
} else {
209+
Write-Host "Compressing module to [$zipPath]"
210+
Compress-Archive -Path $modulePath -DestinationPath $zipPath -Force
211+
203212
gh release upload $releaseTag $zipPath --clobber
204213
if ($LASTEXITCODE -ne 0) {
205214
Write-Error "Failed to upload module artifact to release [$releaseTag]."
206215
exit $LASTEXITCODE
207216
}
217+
Remove-Item -Path $zipPath -Force
208218
Write-Host "::notice title=📦 Attached module artifact to release::$zipFileName"
209219
}
210220

0 commit comments

Comments
 (0)