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 9c64ed8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 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: 4 additions & 0 deletions .github/workflows/azure-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ jobs:
run: npm run env:init
shell: pwsh
env:
PROJECT_NAME: ${{ vars.PROJECT_NAME }}
SERVICE_WEB_MIN_LOG_LEVEL: ${{ vars.SERVICE_WEB_MIN_LOG_LEVEL }}
SERVICE_WEB_SERVICE_NAME: ${{ vars.SERVICE_WEB_SERVICE_NAME }}
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 9c64ed8

Please sign in to comment.