Skip to content

Commit 12425f8

Browse files
🩹 [Patch]: Add more logging (#43)
## Description This pull request includes several changes to the `Publish-PSModule` script and the `action.yml` configuration file to improve logging. ### Improvements to the `Publish-PSModule` script: * Added new module dependencies (`Retry`, `GitHub`) to the `#Requires` statement in `scripts/helpers/Publish-PSModule.ps1`. * Added `[OutputType([void])]` attribute to the `Publish-PSModule` function to specify the output type. * Replaced `Write-Verbose` with `Write-Output` for better visibility of key process steps and outputs. [[1]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L162-R192) [[2]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L199-R221) [[3]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L240-R246) [[4]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L282-R301) [[5]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L308-R323) [[6]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L332-R345) [[7]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L363-R376) * Introduced retry logic using the `Retry` module when fetching the latest version from PSGallery to handle transient errors. ### Changes to the `action.yml` configuration file: * Updated the step name from `Run Build-PSModule` to `Run Publish-PSModule`. * Simplified the script path reference by removing unnecessary dot notation. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 8025201 commit 12425f8

File tree

2 files changed

+57
-44
lines changed

2 files changed

+57
-44
lines changed

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ inputs:
7979
runs:
8080
using: composite
8181
steps:
82-
- name: Run Build-PSModule
82+
- name: Run Publish-PSModule
8383
uses: PSModule/GitHub-Script@v1
8484
env:
8585
GITHUB_ACTION_INPUT_Name: ${{ inputs.Name }}
@@ -103,4 +103,4 @@ runs:
103103
Version: ${{ inputs.Version }}
104104
Script: |
105105
# Publish-PSModule
106-
. "${{ github.action_path }}\scripts\main.ps1"
106+
${{ github.action_path }}\scripts\main.ps1

scripts/helpers/Publish-PSModule.ps1

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#REQUIRES -Modules Utilities, PowerShellGet, Microsoft.PowerShell.PSResourceGet
1+
#Requires -Modules Utilities, PowerShellGet, Microsoft.PowerShell.PSResourceGet, Retry, GitHub
22

33
function Publish-PSModule {
44
<#
@@ -11,6 +11,7 @@ function Publish-PSModule {
1111
.EXAMPLE
1212
Publish-PSModule -Name 'PSModule.FX' -APIKey $env:PSGALLERY_API_KEY
1313
#>
14+
[OutputType([void])]
1415
[CmdletBinding()]
1516
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
1617
'PSReviewUnusedParameter', '', Scope = 'Function',
@@ -159,31 +160,36 @@ function Publish-PSModule {
159160
Write-Warning 'Could not find the latest release version. Using ''0.0.0'' as the version.'
160161
$ghReleaseVersion = New-PSSemVer -Version '0.0.0'
161162
}
162-
Write-Verbose '-------------------------------------------------'
163-
Write-Verbose 'GitHub version:'
164-
Write-Verbose ($ghReleaseVersion | Format-Table | Out-String)
165-
Write-Verbose $ghReleaseVersion.ToString()
166-
Write-Verbose '-------------------------------------------------'
163+
Write-Output '-------------------------------------------------'
164+
Write-Output 'GitHub version:'
165+
Write-Output ($ghReleaseVersion | Format-Table | Out-String)
166+
Write-Output $ghReleaseVersion.ToString()
167+
Write-Output '-------------------------------------------------'
167168
}
168169

169170
LogGroup 'Get latest version - PSGallery' {
170171
try {
171-
$psGalleryVersion = New-PSSemVer -Version (Find-PSResource -Name $Name -Repository PSGallery -Verbose:$false).Version
172+
Retry -Count 5 -Delay 10 {
173+
$latest = Find-PSResource -Name $Name -Repository PSGallery -Verbose:$false
174+
} -Catch {
175+
Write-Output $_
176+
}
177+
$psGalleryVersion = New-PSSemVer -Version $latest.Version
172178
} catch {
173179
Write-Warning 'Could not find module online. Using ''0.0.0'' as the version.'
174180
$psGalleryVersion = New-PSSemVer -Version '0.0.0'
175181
}
176-
Write-Verbose '-------------------------------------------------'
177-
Write-Verbose 'PSGallery version:'
178-
Write-Verbose ($psGalleryVersion | Format-Table | Out-String)
179-
Write-Verbose $psGalleryVersion.ToString()
180-
Write-Verbose '-------------------------------------------------'
182+
Write-Output '-------------------------------------------------'
183+
Write-Output 'PSGallery version:'
184+
Write-Output ($psGalleryVersion | Format-Table | Out-String)
185+
Write-Output $psGalleryVersion.ToString()
186+
Write-Output '-------------------------------------------------'
181187
}
182188

183189
LogGroup 'Get latest version - Manifest' {
184190
Add-PSModulePath -Path (Split-Path -Path $ModulePath -Parent)
185191
$manifestFilePath = Join-Path $ModulePath "$Name.psd1"
186-
Write-Verbose "Module manifest file path: [$manifestFilePath]"
192+
Write-Output "Module manifest file path: [$manifestFilePath]"
187193
if (-not (Test-Path -Path $manifestFilePath)) {
188194
Write-Error "Module manifest file not found at [$manifestFilePath]"
189195
return
@@ -196,23 +202,23 @@ function Publish-PSModule {
196202
$manifestVersion = New-PSSemVer -Version '0.0.0'
197203
}
198204
}
199-
Write-Verbose '-------------------------------------------------'
200-
Write-Verbose 'Manifest version:'
201-
Write-Verbose ($manifestVersion | Format-Table | Out-String)
202-
Write-Verbose $manifestVersion.ToString()
203-
Write-Verbose '-------------------------------------------------'
205+
Write-Output '-------------------------------------------------'
206+
Write-Output 'Manifest version:'
207+
Write-Output ($manifestVersion | Format-Table | Out-String)
208+
Write-Output $manifestVersion.ToString()
209+
Write-Output '-------------------------------------------------'
204210
}
205211

206212
LogGroup 'Get latest version' {
207-
Write-Verbose "GitHub: [$($ghReleaseVersion.ToString())]"
208-
Write-Verbose "PSGallery: [$($psGalleryVersion.ToString())]"
209-
Write-Verbose "Manifest: [$($manifestVersion.ToString())] (ignored)"
213+
Write-Output "GitHub: [$($ghReleaseVersion.ToString())]"
214+
Write-Output "PSGallery: [$($psGalleryVersion.ToString())]"
215+
Write-Output "Manifest: [$($manifestVersion.ToString())] (ignored)"
210216
$latestVersion = New-PSSemVer -Version ($psGalleryVersion, $ghReleaseVersion | Sort-Object -Descending | Select-Object -First 1)
211-
Write-Verbose '-------------------------------------------------'
212-
Write-Verbose 'Latest version:'
213-
Write-Verbose ($latestVersion | Format-Table | Out-String)
214-
Write-Verbose $latestVersion.ToString()
215-
Write-Verbose '-------------------------------------------------'
217+
Write-Output '-------------------------------------------------'
218+
Write-Output 'Latest version:'
219+
Write-Output ($latestVersion | Format-Table | Out-String)
220+
Write-Output $latestVersion.ToString()
221+
Write-Output '-------------------------------------------------'
216222
}
217223

218224
LogGroup 'Calculate new version' {
@@ -237,7 +243,7 @@ function Publish-PSModule {
237243

238244
if ($createPrerelease) {
239245
Write-Output "Adding a prerelease tag to the version using the branch name [$prereleaseName]."
240-
Write-Verbose ($releases | Where-Object { $_.tagName -like "*$prereleaseName*" } |
246+
Write-Output ($releases | Where-Object { $_.tagName -like "*$prereleaseName*" } |
241247
Select-Object -Property name, isPrerelease, isLatest, publishedAt | Format-Table -AutoSize | Out-String)
242248

243249
$newVersion.Prerelease = $prereleaseName
@@ -279,20 +285,20 @@ function Publish-PSModule {
279285
$newVersion.Prerelease += $latestPrereleaseNumber
280286
}
281287
}
282-
Write-Verbose '-------------------------------------------------'
283-
Write-Verbose 'New version:'
284-
Write-Verbose ($newVersion | Format-Table | Out-String)
285-
Write-Verbose $newVersion.ToString()
286-
Write-Verbose '-------------------------------------------------'
288+
Write-Output '-------------------------------------------------'
289+
Write-Output 'New version:'
290+
Write-Output ($newVersion | Format-Table | Out-String)
291+
Write-Output $newVersion.ToString()
292+
Write-Output '-------------------------------------------------'
287293
}
288-
Write-Verbose "New version is [$($newVersion.ToString())]"
294+
Write-Output "New version is [$($newVersion.ToString())]"
289295

290296
LogGroup 'Update module manifest' {
291-
Write-Verbose 'Bump module version -> module metadata: Update-ModuleMetadata'
297+
Write-Output 'Bump module version -> module metadata: Update-ModuleMetadata'
292298
$manifestNewVersion = "$($newVersion.Major).$($newVersion.Minor).$($newVersion.Patch)"
293299
Set-ModuleManifest -Path $manifestFilePath -ModuleVersion $manifestNewVersion -Verbose:$false
294300
if ($createPrerelease) {
295-
Write-Verbose "Prerelease is: [$($newVersion.Prerelease)]"
301+
Write-Output "Prerelease is: [$($newVersion.Prerelease)]"
296302
Set-ModuleManifest -Path $manifestFilePath -Prerelease $($newVersion.Prerelease) -Verbose:$false
297303
}
298304

@@ -305,9 +311,16 @@ function Publish-PSModule {
305311

306312
if ($createPrerelease -or $createRelease -or $whatIf) {
307313
LogGroup 'Publish-ToPSGallery' {
308-
Write-Verbose "Publish module to PowerShell Gallery using [$APIKey]"
314+
if ($createPrerelease) {
315+
$publishPSVersion = "$($newVersion.Major).$($newVersion.Minor).$($newVersion.Patch)-$($newVersion.Prerelease)"
316+
$psGalleryReleaseLink = "https://www.powershellgallery.com/packages/$Name/$publishPSVersion"
317+
} else {
318+
$publishPSVersion = $newVersion.ToString()
319+
$psGalleryReleaseLink = "https://www.powershellgallery.com/packages/$Name/$($newVersion.ToString())"
320+
}
321+
Write-Output "Publish module to PowerShell Gallery using [$APIKey]"
309322
if ($whatIf) {
310-
Write-Verbose "Publish-PSResource -Path $ModulePath -Repository PSGallery -ApiKey $APIKey -Verbose"
323+
Write-Output "Publish-PSResource -Path $ModulePath -Repository PSGallery -ApiKey $APIKey"
311324
} else {
312325
try {
313326
Publish-PSResource -Path $ModulePath -Repository PSGallery -ApiKey $APIKey
@@ -317,10 +330,10 @@ function Publish-PSModule {
317330
}
318331
}
319332
if ($whatIf) {
320-
Write-Output "gh pr comment $($pull_request.number) -b 'Published to the PowerShell Gallery [$newVersion]($releaseURL) has been created.'"
333+
Write-Output "gh pr comment $($pull_request.number) -b 'Published to the PowerShell Gallery [$publishPSVersion]($psGalleryReleaseLink) has been created.'"
321334
} else {
322-
Write-Output "::notice::Module [$Name - $newVersion] published to the PowerShell Gallery."
323-
gh pr comment $pull_request.number -b "Module [$Name - $newVersion] published to the PowerShell Gallery."
335+
Write-GithubNotice "Module [$Name - $publishPSVersion] published to the PowerShell Gallery."
336+
gh pr comment $pull_request.number -b "Module [$Name - $publishPSVersion]($psGalleryReleaseLink) published to the PowerShell Gallery."
324337
if ($LASTEXITCODE -ne 0) {
325338
Write-Error 'Failed to comment on the pull request.'
326339
exit $LASTEXITCODE
@@ -329,7 +342,7 @@ function Publish-PSModule {
329342
}
330343

331344
LogGroup 'New-GitHubRelease' {
332-
Write-Verbose 'Create new GitHub release'
345+
Write-Output 'Create new GitHub release'
333346
if ($createPrerelease) {
334347
if ($whatIf) {
335348
Write-Output "WhatIf: gh release create $newVersion --title $newVersion --target $prHeadRef --generate-notes --prerelease"
@@ -360,7 +373,7 @@ function Publish-PSModule {
360373
exit $LASTEXITCODE
361374
}
362375
}
363-
Write-Output "::notice::Release created: [$newVersion]"
376+
Write-GithubNotice "Release created: [$newVersion]"
364377
}
365378
}
366379

0 commit comments

Comments
 (0)