Skip to content

Commit

Permalink
Fix issue with env vars not merging in preprovision script
Browse files Browse the repository at this point in the history
  • Loading branch information
CMeeg committed Oct 7, 2023
1 parent 3a49e54 commit d6d566b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
45 changes: 39 additions & 6 deletions .azd/scripts/create-infra-env-vars.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function Remove-Quotes {
param(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[string]$value,
[string]$quoteChar = '"'
)
Expand All @@ -14,7 +15,7 @@ function Remove-Quotes {

function Read-EnvVars {
param(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string]$path
)

Expand All @@ -32,7 +33,7 @@ function Read-EnvVars {
$key, $value = $_.Name, $_.Value

if (($null -eq $value) -or ($value -eq "")) {
$envVars[$key] = ""
$envVars[$key] = $null
} else {
$value = Remove-Quotes -value $value -quoteChar '"'
$value = Remove-Quotes -value $value -quoteChar "'"
Expand All @@ -44,6 +45,38 @@ function Read-EnvVars {
return $envVars
}

function Merge-Objects {
param(
[Parameter(Mandatory = $true)]
$base,
[Parameter(Mandatory = $true)]
$with
)

$merged = @{}
$base.GetEnumerator() | ForEach-Object { $merged[$_.Key] = $_.Value }

$with.GetEnumerator() | ForEach-Object {
$withValue = $_.Value

if ($merged.ContainsKey($_.Key)) {
$baseValue = $merged[$_.Key]

if ($null -eq $withValue -and $null -ne $baseValue) {
# Keep the base value
$merged[$_.Key] = $baseValue
} else {
# Overwrite the base value
$merged[$_.Key] = $null -eq $withValue ? "" : $withValue
}
} else {
$merged[$_.Key] = $null -eq $withValue ? "" : $withValue
}
}

return $merged
}

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Read `.env`, `.env.production` and `.env.local` files into memory
Expand All @@ -59,11 +92,11 @@ $envLocal = Read-EnvVars -path $envLocalPath

# Merge `.env.production` and `.env.local` into `.env` (duplicate keys will be overwritten)

$env += $envProduction
$env += $envLocal
$envVars = Merge-Objects -base $env -with $envProduction
$envVars = Merge-Objects -base $envVars -with $envLocal

# Produce a `env-vars.json` file that can be used by the infra scripts

$outputPath = Join-Path $scriptDir "../../infra/env-vars.json"

$env | ConvertTo-Json | Out-File -FilePath $outputPath -Encoding utf8
$envVars | ConvertTo-Json | Out-File -FilePath $outputPath -Encoding utf8
4 changes: 0 additions & 4 deletions .env.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
# In CI this file is used to create a `.env.local` specific to the target environment so it's important to keep this file up to date as you add new env vars for your app or infra and update the `main.bicep` file accordingly
# You can include values in here as an example, but don't include any secrets or sensitive data that you don't want to commit to your repo - any values set here will be ignored when creating the `.env.local` file in CI

# project settings
PROJECT_NAME=

# web app settings
SERVICE_WEB_MIN_LOG_LEVEL=
SERVICE_WEB_SERVICE_NAME=

## infra settings
SERVICE_WEB_CONTAINER_CPU_CORE_COUNT=
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/azure-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ jobs:
run: npm run env:init
shell: pwsh
env:
SERVICE_WEB_MIN_LOG_LEVEL: ${{ vars.SERVICE_WEB_MIN_LOG_LEVEL }}
SERVICE_WEB_CONTAINER_CPU_CORE_COUNT: ${{ vars.SERVICE_WEB_CONTAINER_CPU_CORE_COUNT }}
SERVICE_WEB_CONTAINER_MEMORY: ${{ vars.SERVICE_WEB_CONTAINER_MEMORY }}
SERVICE_WEB_CONTAINER_MIN_REPLICAS: ${{ vars.SERVICE_WEB_CONTAINER_MIN_REPLICAS }}
SERVICE_WEB_CONTAINER_MAX_REPLICAS: ${{ vars.SERVICE_WEB_CONTAINER_MAX_REPLICAS }}
SERVICE_WEB_CUSTOM_DOMAIN_NAME: ${{ vars.SERVICE_WEB_CUSTOM_DOMAIN_NAME }}
SERVICE_WEB_CUSTOM_DOMAIN_CERT_ID: ${{ vars.SERVICE_WEB_CUSTOM_DOMAIN_CERT_ID }}

- name: Provision Infrastructure
run: azd provision --no-prompt
Expand Down
2 changes: 1 addition & 1 deletion infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ module webAppServiceContainerApp './containers/container-app.bicep' = {
}
{
name: 'SERVICE_WEB_CUSTOM_DOMAIN_NAME'
value: stringOrDefault(envVars.SERVICE_WEB_CUSTOM_DOMAIN_NAME, '')
value: webAppServiceCustomDomainName
}
{
name: 'SERVICE_WEB_MIN_LOG_LEVEL'
Expand Down

0 comments on commit d6d566b

Please sign in to comment.