Skip to content

Commit

Permalink
docs(week 21-22): add packer, powershell (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusgb authored Jun 1, 2023
1 parent baf3c85 commit 5d5c6c2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .editorconfig
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
47 changes: 47 additions & 0 deletions packer/whitespace-in-env-vars.md
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
```
14 changes: 14 additions & 0 deletions powershell/set-env-vars-differing-scope.md
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)`

0 comments on commit 5d5c6c2

Please sign in to comment.