Skip to content

🩹 [Patch]: Move functionality of setting local environment variable #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/functions/public/Variables/Export-GitHubVariable.ps1
Original file line number Diff line number Diff line change
@@ -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
.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"
}
}
11 changes: 0 additions & 11 deletions src/functions/public/Variables/Get-GitHubVariable.ps1
Original file line number Diff line number Diff line change
@@ -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
}

27 changes: 27 additions & 0 deletions tests/Variables.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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*"