Skip to content

Instantly share code, notes, and snippets.

@techdecline
Last active November 29, 2022 08:34
Show Gist options
  • Save techdecline/cdf0809dceac9771fc17d49c1d7bd82f to your computer and use it in GitHub Desktop.
Save techdecline/cdf0809dceac9771fc17d49c1d7bd82f to your computer and use it in GitHub Desktop.
Extract GitLab Variables from Projects and Groups to declare Environment Variables in Bash and PowerShell
[cmdletbinding()]
param (
[String]$GitLabUrl,
[String]$GitLabAccessToken,
[String]$GroupId,
[String]$ProjectId,
[switch]$Pwsh
)
$env:GITLAB_URL = $GitLabUrl
$env:GITLAB_ACCESS_TOKEN = $GitLabAccessToken
$varArr = [System.Collections.ArrayList]@()
Write-Verbose "Fetching Group Variables for Group: $($GroupId)"
Get-GitlabGroupVariable -GroupId $GroupId | ForEach-Object {
$tmpObj = $_ | Select-Object @{Name = 'VariableName'; Expression = { $_.Key } }, @{Name = 'VariableValue'; Expression = { $_.Value } }
$null = $varArr.Add($tmpObj)
}
Write-Verbose "Fetching Project Variables for Project: $($ProjectId)"
Get-GitlabProjectVariable -ProjectId $ProjectId | ForEach-Object {
$tmpObj = $_ | Select-Object @{Name = 'VariableName'; Expression = { $_.Key } }, @{Name = 'VariableValue'; Expression = { $_.Value } }
$null = $varArr.Add($tmpObj)
}
$sb = [System.Text.StringBuilder]::new()
switch ($Pwsh) {
$true {
Write-Verbose "Variable will be declared for PowerShell Environment"
$varArr | ForEach-Object {
$null = $sb.AppendLine("`$ENV:$($_.VariableName) = `"$($_.VariableValue)`"")
}
}
$false {
Write-Verbose "Variable will be declared for Bash Environment"
$varArr | ForEach-Object {
$null = $sb.AppendLine("export $($_.VariableName)=`"$($_.VariableValue)`"")
}
}
}
return $sb.ToString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment