Skip to content

Commit

Permalink
🩹 [Patch]: Adding GitHub action loader and output + var commands (#119)
Browse files Browse the repository at this point in the history
## Description

- Add detection for module running on GitHub Runners, if so run an
initializer.
  - run loader if `$env:GITHUB_ACTIONS == true`.
- Adding commands for creating a step output and setting a GitHub env
var, mean to be used while on a GitHub runner.
  - `Set-GitHubEnvironmentVariable`
  - `Set-GitHubOutput`

## 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
  • Loading branch information
MariusStorhaug authored Aug 15, 2024
1 parent 1556473 commit a62bac0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/Linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ jobs:
env:
GITHUB_TOKEN: ${{ github.token }}
VALIDATE_JSCPD: false
VALIDATE_MARKDOWN_PRETTIER: false
VALIDATE_YAML_PRETTIER: false
4 changes: 4 additions & 0 deletions src/loader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Write-Verbose "[$scriptFilePath] - Initializing GitHub PowerShell module..."

Initialize-Store -Name 'GitHubPowerShell' -SecretVaultName $script:Config.Name -SecretVaultType $script:Config.Type

if ($env:GITHUB_ACTIONS -eq 'true') {
Initialize-RunnerEnvironment
}

# Autologon if a token is present in environment variables
$envVar = Get-ChildItem -Path 'Env:' | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1
$envVarPresent = $envVar.count -gt 0
Expand Down
21 changes: 21 additions & 0 deletions src/private/Commands/Initialize-RunnerEnvironment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function Initialize-RunnerEnvironment {
<#
.SYNOPSIS
Initialize the runner environment for the GitHub module
.DESCRIPTION
Initialize the runner environment for the GitHub module
.EXAMPLE
Initialize-RunnerEnvironment
Initializes the runner environment for the GitHub module
#>
[CmdletBinding()]
param ()

Write-Warning 'Detected running on a GitHub Actions runner, preparing environment...'
$env:GITHUB_REPOSITORY_NAME = $env:GITHUB_REPOSITORY -replace '.+/'
Set-GitHubEnv -Name 'GITHUB_REPOSITORY_NAME' -Value $env:GITHUB_REPOSITORY_NAME

}
31 changes: 31 additions & 0 deletions src/public/Commands/Set-GitHubEnvironmentVariable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function Set-GitHubEnvironmentVariable {
<#
.SYNOPSIS
Set a GitHub environment variable
.DESCRIPTION
Set a GitHub environment variable
.EXAMPLE
Set-GitHubEnv -Name 'MyVariable' -Value 'MyValue'
#>
[OutputType([void])]
[Alias('Set-GitHubEnv')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
Justification = 'Does not change system state significantly'
)]
[CmdletBinding()]
param (
# Name of the variable
[Parameter(Mandatory)]
[string] $Name,

# Value of the variable
[Parameter(Mandatory)]
[AllowNull()]
[string] $Value
)
Write-Verbose (@{ $Name = $Value } | Format-Table -Wrap -AutoSize | Out-String)
"$Name=$Value" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
}
42 changes: 42 additions & 0 deletions src/public/Commands/Set-GitHubOutput.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function Set-GitHubOutput {
<#
.SYNOPSIS
Set a output variable in GitHub Actions
.DESCRIPTION
Set a output variable in GitHub Actions. If the variable is a SecureString, it will be converted to plain text and masked.
.EXAMPLE
Set-GitHubOutput -Name 'MyOutput' -Value 'Hello, World!'
Creates a new output variable named 'MyOutput' with the value 'Hello, World!'.
#>
[OutputType([void])]
[Alias('Output')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function',
Justification = 'Does not change system state significantly'
)]
[CmdletBinding()]
param (
# Name of the variable
[Parameter(Mandatory)]
[string] $Name,

# Value of the variable
[Parameter(Mandatory)]
[AllowNull()]
[object] $Value
)
if ($Value -Is [securestring]) {
$Value = $Value | ConvertFrom-SecureString -AsPlainText -Force
Add-Mask -Value $Value
}
Write-Verbose (@{ $Name = $Value } | Format-Table -Wrap -AutoSize | Out-String)
"$Name=$Value" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) {
Write-Warning "Cannot create output as the step has no ID."
} else {
Write-Verbose "Output: [$Name] avaiable as `${{ steps.$env:GITHUB_ACTION.outputs.$Name }}'"
}
}

0 comments on commit a62bac0

Please sign in to comment.