Skip to content

Commit 67ea93f

Browse files
🩹 [Patch]: Add ShowInfo input to display environment information and refactor script execution flow
1 parent 8a986f3 commit 67ea93f

File tree

5 files changed

+146
-129
lines changed

5 files changed

+146
-129
lines changed

action.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ inputs:
3434
description: Allow prerelease versions if available.
3535
required: false
3636
default: 'false'
37+
ShowInfo:
38+
description: Show information about the environment.
39+
required: false
40+
default: 'true'
3741
ShowOutput:
3842
description: Show the output of the script.
3943
required: false
40-
default: 'false'
44+
default: 'true'
4145
WorkingDirectory:
4246
description: The working directory where the script will run from.
4347
required: false
@@ -62,11 +66,13 @@ runs:
6266
GITHUB_ACTION_INPUT_Debug: ${{ inputs.Debug }}
6367
GITHUB_ACTION_INPUT_Verbose: ${{ inputs.Verbose }}
6468
GITHUB_ACTION_INPUT_Version: ${{ inputs.Version }}
69+
GITHUB_ACTION_INPUT_ShowInfo: ${{ inputs.ShowInfo }}
6570
GITHUB_ACTION_INPUT_ShowOutput: ${{ inputs.ShowOutput }}
6671
GITHUB_ACTION_INPUT_Prerelease: ${{ inputs.Prerelease }}
72+
6773
run: |
6874
# GitHub-Script
6975
. ${{ github.action_path }}\scripts\init.ps1
70-
. ${{ github.action_path }}\scripts\boilerplate.ps1
76+
. ${{ github.action_path }}\scripts\info.ps1
7177
${{ inputs.Script }}
72-
. ${{ github.action_path }}\scripts\outputs.ps1
78+
. ${{ github.action_path }}\scripts\result.ps1

scripts/boilerplate.ps1

Lines changed: 0 additions & 39 deletions
This file was deleted.

scripts/info.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
begin {
5+
$scriptName = $MyInvocation.MyCommand.Name
6+
Write-Debug "[$scriptName] - Start"
7+
}
8+
9+
process {
10+
try {
11+
Write-Debug "[$scriptName] - ShowInfo: $env:GITHUB_ACTION_INPUT_ShowInfo"
12+
if ($env:GITHUB_ACTION_INPUT_ShowInfo -ne 'true') {
13+
return
14+
}
15+
16+
$fenceStart = "┏━━━━━┫ $Name - Info ┣━━━━━┓"
17+
Write-Output $fenceStart
18+
19+
LogGroup ' - Installed modules' {
20+
Get-InstalledPSResource | Select-Object Name, Version, Prerelease | Sort-Object -Property Name | Format-Table -AutoSize
21+
}
22+
23+
LogGroup ' - GitHub connection - Default' {
24+
Get-GitHubContext | Format-List
25+
}
26+
27+
LogGroup ' - GitHub connection - List' {
28+
Get-GitHubContext -ListAvailable | Format-Table
29+
}
30+
31+
LogGroup ' - Configuration' {
32+
Get-GitHubConfig | Format-List
33+
}
34+
35+
$fenceEnd = '' + ('' * ($fenceStart.Length - 2)) + ''
36+
Write-Output $fenceEnd
37+
} catch {
38+
throw $_
39+
}
40+
}
41+
42+
end {
43+
Write-Debug "[$scriptName] - End"
44+
$DebugPreference = $env:GITHUB_ACTION_INPUT_Debug -eq 'true' ? 'Continue' : 'SilentlyContinue'
45+
$VerbosePreference = $env:GITHUB_ACTION_INPUT_Verbose -eq 'true' ? 'Continue' : 'SilentlyContinue'
46+
}

scripts/init.ps1

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,102 @@
11
[CmdletBinding()]
2-
param(
3-
[string]$Name = 'GitHub-Script - Init'
4-
)
2+
param()
53

