From d6d566bbde01133000ef1ae81e4ed2db3f5f8980 Mon Sep 17 00:00:00 2001 From: Chris Meagher Date: Sat, 7 Oct 2023 19:29:01 +0100 Subject: [PATCH] Fix issue with env vars not merging in preprovision script --- .azd/scripts/create-infra-env-vars.ps1 | 45 ++++++++++++++++++++++---- .env.local.template | 4 --- .github/workflows/azure-dev.yml | 2 ++ infra/main.bicep | 2 +- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/.azd/scripts/create-infra-env-vars.ps1 b/.azd/scripts/create-infra-env-vars.ps1 index ee7917d..ab75c35 100644 --- a/.azd/scripts/create-infra-env-vars.ps1 +++ b/.azd/scripts/create-infra-env-vars.ps1 @@ -1,6 +1,7 @@ function Remove-Quotes { param( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] + [AllowEmptyString()] [string]$value, [string]$quoteChar = '"' ) @@ -14,7 +15,7 @@ function Remove-Quotes { function Read-EnvVars { param( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [string]$path ) @@ -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 "'" @@ -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 @@ -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 diff --git a/.env.local.template b/.env.local.template index 1d1e710..64e94d4 100644 --- a/.env.local.template +++ b/.env.local.template @@ -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= diff --git a/.github/workflows/azure-dev.yml b/.github/workflows/azure-dev.yml index e55f410..047b6dd 100644 --- a/.github/workflows/azure-dev.yml +++ b/.github/workflows/azure-dev.yml @@ -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 diff --git a/infra/main.bicep b/infra/main.bicep index 88c90e5..9d76538 100644 --- a/infra/main.bicep +++ b/infra/main.bicep @@ -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'