Skip to content

Commit

Permalink
Merge pull request #6 from CMeeg/feature/fix-azd-provision-image-reset
Browse files Browse the repository at this point in the history
  • Loading branch information
CMeeg authored Nov 9, 2023
2 parents 3fe3673 + d57c993 commit d7f9704
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .azd/hooks/postdown.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

$envAzdPath = Join-Path $scriptDir "../../.azure/${env:AZURE_ENV_NAME}/.env"

if (!(Test-Path $envAzdPath -PathType Leaf)) {
# azd env file does not exist so there is nothing to do

return
}

# Remove any env vars that were most likely set by provisioning the infrastructure - the infrastructure has been removed so they are no longer relevant

$keysToKeep = @("AZURE_ENV_NAME", "AZURE_LOCATION", "AZURE_SUBSCRIPTION_ID", "AZURE_TENANT_ID")

$envVars = @{}

Get-Content $envAzdPath | ForEach-Object {
$key, $value = $_ -split '=', 2

if ($keysToKeep -contains $key) {
$envVars[$key] = $value
}
}

$envVars.Keys | Sort-Object | ForEach-Object {
"$_=$($envVars[$_])"
} | Out-File $envAzdPath
8 changes: 6 additions & 2 deletions .azd/scripts/create-infra-env-vars.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,24 @@ function Merge-Objects {

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

# Read `.env`, `.env.production` and `.env.local` files into memory
# Read `.env`, `.env.production`, azd `.env` and `.env.local` files into memory

$envPath = Join-Path $scriptDir "../../.env"
$env = Read-EnvVars -path $envPath

$envProductionPath = Join-Path $scriptDir "../../.env.production"
$envProduction = Read-EnvVars -path $envProductionPath

$envAzdPath = Join-Path $scriptDir "../../.azure/${env:AZURE_ENV_NAME}/.env"
$envAzd = Read-EnvVars -path $envAzdPath

$envLocalPath = Join-Path $scriptDir "../../.env.local"
$envLocal = Read-EnvVars -path $envLocalPath

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

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

# Produce a `env-vars.json` file that can be used by the infra scripts
Expand Down
1 change: 1 addition & 0 deletions .env.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ SERVICE_WEB_CONTAINER_MIN_REPLICAS=
SERVICE_WEB_CONTAINER_MAX_REPLICAS=
SERVICE_WEB_CUSTOM_DOMAIN_NAME=
SERVICE_WEB_CUSTOM_DOMAIN_CERT_ID=
SERVICE_WEB_IMAGE_NAME=
3 changes: 3 additions & 0 deletions azure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ hooks:
postprovision:
shell: pwsh
run: ./.azd/hooks/postprovision.ps1
postdown:
shell: pwsh
run: ./.azd/hooks/postdown.ps1
services:
web:
project: ./
Expand Down
3 changes: 3 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ module webAppServiceContainerApp './containers/container-app.bicep' = {
containerAppEnvironmentId: containerAppEnvironment.outputs.id
userAssignedIdentityId: webAppServiceIdentity.outputs.id
containerRegistryName: containerRegistry.outputs.name
imageName: stringOrDefault(envVars.SERVICE_WEB_IMAGE_NAME, '')
containerCpuCoreCount: stringOrDefault(envVars.SERVICE_WEB_CONTAINER_CPU_CORE_COUNT, '0.5')
containerMemory: stringOrDefault(envVars.SERVICE_WEB_CONTAINER_MEMORY, '1.0Gi')
containerMinReplicas: intOrDefault(envVars.SERVICE_WEB_CONTAINER_MIN_REPLICAS, 0)
Expand Down Expand Up @@ -222,6 +223,7 @@ module webAppServiceContainerApp './containers/container-app.bicep' = {
// azd outputs
output AZURE_LOCATION string = location
output AZURE_TENANT_ID string = tenant().tenantId
output AZURE_RESOURCE_GROUP string = resourceGroup.name

// Container outputs
output AZURE_CONTAINER_ENVIRONMENT_NAME string = containerAppEnvironment.outputs.name
Expand All @@ -239,3 +241,4 @@ output NEXT_PUBLIC_BASE_URL string = webAppServiceUri
output NEXT_PUBLIC_BUILD_ID string = buildId
output NEXT_PUBLIC_CDN_HOSTNAME string = webAppServiceCdn.outputs.endpointHostName
output NEXT_PUBLIC_CDN_URL string = webAppServiceCdn.outputs.endpointUri
output SERVICE_WEB_ENDPOINTS string[] = [webAppServiceUri]

0 comments on commit d7f9704

Please sign in to comment.