forked from libre-devops/ci-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulate-LocalVars.ps1
66 lines (59 loc) · 2.76 KB
/
Populate-LocalVars.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# PowerShell script to set user-level environment variables with predefined values for Linux
# Define a hashtable with key-value pairs for environment variables
$predefinedVariableValues = @{
"WORKING_DIRECTORY" = "example_working_directory"
"RUN_TERRAFORM_INIT" = "true"
"RUN_TERRAFORM_PLAN" = "true"
"RUN_TERRAFORM_PLAN_DESTROY" = "false"
"RUN_TERRAFORM_APPLY" = "false"
"RUN_TERRAFORM_DESTROY" = "false"
"ENABLE_DEBUG_MODE" = "true"
"DELETE_PLAN_FILES" = "true"
"TERRAFORM_VERSION" = "latest"
"BACKEND_STORAGE_SUBSCRIPTION_ID" = "example_subscription_id"
"BACKEND_STORAGE_RESOURCE_GROUP_NAME" = "example_resource_group_name"
"BACKEND_STORAGE_ACCOUNT_NAME" = "example_account_name"
"BACKEND_STORAGE_ACCOUNT_BLOB_CONTAINER_NAME" = "example_blob_container_name"
"TERRAFORM_STATE_NAME" = "example_state_name"
}
# Set each predefined variable
foreach ($varName in $predefinedVariableValues.Keys) {
$value = $predefinedVariableValues[$varName]
if ($isLinux) {
# Ensure the PowerShell profile directory exists
$profileDirectory = [System.IO.Path]::GetDirectoryName($PROFILE)
if (-not (Test-Path $profileDirectory)) {
New-Item -ItemType Directory -Path $profileDirectory -Force
}
# Ensure the PowerShell profile file exists
if (-not (Test-Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
# Append to PowerShell profile and set in current session
$exportCommand = "`n`$Env:$varName = `"$value`""
Add-Content -Path $PROFILE -Value $exportCommand
Set-Variable -Name $varName -Value $value -Scope Global
}
elseif ($IsMacOS)
{
# Ensure the PowerShell profile directory exists
$profileDirectory = [System.IO.Path]::GetDirectoryName($PROFILE)
if (-not (Test-Path $profileDirectory)) {
New-Item -ItemType Directory -Path $profileDirectory -Force
}
# Ensure the PowerShell profile file exists
if (-not (Test-Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
# Append to PowerShell profile and set in current session
$exportCommand = "`n`$Env:$varName = `"$value`""
Add-Content -Path $PROFILE -Value $exportCommand
Set-Variable -Name $varName -Value $value -Scope Global
}
else {
# On other systems, set user environment variable
[System.Environment]::SetEnvironmentVariable($varName, $value, [System.EnvironmentVariableTarget]::User)
}
}
Write-Host "User-level environment variables have been set."
Write-Host "Please close your powershell window and reopen to refresh environment" -ForegroundColor Yellow