6-
$scriptName = $MyInvocation.MyCommand.Name
7-
Write-Debug "[$scriptName] - Start"
8-
9-
try {
10-
$env:PSMODULE_GITHUB_SCRIPT = $true
4+
begin {
5+
$scriptName = $MyInvocation.MyCommand.Name
6+
Write-Debug "[$scriptName] - Start"
7+
}
118

12-
if ($VerbosePreference -eq 'Continue') {
13-
$title = "┏━━━━━┫ $Name ┣━━━━━┓"
14-
Write-Output $title
15-
Write-Output '::group:: - SetupGitHub PowerShell module'
16-
}
17-
$Name = 'GitHub'
18-
$Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version
19-
$Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'
9+
process {
10+
try {
11+
$env:PSMODULE_GITHUB_SCRIPT = $true
2012

21-
$alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue
22-
if ($Version) {
23-
Write-Verbose "Filtering by version: $Version"
24-
$alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version
25-
}
26-
if ($Prerelease) {
27-
Write-Verbose 'Filtering by prerelease'
28-
$alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease
29-
}
30-
Write-Verbose 'Already installed:'
31-
Write-Verbose "$($alreadyInstalled | Format-List)"
32-
if (-not $alreadyInstalled) {
33-
$params = @{
34-
Name = $Name
35-
Repository = 'PSGallery'
36-
TrustRepository = $true
37-
Prerelease = $Prerelease
13+
if ($VerbosePreference -eq 'Continue') {
14+
$fenceStart = "┏━━━━━┫ $Name - Init ┣━━━━━┓"
15+
Write-Output $fenceStart
16+
Write-Output '::group:: - SetupGitHub PowerShell module'
3817
}
18+
$Name = 'GitHub'
19+
$Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version
20+
$Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'
21+
22+
$alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue
3923
if ($Version) {
40-
$params['Version'] = $Version
24+
Write-Verbose "Filtering by version: $Version"
25+
$alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version
26+
}
27+
if ($Prerelease) {
28+
Write-Verbose 'Filtering by prerelease'
29+
$alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease
4130
}
42-
$Count = 5
43-
$Delay = 10
44-
for ($i = 1; $i -le $Count; $i++) {
45-
try {
46-
Install-PSResource @params -ErrorAction Stop
47-
break
48-
} catch {
49-
Write-Warning $_.Exception.Message
50-
if ($i -eq $Count) {
51-
throw $_
31+
Write-Verbose 'Already installed:'
32+
Write-Verbose "$($alreadyInstalled | Format-List)"
33+
if (-not $alreadyInstalled) {
34+
$params = @{
35+
Name = $Name
36+
Repository = 'PSGallery'
37+
TrustRepository = $true
38+
Prerelease = $Prerelease
39+
}
40+
if ($Version) {
41+
$params['Version'] = $Version
42+
}
43+
$Count = 5
44+
$Delay = 10
45+
for ($i = 1; $i -le $Count; $i++) {
46+
try {
47+
Install-PSResource @params -ErrorAction Stop
48+
break
49+
} catch {
50+
Write-Warning $_.Exception.Message
51+
if ($i -eq $Count) {
52+
throw $_
53+
}
54+
Start-Sleep -Seconds $Delay
5255
}
53-
Start-Sleep -Seconds $Delay
5456
}
5557
}
56-
}
5758

58-
$alreadyImported = Get-Module -Name $Name
59-
Write-Verbose 'Already imported:'
60-
Write-Verbose "$($alreadyImported | Format-Table)"
61-
if (-not $alreadyImported) {
62-
Write-Verbose "Importing module: $Name"
63-
Import-Module -Name $Name
64-
}
59+
$alreadyImported = Get-Module -Name $Name
60+
Write-Verbose 'Already imported:'
61+
Write-Verbose "$($alreadyImported | Format-Table)"
62+
if (-not $alreadyImported) {
63+
Write-Verbose "Importing module: $Name"
64+
Import-Module -Name $Name
65+
}
6566

66-
$providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token)
67-
$providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID)
68-
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey)
69-
$moduleStatus = [pscustomobject]@{
70-
Name = $Name
71-
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
72-
Prerelease = $Prerelease
73-
'Already installed' = $null -ne $alreadyInstalled
74-
'Already imported' = $null -ne $alreadyImported
75-
'Provided Token' = $providedToken
76-
'Provided ClientID' = $providedClientID
77-
'Provided PrivateKey' = $providedPrivateKey
78-
}
79-
Write-Verbose "$($moduleStatus | Format-List)"
80-
if ($VerbosePreference -eq 'Continue') {
81-
Write-Output '::endgroup::'
82-
Write-Output '::group:: - GitHub connection'
83-
}
84-
if ($providedClientID -and $providedPrivateKey) {
85-
Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent
86-
} elseif ($providedToken) {
87-
Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent
88-
}
89-
if ($VerbosePreference -eq 'Continue') {
90-
Write-Output '::endgroup::'
91-
$endingFence = '' + ('' * ($title.Length - 2)) + ''
92-
Write-Output $endingFence
67+
$providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token)
68+
$providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID)
69+
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey)
70+
$moduleStatus = [pscustomobject]@{
71+
Name = $Name
72+
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
73+
Prerelease = $Prerelease
74+
'Already installed' = $null -ne $alreadyInstalled
75+
'Already imported' = $null -ne $alreadyImported
76+
'Provided Token' = $providedToken
77+
'Provided ClientID' = $providedClientID
78+
'Provided PrivateKey' = $providedPrivateKey
79+
}
80+
Write-Verbose "$($moduleStatus | Format-List)"
81+
if ($VerbosePreference -eq 'Continue') {
82+
Write-Output '::endgroup::'
83+
Write-Output '::group:: - GitHub connection'
84+
}
85+
if ($providedClientID -and $providedPrivateKey) {
86+
Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent
87+
} elseif ($providedToken) {
88+
Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent
89+
}
90+
if ($VerbosePreference -eq 'Continue') {
91+
Write-Output '::endgroup::'
92+
$fenceEnd = '' + ('' * ($fenceStart.Length - 2)) + ''
93+
Write-Output $fenceEnd
94+
}
95+
} catch {
96+
throw $_
9397
}
94-
} catch {
95-
throw $_
9698
}
9799

98-
Write-Debug "[$scriptName] - End"
100+
end {
101+
Write-Debug "[$scriptName] - End"
102+
}

scripts/outputs.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ try {
1717
if (-not $result) {
1818
return
1919
}
20-
$title = "┏━━━━━┫ $Name ┣━━━━━┓"
21-
Write-Output $title
20+
$fenceStart = "┏━━━━━┫ $Name ┣━━━━━┓"
21+
Write-Output $fenceStart
2222
LogGroup ' - Outputs' {
2323
if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) {
2424
Write-GitHubWarning 'Outputs cannot be accessed as the step has no ID.'
@@ -31,8 +31,8 @@ try {
3131
$result | Format-List
3232
Write-Host "Access outputs using `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result).<output-name> }}"
3333
}
34-
$endingFence = '' + ('' * ($title.Length - 2)) + ''
35-
Write-Output $endingFence
34+
$fenceEnd = '' + ('' * ($fenceStart.Length - 2)) + ''
35+
Write-Output $fenceEnd
3636
} catch {
3737
throw $_
3838
}

0 commit comments

Comments
 (0)