-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(week 21-22): add packer, powershell (#30)
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
root = true | ||
|
||
# All Files | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_style = tab | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
# Unix-style newlines with a newline ending every file | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
######################### | ||
# File Extension Settings | ||
######################### | ||
|
||
[*.{yml,yaml}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
# Markdown Files | ||
# | ||
# Two spaces at the end of a line in Markdown mean "new line", | ||
# so trimming trailing whitespace for such files can cause breakage. | ||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# 'Packer - whitespace in env vars | ||
|
||
When setting [environment variables Packer](https://developer.hashicorp.com/packer/docs/provisioners/powershell#environment_vars), make sure there's no space in between. | ||
If there's a whitespace, Packer passes on that whitespace within the value of the env var. | ||
|
||
Using `#` as a marker, we can see what value our env var will look like: | ||
|
||
```hcl | ||
provisioner "powershell" { | ||
environment_vars = [ | ||
"ENV_VAR_WITHOUT_SPACE=${var.tmp_folder}", | ||
"ENV_VAR_WITH_SPACE = ${var.tmp_folder}" | ||
] | ||
inline = [ | ||
"$ErrorActionPreference='Stop'", | ||
"Write-Host \"EnvVarWithoutSpace:#$Env:ENV_VAR_WITHOUT_SPACE\"", | ||
"Write-Host \"EnvVarWithSpace:#$Env:ENV_VAR_WITH_SPACE\"" | ||
] | ||
} | ||
``` | ||
|
||
output is | ||
|
||
```console | ||
. . . | ||
EnvVarWithoutSpace:#C:\tmp\packer-env-var | ||
EnvVarWithSpace:# C:\tmp\packer-env-var | ||
``` | ||
|
||
Another way to deal with this is to call the `Trim` method before processing those env vars: | ||
|
||
```powershell | ||
# check if they exist | ||
$expectedEnvVars = @( | ||
$env:ENV_VAR_WITHOUT_SPACE, | ||
$env:ENV_VAR_WITH_SPACE | ||
) | ||
foreach ($envVar in $expectedEnvVars) { | ||
if ([string]::IsNullOrWhiteSpace($envVar)) { | ||
throw "$envVar is null or is empty" | ||
} | ||
} | ||
$MyVar1 = ($env:ENV_VAR_WITHOUT_SPACE).Trim() | ||
$MyVar2 = ($env:ENV_VAR_WITH_SPACE).Trim() | ||
# do something with $MyVar1 and $MyVar2 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Powershell - set env vars in different scopes | ||
|
||
While trying to understand [this line](https://github.com/actions/runner-images/blob/035dc70f77d4353de9fbacd66be4f0797d4d9f62/images/win/scripts/ImageHelpers/PathHelpers.ps1#L63) from the utilities that GitHub uses for their GitHub Runners, it occurred to me that environment variables are defined in different contexts that affects how long they last. | ||
|
||
Thanks to [shellgeek](https://shellgeek.com/set-environment-variable-using-powershell/), there's a way to set the env vars in PowerShell accordingly: | ||
|
||
- Env vars for the duration of the powershell session and process: | ||
`$Env:GoHugo = "C:\Hugo\bin\"` | ||
|
||
- Env var for the User account (System Settings -> Advanced -> Environment Variables -> User): | ||
`[System.Environment]::SetEnvironmentVariable('GoHugo','C:\Hugo\bin',[System.EnvironmentVariableTarget]::User)` | ||
|
||
- Env var for the machine in (System Settings -> Advanced -> Environment Variables -> System): | ||
`[System.Environment]::SetEnvironmentVariable('GoHugo','C:\Hugo\bin',[System.EnvironmentVariableTarget]::Machine)` |