From 00e8ebec7647f64742d8fad0d1dd3909c1fd084c Mon Sep 17 00:00:00 2001 From: Marius Storhaug <marstor@hotmail.com> Date: Fri, 4 Apr 2025 10:32:49 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Remove=20local=20?= =?UTF-8?q?environment=20variable=20setting=20from=20`Get-GitHubVariable`?= =?UTF-8?q?=20and=20add=20`Export-GitHubVariable`=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Variables/Export-GitHubVariable.ps1 | 57 +++++++++++++++++++ .../public/Variables/Get-GitHubVariable.ps1 | 11 ---- tests/Variables.Tests.ps1 | 27 +++++++++ 3 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 src/functions/public/Variables/Export-GitHubVariable.ps1 diff --git a/src/functions/public/Variables/Export-GitHubVariable.ps1 b/src/functions/public/Variables/Export-GitHubVariable.ps1 new file mode 100644 index 000000000..bd6e39ee8 --- /dev/null +++ b/src/functions/public/Variables/Export-GitHubVariable.ps1 @@ -0,0 +1,57 @@ +function Export-GitHubVariable { + <# + .SYNOPSIS + Exports a GitHub variable to the local environment. + + .DESCRIPTION + This function takes a GitHub variable and sets it as an environment variable. + The variable can be exported to the Process, User, or Machine environment scope. + + By default, the variable is exported to the Process scope, meaning it will persist only for the current session. + + The function accepts pipeline input, allowing GitHub variables retrieved using `Get-GitHubVariable` to be exported seamlessly. + + .EXAMPLE + Get-GitHubVariable -Owner 'octocat' -Repository 'Hello-World' -Environment 'staging' | Export-GitHubVariable + + Exports the variables retrieved from the GitHub API to the local environment. + + .INPUTS + GitHubVariable. Accepts objects representing GitHub variables via pipeline input. + + .OUTPUTS + void + + .LINK + https://psmodule.io/GitHub/Functions/Variables/Export-GitHubVariable + #> + [OutputType([void])] + [CmdletBinding()] + param( + # The name of the variable. + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [string] $Name, + + # The value of the variable. + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [string] $Value, + + # The target scope for the environment variable. + [Parameter()] + [System.EnvironmentVariableTarget] $Target = [System.EnvironmentVariableTarget]::Process + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + } + + process { + Write-Debug "$($_.Name) = $($_.Value)" + [System.Environment]::SetEnvironmentVariable($Name, $Value, $Target) + } + + end { + Write-Debug "[$stackPath] - End" + } +} diff --git a/src/functions/public/Variables/Get-GitHubVariable.ps1 b/src/functions/public/Variables/Get-GitHubVariable.ps1 index cdd9e5a09..db1b0ea64 100644 --- a/src/functions/public/Variables/Get-GitHubVariable.ps1 +++ b/src/functions/public/Variables/Get-GitHubVariable.ps1 @@ -154,10 +154,6 @@ function Get-GitHubVariable { [Parameter()] [switch] $All, - # Set the environment variables locally for the current session. - [Parameter()] - [switch] $SetLocalEnvironment, - # The context to run the command in. Used to get the details for the API call. # Can be either a string or a GitHubContext object. [Parameter()] @@ -249,13 +245,6 @@ function Get-GitHubVariable { } } - if ($SetLocalEnvironment) { - Write-Debug 'Set local environment variables:' - $variables | ForEach-Object { - [System.Environment]::SetEnvironmentVariable($_.Name, $_.Value, 'Process') - Write-Debug "$($_.Name) = $($_.Value)" - } - } $variables } diff --git a/tests/Variables.Tests.ps1 b/tests/Variables.Tests.ps1 index 82edd7347..fffc93839 100644 --- a/tests/Variables.Tests.ps1 +++ b/tests/Variables.Tests.ps1 @@ -184,6 +184,15 @@ Describe 'Environments' { $result | Should -Not -BeNullOrEmpty } + It 'Export-GitHubVariable' { + Get-GitHubVariable @scope -IncludeInherited | Export-GitHubVariable + $result = Get-ChildItem -Path env: | Where-Object { $_.Name -like "$variablePrefix*" } + LogGroup 'Variables' { + Write-Host "$($result | Select-Object * | Format-Table | Out-String)" + } + $result | Should -Not -BeNullOrEmpty + } + It 'Remove-GitHubVariable' { $testVarName = "$variablePrefix`TestVariable*" LogGroup 'Before remove' { @@ -324,6 +333,15 @@ Describe 'Environments' { $result | Should -Not -BeNullOrEmpty } + It 'Export-GitHubVariable' { + Get-GitHubVariable @scope -IncludeInherited | Export-GitHubVariable + $result = Get-ChildItem -Path env: | Where-Object { $_.Name -like "$variablePrefix*" } + LogGroup 'Variables' { + Write-Host "$($result | Select-Object * | Format-Table | Out-String)" + } + $result | Should -Not -BeNullOrEmpty + } + It 'Remove-GitHubVariable' { $before = Get-GitHubVariable @scope -Name "*$os*" LogGroup 'Variables - Before' { @@ -406,6 +424,15 @@ Describe 'Environments' { $result | Should -Not -BeNullOrEmpty } + It 'Export-GitHubVariable' { + Get-GitHubVariable @scope -IncludeInherited | Export-GitHubVariable + $result = Get-ChildItem -Path env: | Where-Object { $_.Name -like "$variablePrefix*" } + LogGroup 'Variables' { + Write-Host "$($result | Select-Object * | Format-Table | Out-String)" + } + $result | Should -Not -BeNullOrEmpty + } + It 'Remove-GitHubVariable' { LogGroup 'Variables - Before' { $before = Get-GitHubVariable @scope -Name "*$os*" From 179b594930b31921503202562fad17897dc89505 Mon Sep 17 00:00:00 2001 From: Marius Storhaug <marstor@hotmail.com> Date: Fri, 4 Apr 2025 11:19:50 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Update=20document?= =?UTF-8?q?ation=20for=20`Export-GitHubVariable`=20to=20clarify=20input=20?= =?UTF-8?q?parameter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Variables/Export-GitHubVariable.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/public/Variables/Export-GitHubVariable.ps1 b/src/functions/public/Variables/Export-GitHubVariable.ps1 index bd6e39ee8..f920c0a75 100644 --- a/src/functions/public/Variables/Export-GitHubVariable.ps1 +++ b/src/functions/public/Variables/Export-GitHubVariable.ps1 @@ -17,7 +17,7 @@ function Export-GitHubVariable { Exports the variables retrieved from the GitHub API to the local environment. .INPUTS - GitHubVariable. Accepts objects representing GitHub variables via pipeline input. + GitHubVariable .OUTPUTS void