Skip to content

Commit 15b36d8

Browse files
🩹 [Patch]: Add debug-logging (#22)
## Description This pull request includes several enhancements to the PowerShell scripts to improve logging, error handling, and overall structure. The most important changes are the addition of `begin`, `process`, and `end` blocks, improved error handling, and enhanced logging for better debugging. ### Enhancements to logging and structure: * [`scripts/main.ps1`](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011R4-R9): Added `begin`, `process`, and `end` blocks to provide a clearer structure and added debug messages at the start and end of the script. [[1]](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011R4-R9) [[2]](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011L75-R108) * [`scripts/outputs.ps1`](diffhunk://#diff-ee715ca93229232e95883bf00629fd14e3bf174cdc17b723c4cc5d70e6a60a58R4-L17): Added `begin`, `process`, and `end` blocks to provide a clearer structure and added debug messages at the start and end of the script. [[1]](diffhunk://#diff-ee715ca93229232e95883bf00629fd14e3bf174cdc17b723c4cc5d70e6a60a58R4-L17) [[2]](diffhunk://#diff-ee715ca93229232e95883bf00629fd14e3bf174cdc17b723c4cc5d70e6a60a58L30-R43) ### Improvements to error handling: * [`scripts/main.ps1`](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011L37-R46): Enhanced error handling by adding `-ErrorAction Stop` to the `Install-PSResource` command and logging warnings for exceptions. * [`scripts/main.ps1`](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011L75-R108): Wrapped the main process block in a `try` block to catch and rethrow exceptions, ensuring any errors are properly handled and logged. * [`scripts/outputs.ps1`](diffhunk://#diff-ee715ca93229232e95883bf00629fd14e3bf174cdc17b723c4cc5d70e6a60a58L30-R43): Wrapped the main process block in a `try` block to catch and rethrow exceptions, ensuring any errors are properly handled and logged. ## 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 86c35ee commit 15b36d8

File tree

2 files changed

+128
-101
lines changed

2 files changed

+128
-101
lines changed

scripts/main.ps1

Lines changed: 93 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,108 @@
11
[CmdletBinding()]
22
param()
33

4-
$env:PSMODULE_GITHUB_SCRIPT = $true
5-
Write-Host '┏━━━━━┫ GitHub-Script ┣━━━━━┓'
6-
Write-Host '::group:: - Setup GitHub PowerShell'
4+
begin {
5+
Write-Debug '[main] - Start'
6+
}
77

8-
$Name = 'GitHub'
9-
$Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version
10-
$Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'
8+
process {
9+
try {
10+
$env:PSMODULE_GITHUB_SCRIPT = $true
11+
Write-Host '┏━━━━━┫ GitHub-Script ┣━━━━━┓'
12+
Write-Host '::group:: - Setup GitHub PowerShell'
1113

12-
$alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue
13-
if ($Version) {
14-
Write-Verbose "Filtering by version: $Version"
15-
$alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version
16-
}
17-
if ($Prerelease) {
18-
Write-Verbose 'Filtering by prerelease'
19-
$alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease
20-
}
21-
Write-Verbose 'Already installed:'
22-
$alreadyInstalled | Format-Table
23-
if (-not $alreadyInstalled) {
24-
$params = @{
25-
Name = $Name
26-
Repository = 'PSGallery'
27-
TrustRepository = $true
28-
Prerelease = $Prerelease
29-
}
30-
if ($Version) {
31-
$params['Version'] = $Version
32-
}
33-
$Count = 5
34-
$Delay = 10
35-
for ($i = 1; $i -le $Count; $i++) {
36-
try {
37-
Install-PSResource @params
38-
break
39-
} catch {
40-
if ($i -eq $Count) {
41-
throw $_
14+
$Name = 'GitHub'
15+
$Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version
16+
$Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'
17+
18+
$alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue
19+
if ($Version) {
20+
Write-Verbose "Filtering by version: $Version"
21+
$alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version
22+
}
23+
if ($Prerelease) {
24+
Write-Verbose 'Filtering by prerelease'
25+
$alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease
26+
}
27+
Write-Verbose 'Already installed:'
28+
$alreadyInstalled | Format-Table
29+
if (-not $alreadyInstalled) {
30+
$params = @{
31+
Name = $Name
32+
Repository = 'PSGallery'
33+
TrustRepository = $true
34+
Prerelease = $Prerelease
35+
}
36+
if ($Version) {
37+
$params['Version'] = $Version
38+
}
39+
$Count = 5
40+
$Delay = 10
41+
for ($i = 1; $i -le $Count; $i++) {
42+
try {
43+
Install-PSResource @params -ErrorAction Stop
44+
break
45+
} catch {
46+
Write-Warning $_.Exception.Message
47+
if ($i -eq $Count) {
48+
throw $_
49+
}
50+
Start-Sleep -Seconds $Delay
51+
}
4252
}
43-
Start-Sleep -Seconds $Delay
4453
}
45-
}
46-
}
4754

48-
$alreadyImported = Get-Module -Name $Name
49-
Write-Verbose 'Already imported:'
50-
$alreadyImported | Format-Table
51-
if (-not $alreadyImported) {
52-
Write-Verbose "Importing module: $Name"
53-
Import-Module -Name $Name
54-
}
55+
$alreadyImported = Get-Module -Name $Name
56+
Write-Verbose 'Already imported:'
57+
$alreadyImported | Format-Table
58+
if (-not $alreadyImported) {
59+
Write-Verbose "Importing module: $Name"
60+
Import-Module -Name $Name
61+
}
5562

56-
$providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token)
57-
$providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID)
58-
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey)
59-
[pscustomobject]@{
60-
Name = $Name
61-
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
62-
Prerelease = $Prerelease
63-
'Already installed' = $null -ne $alreadyInstalled
64-
'Already imported' = $null -ne $alreadyImported
65-
'Provided Token' = $providedToken
66-
'Provided ClientID' = $providedClientID
67-
'Provided PrivateKey' = $providedPrivateKey
68-
} | Format-List
69-
Write-Host '::endgroup::'
63+
$providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token)
64+
$providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID)
65+
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey)
66+
[pscustomobject]@{
67+
Name = $Name
68+
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
69+
Prerelease = $Prerelease
70+
'Already installed' = $null -ne $alreadyInstalled
71+
'Already imported' = $null -ne $alreadyImported
72+
'Provided Token' = $providedToken
73+
'Provided ClientID' = $providedClientID
74+
'Provided PrivateKey' = $providedPrivateKey
75+
} | Format-List
76+
Write-Host '::endgroup::'
7077

71-
LogGroup ' - Installed modules' {
72-
Get-InstalledPSResource | Select-Object Name, Version, Prerelease | Sort-Object -Property Name | Format-Table -AutoSize
73-
}
78+
LogGroup ' - Installed modules' {
79+
Get-InstalledPSResource | Select-Object Name, Version, Prerelease | Sort-Object -Property Name | Format-Table -AutoSize
80+
}
81+
82+
if ($providedClientID -and $providedPrivateKey) {
83+
LogGroup ' - GitHub connection - GitHub App' {
84+
Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent -PassThru |
85+
Format-List
86+
}
87+
} elseif ($providedToken) {
88+
LogGroup ' - GitHub connection - Token' {
89+
Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent -PassThru |
90+
Format-List
91+
}
92+
}
93+
94+
LogGroup ' - Configuration' {
95+
Get-GitHubConfig | Format-List
96+
}
7497

75-
LogGroup ' - GitHub connection' {
76-
if ($providedClientID -and $providedPrivateKey) {
77-
Write-Verbose 'Connected using provided GitHub App'
78-
Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent
79-
} elseif ($providedToken) {
80-
Write-Verbose 'Connected using provided token'
81-
Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent
98+
Write-Host '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
99+
} catch {
100+
throw $_
82101
}
83-
Get-GitHubContext | Format-List
84102
}
85103

86-
LogGroup ' - Configuration' {
87-
Get-GitHubConfig | Format-List
104+
end {
105+
Write-Debug '[main] - End'
106+
$DebugPreference = $env:GITHUB_ACTION_INPUT_Debug -eq 'true' ? 'Continue' : 'SilentlyContinue'
107+
$VerbosePreference = $env:GITHUB_ACTION_INPUT_Verbose -eq 'true' ? 'Continue' : 'SilentlyContinue'
88108
}
89-
90-
Write-Host '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
91-
92-
$DebugPreference = $env:GITHUB_ACTION_INPUT_Debug -eq 'true' ? 'Continue' : 'SilentlyContinue'
93-
$VerbosePreference = $env:GITHUB_ACTION_INPUT_Verbose -eq 'true' ? 'Continue' : 'SilentlyContinue'

scripts/outputs.ps1

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
[CmdletBinding()]
22
param()
33

4-
$DebugPreference = 'SilentlyContinue'
5-
$VerbosePreference = 'SilentlyContinue'
6-
7-
if ($env:GITHUB_ACTION_INPUT_ShowOutput -ne 'true') {
8-
return
9-
}
10-
11-
$result = (Get-GitHubOutput).result
12-
if (-not $result) {
13-
return
4+
begin {
5+
$DebugPreference = 'SilentlyContinue'
6+
$VerbosePreference = 'SilentlyContinue'
7+
Write-Debug '[outputs] - Start'
148
}
159

16-
Write-Host '┏━━━━━┫ GitHub-Script ┣━━━━━┓'
17-
18-
LogGroup ' - Outputs' {
19-
if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) {
20-
Write-GitHubWarning 'Outputs cannot be accessed as the step has no ID.'
10+
process {
11+
try {
12+
Write-Debug "[outputs] - ShowOutput: $env:GITHUB_ACTION_INPUT_ShowOutput"
13+
if ($env:GITHUB_ACTION_INPUT_ShowOutput -ne 'true') {
14+
return
15+
}
16+
17+
$result = (Get-GitHubOutput).result
18+
Write-Debug "[outputs] - Result: $(-not $result)"
19+
if (-not $result) {
20+
return
21+
}
22+
Write-Host '┏━━━━━┫ GitHub-Script ┣━━━━━┓'
23+
LogGroup ' - Outputs' {
24+
if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) {
25+
Write-GitHubWarning 'Outputs cannot be accessed as the step has no ID.'
26+
}
27+
28+
if (-not (Test-Path -Path $env:GITHUB_OUTPUT)) {
29+
Write-Warning "File not found: $env:GITHUB_OUTPUT"
30+
}
31+
32+
$result | Format-List
33+
Write-Host "Access outputs using `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result).<output-name> }}"
34+
}
35+
Write-Host '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
36+
} catch {
37+
throw $_
2138
}
22-
23-
if (-not (Test-Path -Path $env:GITHUB_OUTPUT)) {
24-
Write-Warning "File not found: $env:GITHUB_OUTPUT"
25-
}
26-
27-
$result | Format-List
28-
Write-Host "Access outputs using `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result).<output-name> }}"
2939
}
3040

31-
Write-Host '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
41+
end {
42+
Write-Debug '[outputs] - End'
43+
}

0 commit comments

Comments
 (0)