Skip to content

Commit 8ec856b

Browse files
committed
Updates to deploy local
1 parent b3e0c09 commit 8ec856b

6 files changed

Lines changed: 76 additions & 40 deletions

File tree

alz/local/locals.files.tf

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ locals {
1515

1616
target_folder_name = local.is_bicep_avm ? "scripts-bicep-avm" : "scripts"
1717

18-
# Get a list of on-demand folders
19-
on_demand_folders = local.is_bicep_iac_type ? try(local.starter_module_config.on_demand_folders, []) : []
18+
# Get a list of on-demand folders (only for classic bicep, not bicep-avm)
19+
on_demand_folders = local.is_classic_bicep ? try(local.starter_module_config.on_demand_folders, []) : []
2020

21-
networking_type = local.is_classic_bicep && fileexists("${var.module_folder_path}/${var.bicep_parameters_file_path}") ? jsondecode(file("${var.module_folder_path}/${var.bicep_parameters_file_path}")).NETWORK_TYPE : ""
21+
networking_type = local.is_bicep_avm ? var.network_type : (local.is_classic_bicep && fileexists("${var.module_folder_path}/${var.bicep_parameters_file_path}") ? jsondecode(file("${var.module_folder_path}/${var.bicep_parameters_file_path}")).NETWORK_TYPE : "")
2222
script_files = local.is_bicep_iac_type ? { for script_file in local.script_files_all : format("%03d", script_file.order) => {
2323
name = script_file.name
2424
displayName = script_file.displayName
@@ -36,13 +36,27 @@ locals {
3636
{
3737
content = templatefile("${local.deploy_script_file_directory_path}/${deploy_script_file}", {
3838
script_files = local.script_files
39-
on_demand_folders = local.on_demand_folders
40-
on_demand_folder_repository = var.on_demand_folder_repository
41-
on_demand_folder_artifact_name = var.on_demand_folder_artifact_name
39+
on_demand_folders = local.is_classic_bicep ? local.on_demand_folders : []
40+
on_demand_folder_repository = local.is_classic_bicep ? var.on_demand_folder_repository : ""
41+
on_demand_folder_artifact_name = local.is_classic_bicep ? var.on_demand_folder_artifact_name : ""
4242
})
4343
}
4444
}
4545

46+
# Add parameters.json for bicep-avm
47+
parameters_json_file = local.is_bicep_avm ? {
48+
"${local.target_folder_name}/parameters.json" = {
49+
content = templatefile("${local.deploy_script_file_directory_path}/parameters.json.tftpl", {
50+
management_group_id = local.root_parent_management_group_id
51+
subscription_id_management = try(var.subscription_ids["management"], var.subscription_id_management, "")
52+
subscription_id_identity = try(var.subscription_ids["identity"], var.subscription_id_identity, "")
53+
subscription_id_connectivity = try(var.subscription_ids["connectivity"], var.subscription_id_connectivity, "")
54+
location = var.bootstrap_location
55+
network_type = local.networking_type
56+
})
57+
}
58+
} : {}
59+
4660
module_files = { for key, value in module.files.files : key =>
4761
{
4862
content = try(replace((file(value.path)), "# backend \"azurerm\" {}", (var.create_bootstrap_resources_in_azure ? "backend \"azurerm\" {}" : "backend \"local\" {}")), "unsupported_file_type")
@@ -52,13 +66,13 @@ locals {
5266
# Build a map of module files with types that are supported
5367
module_files_supported = { for key, value in local.module_files : key => value if value.content != "unsupported_file_type" && !endswith(key, "-cache.json") && !endswith(key, local.bicep_config_file_path) }
5468

55-
# Build a list of files to exclude from the repository based on the on-demand folders
56-
excluded_module_files = distinct(flatten([for exclusion in local.on_demand_folders :
69+
# Build a list of files to exclude from the repository based on the on-demand folders (only for classic bicep)
70+
excluded_module_files = local.is_classic_bicep ? distinct(flatten([for exclusion in local.on_demand_folders :
5771
[for key, value in local.module_files_supported : key if startswith(key, exclusion.target)]
58-
]))
72+
])) : []
5973

6074
# Filter out the excluded files
6175
module_files_filtered = { for key, value in local.module_files_supported : key => value if !contains(local.excluded_module_files, key) }
6276

63-
final_module_files = merge(local.module_files_filtered, local.deploy_script_files_parsed)
77+
final_module_files = merge(local.module_files_filtered, local.deploy_script_files_parsed, local.parameters_json_file)
6478
}

alz/local/scripts-bicep-avm/bicep-deploy.ps1

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
param(
2+
[string]$name,
23
[string]$displayName,
34
[string]$templateFilePath,
45
[string]$templateParametersFilePath,
@@ -9,6 +10,12 @@ param(
910
[string]$deploymentType
1011
)
1112

13+
# Resolve template paths relative to the script's parent directory (where templates folder is located)
14+
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
15+
$templateRoot = Split-Path -Parent $scriptRoot
16+
$templateFilePath = Join-Path $templateRoot $templateFilePath
17+
$templateParametersFilePath = Join-Path $templateRoot $templateParametersFilePath
18+
1219
Write-Host "<---------------------------------------------------------------------------->" -ForegroundColor Blue
1320
Write-Host "Starting deployment stack for $displayName..." -ForegroundColor Blue
1421
Write-Host "<---------------------------------------------------------------------------->" -ForegroundColor Blue
@@ -23,35 +30,42 @@ Write-Host "Resource Group Name: $resourceGroupName" -ForegroundColor DarkGray
2330
Write-Host "Location: $location" -ForegroundColor DarkGray
2431
Write-Host "Deployment Type: $deploymentType" -ForegroundColor DarkGray
2532

26-
$deploymentPrefix = $env:PREFIX
27-
$deploymentName = $displayName.Replace(" ", "-")
28-
$deploymentTimeStamp = Get-Date -Format 'yyyyMMddHHmmss'
29-
30-
$prefixPostFixAndHyphenLength = $deploymentPrefix.Length + $deploymentTimeStamp.Length + 2
31-
$deploymentNameMaxLength = 61 - $prefixPostFixAndHyphenLength
33+
$deploymentName = $name
34+
Write-Host "Deployment Stack Name: $deploymentName"
35+
Write-Host ""
3236

33-
if($deploymentName.Length -gt $deploymentNameMaxLength) {
34-
$deploymentName = $deploymentName.Substring(0, $deploymentNameMaxLength)
37+
# Check if template files exist
38+
if (-not (Test-Path $templateFilePath)) {
39+
Write-Error "Template file not found: $templateFilePath"
40+
throw "Template file not found: $templateFilePath"
3541
}
42+
Write-Host "Template file exists: $templateFilePath" -ForegroundColor Green
3643

37-
$deploymentName = "$deploymentPrefix-$deploymentName-$deploymentTimeStamp"
38-
Write-Host "Deployment Stack Name: $deploymentName"
44+
if (-not (Test-Path $templateParametersFilePath)) {
45+
Write-Error "Template parameters file not found: $templateParametersFilePath"
46+
throw "Template parameters file not found: $templateParametersFilePath"
47+
}
48+
Write-Host "Template parameters file exists: $templateParametersFilePath" -ForegroundColor Green
49+
Write-Host ""
3950

4051
$stackParameters = @{
4152
Name = $deploymentName
4253
TemplateFile = $templateFilePath
4354
TemplateParameterFile = $templateParametersFilePath
55+
DenySettingsMode = "None"
56+
ActionOnUnmanage = "DeleteAll"
4457
Force = $true
58+
Verbose = $true
4559
}
4660

4761
$retryCount = 0
48-
$retryMax = 30
62+
$retryMax = 5
4963
$initialRetryDelay = 20
50-
$retryDelayIncrement = 10
64+
$retryDelayIncrement = 5
5165
$finalSuccess = $false
5266

5367
while ($retryCount -lt $retryMax) {
54-
if($retryCount -gt 0) {
68+
if ($retryCount -gt 0) {
5569
$retryDelay = $initialRetryDelay + ($retryCount * $retryDelayIncrement)
5670
Write-Host "Retrying deployment stack after $retryDelay seconds..." -ForegroundColor Green
5771
Start-Sleep -Seconds $retryDelay
@@ -105,10 +119,10 @@ while ($retryCount -lt $retryMax) {
105119
}
106120

107121
Write-Host "Deployment Stack ID: $($result.Id)"
108-
Write-Host "Provisioning State: $($result.Properties.ProvisioningState)"
122+
Write-Host "Provisioning State: $($result.ProvisioningState)"
109123

110-
if($result.Properties.ProvisioningState -ne "Succeeded") {
111-
Write-Host "Deployment stack provisioning did not succeed. Entering retry loop..." -ForegroundColor Red
124+
if ($result.ProvisioningState -ne "succeeded") {
125+
Write-Host "Deployment stack provisioning did not succeed. Current state: $($result.ProvisioningState). Entering retry loop..." -ForegroundColor Red
112126
$retryCount++
113127
} else {
114128
$finalSuccess = $true

alz/local/scripts-bicep-avm/bicep-get-variables.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ param(
22
[string]$fileName="parameters.json"
33
)
44

5-
Write-Host "Getting variables from $fileName"
6-
$json = Get-Content -Path $fileName | ConvertFrom-Json
5+
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
6+
$parametersPath = Join-Path $scriptRoot $fileName
7+
8+
Write-Host "Getting variables from $parametersPath"
9+
$json = Get-Content -Path $parametersPath | ConvertFrom-Json
710

811
foreach ($key in $json.PSObject.Properties) {
912
$envVarName = $key.Name
1013
$envVarValue = $key.Value
11-
[Environment]::SetEnvironmentVariable($envVarName, $envVarValue)
14+
[Environment]::SetEnvironmentVariable($envVarName, $envVarValue, "Process")
1215
Write-Output "Set $envVarName to $envVarValue"
1316
}

alz/local/scripts-bicep-avm/deploy-local.ps1

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,19 @@ Write-Host "Verbose: $verbose"
88
# Getting the variables from the parameters.json file
99
& (Join-Path $scriptRoot 'bicep-get-variables.ps1') -fileName "parameters.json"
1010

11-
%{ for on_demand_folder in on_demand_folders ~}
12-
# Downloading the on demand folder for ${on_demand_folder.target}
13-
& (Join-Path $scriptRoot 'bicep-on-demand-folder.ps1') `
14-
-repository "${on_demand_folder_repository}" `
15-
-releaseArtifactName "${on_demand_folder_artifact_name}" `
16-
-releaseVersion $env:RELEASE_VERSION `
17-
-sourcePath "${on_demand_folder.source}" `
18-
-targetPath "${on_demand_folder.target}" `
19-
%{ endfor ~}
20-
2111
Write-Host ""
2212
$deployApproved = Read-Host -Prompt "Type 'yes' and hit Enter to continue with the full deployment"
2313
Write-Host ""
2414

25-
if($deployApproved -ne "yes") {
15+
if ($deployApproved -ne "yes") {
2616
Write-Error "Deployment was not approved. Exiting..."
2717
exit 1
2818
}
2919

3020
%{ for script_file in script_files ~}
3121
# Running deployment stack for ${script_file.displayName}
3222
& (Join-Path $scriptRoot 'bicep-deploy.ps1') `
23+
-name "${script_file.name}" `
3324
-displayName "${script_file.displayName}" `
3425
-templateFilePath "${script_file.templateFilePath}" `
3526
-templateParametersFilePath "${script_file.templateParametersFilePath}" `
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"MANAGEMENT_GROUP_ID": "${management_group_id}",
3+
"SUBSCRIPTION_ID_MANAGEMENT": "${subscription_id_management}",
4+
"SUBSCRIPTION_ID_IDENTITY": "${subscription_id_identity}",
5+
"SUBSCRIPTION_ID_CONNECTIVITY": "${subscription_id_connectivity}",
6+
"LOCATION": "${location}",
7+
"NETWORK_TYPE": "${network_type}"
8+
}

alz/local/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ variable "bootstrap_location" {
7777
default = ""
7878
}
7979

80+
variable "network_type" {
81+
description = "The network type for the deployment (e.g. 'hubNetworking' or 'vwanConnectivity')"
82+
type = string
83+
default = ""
84+
}
85+
8086
variable "on_demand_folder_repository" {
8187
description = "The repository to use for the on-demand folders"
8288
type = string

0 commit comments

Comments
 (0)