diff --git a/.github/workflows/TestWorkflow.yml b/.github/workflows/TestWorkflow.yml index 973e194..7e6728b 100644 --- a/.github/workflows/TestWorkflow.yml +++ b/.github/workflows/TestWorkflow.yml @@ -47,6 +47,13 @@ jobs: Debug: true Verbose: true + - name: Action-Test [ShowInit] + uses: ./ + with: + Debug: true + Verbose: true + ShowInit: true + ActionTestWithScript: name: WithScript runs-on: ${{ inputs.runs-on }} @@ -121,6 +128,7 @@ jobs: Debug: true Verbose: true Prerelease: true + ShowInit: true ShowOutput: true Script: | LogGroup 'Get-GitHubZen' { diff --git a/README.md b/README.md index 591deb1..5c19377 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ For more information on available functions and automatically loaded variables, | `Verbose` | Enable verbose output. | false | `'false'` | | `Version` | Specifies the exact version of the GitHub module to install. | false | | | `Prerelease` | Allow prerelease versions if available. | false | `'false'` | +| `ShowInfo` | Show information about the environment. | false | `'true'` | +| `ShowInit` | Show information about the initialization. | false | `'false'` | | `ShowOutput` | Show the script's output. | false | `'false'` | | `WorkingDirectory` | The working directory where the script runs. | false | `${{ github.workspace }}` | diff --git a/action.yml b/action.yml index 5935e46..c7edc13 100644 --- a/action.yml +++ b/action.yml @@ -34,6 +34,14 @@ inputs: description: Allow prerelease versions if available. required: false default: 'false' + ShowInfo: + description: Show information about the environment. + required: false + default: 'true' + ShowInit: + description: Show information about the initialization. + required: false + default: 'false' ShowOutput: description: Show the output of the script. required: false @@ -62,10 +70,13 @@ runs: GITHUB_ACTION_INPUT_Debug: ${{ inputs.Debug }} GITHUB_ACTION_INPUT_Verbose: ${{ inputs.Verbose }} GITHUB_ACTION_INPUT_Version: ${{ inputs.Version }} + GITHUB_ACTION_INPUT_ShowInit: ${{ inputs.ShowInit }} + GITHUB_ACTION_INPUT_ShowInfo: ${{ inputs.ShowInfo }} GITHUB_ACTION_INPUT_ShowOutput: ${{ inputs.ShowOutput }} GITHUB_ACTION_INPUT_Prerelease: ${{ inputs.Prerelease }} run: | # GitHub-Script - . $(Join-Path -Path '${{ github.action_path }}' -ChildPath 'scripts\main.ps1') + . ${{ github.action_path }}\scripts\init.ps1 + . ${{ github.action_path }}\scripts\info.ps1 ${{ inputs.Script }} - . $(Join-Path -Path '${{ github.action_path }}' -ChildPath 'scripts\outputs.ps1') + . ${{ github.action_path }}\scripts\outputs.ps1 diff --git a/scripts/info.ps1 b/scripts/info.ps1 new file mode 100644 index 0000000..8efdbf5 --- /dev/null +++ b/scripts/info.ps1 @@ -0,0 +1,50 @@ +#Requires -Modules GitHub + +[CmdletBinding()] +param() + +begin { + $scriptName = $MyInvocation.MyCommand.Name + Write-Debug "[$scriptName] - Start" +} + +process { + try { + $fenceTitle = 'GitHub-Script' + + Write-Debug "[$scriptName] - ShowInfo: $env:GITHUB_ACTION_INPUT_ShowInfo" + if ($env:GITHUB_ACTION_INPUT_ShowInfo -ne 'true') { + return + } + + $fenceStart = "┏━━┫ $fenceTitle - Info ┣━━━━━━━━┓" + Write-Output $fenceStart + + LogGroup ' - Installed modules' { + Get-InstalledPSResource | Select-Object Name, Version, Prerelease | Sort-Object -Property Name | Format-Table -AutoSize + } + + LogGroup ' - GitHub connection - Default' { + Get-GitHubContext | Format-List + } + + LogGroup ' - GitHub connection - List' { + Get-GitHubContext -ListAvailable | Format-Table + } + + LogGroup ' - Configuration' { + Get-GitHubConfig | Format-List + } + + $fenceEnd = '┗' + ('━' * ($fenceStart.Length - 2)) + '┛' + Write-Output $fenceEnd + } catch { + throw $_ + } +} + +end { + Write-Debug "[$scriptName] - End" + $DebugPreference = $env:GITHUB_ACTION_INPUT_Debug -eq 'true' ? 'Continue' : 'SilentlyContinue' + $VerbosePreference = $env:GITHUB_ACTION_INPUT_Verbose -eq 'true' ? 'Continue' : 'SilentlyContinue' +} diff --git a/scripts/main.ps1 b/scripts/init.ps1 similarity index 60% rename from scripts/main.ps1 rename to scripts/init.ps1 index 5bce82b..d8993a5 100644 --- a/scripts/main.ps1 +++ b/scripts/init.ps1 @@ -1,108 +1,112 @@ -[CmdletBinding()] -param() - -begin { - Write-Debug '[main] - Start' -} - -process { - try { - $env:PSMODULE_GITHUB_SCRIPT = $true - Write-Output '┏━━━━━┫ GitHub-Script ┣━━━━━┓' - Write-Output '::group:: - Setup GitHub PowerShell' - - $Name = 'GitHub' - $Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version - $Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true' - - $alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue - if ($Version) { - Write-Verbose "Filtering by version: $Version" - $alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version - } - if ($Prerelease) { - Write-Verbose 'Filtering by prerelease' - $alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease - } - Write-Verbose 'Already installed:' - $alreadyInstalled | Format-Table - if (-not $alreadyInstalled) { - $params = @{ - Name = $Name - Repository = 'PSGallery' - TrustRepository = $true - Prerelease = $Prerelease - } - if ($Version) { - $params['Version'] = $Version - } - $Count = 5 - $Delay = 10 - for ($i = 1; $i -le $Count; $i++) { - try { - Install-PSResource @params -ErrorAction Stop - break - } catch { - Write-Warning $_.Exception.Message - if ($i -eq $Count) { - throw $_ - } - Start-Sleep -Seconds $Delay - } - } - } - - $alreadyImported = Get-Module -Name $Name - Write-Verbose 'Already imported:' - $alreadyImported | Format-Table - if (-not $alreadyImported) { - Write-Verbose "Importing module: $Name" - Import-Module -Name $Name - } - - $providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token) - $providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID) - $providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey) - [pscustomobject]@{ - Name = $Name - Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version - Prerelease = $Prerelease - 'Already installed' = $null -ne $alreadyInstalled - 'Already imported' = $null -ne $alreadyImported - 'Provided Token' = $providedToken - 'Provided ClientID' = $providedClientID - 'Provided PrivateKey' = $providedPrivateKey - } | Format-List - Write-Output '::endgroup::' - - LogGroup ' - Installed modules' { - Get-InstalledPSResource | Select-Object Name, Version, Prerelease | Sort-Object -Property Name | Format-Table -AutoSize - } - - LogGroup ' - GitHub connection' { - if ($providedClientID -and $providedPrivateKey) { - Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent -PassThru | - Select-Object * | Format-List - } elseif ($providedToken) { - Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent -PassThru | - Select-Object * | Format-List - } else { - Write-Output 'No connection provided' - } - } - - LogGroup ' - Configuration' { - Get-GitHubConfig | Format-List - } - - Write-Output '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛' - } catch { - throw $_ - } -} - -end { - Write-Debug '[main] - End' - $DebugPreference = $env:GITHUB_ACTION_INPUT_Debug -eq 'true' ? 'Continue' : 'SilentlyContinue' - $VerbosePreference = $env:GITHUB_ACTION_INPUT_Verbose -eq 'true' ? 'Continue' : 'SilentlyContinue' -} +[CmdletBinding()] +param() + +begin { + $scriptName = $MyInvocation.MyCommand.Name + Write-Debug "[$scriptName] - Start" +} + +process { + try { + $env:PSMODULE_GITHUB_SCRIPT = $true + $fenceTitle = 'GitHub-Script' + $showInit = $env:GITHUB_ACTION_INPUT_ShowInit -eq 'true' + + Write-Debug "[$scriptName] - ShowInit: $env:GITHUB_ACTION_INPUT_ShowInit" + + if ($showInit) { + $fenceStart = "┏━━┫ $fenceTitle - Init ┣━━━━━━━━┓" + Write-Output $fenceStart + Write-Output '::group:: - Install GitHub PowerShell module' + } + $Name = 'GitHub' + $Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version + $Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true' + + $alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue + if ($Version) { + Write-Verbose "Filtering by version: $Version" + $alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version + } + if ($Prerelease) { + Write-Verbose 'Filtering by prerelease' + $alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease + } + + if ($showInit) { + Write-Output 'Already installed:' + $alreadyInstalled | Format-List + } + if (-not $alreadyInstalled) { + $params = @{ + Name = $Name + Repository = 'PSGallery' + TrustRepository = $true + Prerelease = $Prerelease + } + if ($Version) { + $params['Version'] = $Version + } + $Count = 5 + $Delay = 10 + for ($i = 1; $i -le $Count; $i++) { + try { + Install-PSResource @params -ErrorAction Stop + break + } catch { + Write-Warning $_.Exception.Message + if ($i -eq $Count) { + throw $_ + } + Start-Sleep -Seconds $Delay + } + } + } + + $alreadyImported = Get-Module -Name $Name + if ($showInit) { + Write-Output 'Already imported:' + $alreadyImported | Format-List + } + if (-not $alreadyImported) { + Write-Verbose "Importing module: $Name" + Import-Module -Name $Name + } + + $providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token) + $providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID) + $providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey) + $moduleStatus = [pscustomobject]@{ + Name = $Name + Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version + Prerelease = $Prerelease + 'Already installed' = $null -ne $alreadyInstalled + 'Already imported' = $null -ne $alreadyImported + 'Provided Token' = $providedToken + 'Provided ClientID' = $providedClientID + 'Provided PrivateKey' = $providedPrivateKey + } + if ($showInit) { + Write-Output 'Module status:' + $moduleStatus | Format-List + Write-Output '::endgroup::' + Write-Output '::group:: - Connect to GitHub' + } + if ($providedClientID -and $providedPrivateKey) { + Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent:(-not $showInit) + } elseif ($providedToken) { + Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent:(-not $showInit) + } + if ($showInit) { + Write-Output '::endgroup::' + $fenceEnd = '┗' + ('━' * ($fenceStart.Length - 2)) + '┛' + Write-Output $fenceEnd + } + } catch { + throw $_ + } +} + +end { + Write-Debug "[$scriptName] - End" +} diff --git a/scripts/outputs.ps1 b/scripts/outputs.ps1 index f847c4b..280e847 100644 --- a/scripts/outputs.ps1 +++ b/scripts/outputs.ps1 @@ -1,43 +1,44 @@ -[CmdletBinding()] +#Requires -Modules GitHub + +[CmdletBinding()] param() -begin { - $DebugPreference = 'SilentlyContinue' - $VerbosePreference = 'SilentlyContinue' - Write-Debug '[outputs] - Start' -} +$DebugPreference = 'SilentlyContinue' +$VerbosePreference = 'SilentlyContinue' +$scriptName = $MyInvocation.MyCommand.Name +Write-Debug "[$scriptName] - Start" -process { - try { - Write-Debug "[outputs] - ShowOutput: $env:GITHUB_ACTION_INPUT_ShowOutput" - if ($env:GITHUB_ACTION_INPUT_ShowOutput -ne 'true') { - return - } +try { + $fenceTitle = 'GitHub-Script' + + Write-Debug "[$scriptName] - ShowOutput: $env:GITHUB_ACTION_INPUT_ShowOutput" + if ($env:GITHUB_ACTION_INPUT_ShowOutput -ne 'true') { + return + } - $result = (Get-GitHubOutput).result - Write-Debug "[outputs] - Result: $(-not $result)" - if (-not $result) { - return + $result = (Get-GitHubOutput).result + Write-Debug "[$scriptName] - Result: $(-not $result)" + if (-not $result) { + return + } + $fenceStart = "┏━━┫ $fenceTitle - Outputs ┣━━━━━┓" + Write-Output $fenceStart + LogGroup ' - Outputs' { + if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) { + Write-GitHubWarning 'Outputs cannot be accessed as the step has no ID.' } - Write-Host '┏━━━━━┫ GitHub-Script ┣━━━━━┓' - LogGroup ' - Outputs' { - if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) { - Write-GitHubWarning 'Outputs cannot be accessed as the step has no ID.' - } - - if (-not (Test-Path -Path $env:GITHUB_OUTPUT)) { - Write-Warning "File not found: $env:GITHUB_OUTPUT" - } - - $result | Format-List - Write-Host "Access outputs using `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result). }}" + + if (-not (Test-Path -Path $env:GITHUB_OUTPUT)) { + Write-Warning "File not found: $env:GITHUB_OUTPUT" } - Write-Host '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛' - } catch { - throw $_ + + $result | Format-List + Write-Host "Access outputs using `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result). }}" } + $fenceEnd = '┗' + ('━' * ($fenceStart.Length - 2)) + '┛' + Write-Output $fenceEnd +} catch { + throw $_ } -end { - Write-Debug '[outputs] - End' -} +Write-Debug "$scriptName - End"