Skip to content

Commit 42e460a

Browse files
committed
Update logic for first deployment
1 parent 02667dd commit 42e460a

4 files changed

Lines changed: 102 additions & 11 deletions

File tree

alz/azuredevops/pipelines/bicep-avm/templates/cd-template.yaml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,22 @@ stages:
5757
azurePowerShellVersion: LatestVersion
5858
ScriptType: "InlineScript"
5959
Inline: |
60-
$managementGroupId = $env:MANAGEMENT_GROUP_ID
61-
$intRootMgId = "$managementGroupId-int-root"
60+
# Read the int-root management group name from the bicepparam file
61+
$intRootParamFile = "templates/core/governance/mgmt-groups/int-root/main.bicepparam"
62+
if (Test-Path $intRootParamFile) {
63+
$paramContent = Get-Content $intRootParamFile -Raw
64+
if ($paramContent -match 'managementGroupName:\s*[''"](\w+)[''"]+') {
65+
$intRootMgId = $matches[1]
66+
Write-Host "Found int-root management group name in param file: $intRootMgId"
67+
} else {
68+
Write-Warning "Could not parse managementGroupName from $intRootParamFile, using default 'alz'"
69+
$intRootMgId = "alz"
70+
}
71+
} else {
72+
Write-Warning "Int-root param file not found at $intRootParamFile, using default 'alz'"
73+
$intRootMgId = "alz"
74+
}
75+
6276
$managementGroups = Get-AzManagementGroup
6377
$intRootMg = $managementGroups | Where-Object { $_.Name -eq $intRootMgId }
6478
@@ -155,8 +169,22 @@ stages:
155169
azurePowerShellVersion: LatestVersion
156170
ScriptType: "InlineScript"
157171
Inline: |
158-
$managementGroupId = $env:MANAGEMENT_GROUP_ID
159-
$intRootMgId = "$managementGroupId-int-root"
172+
# Read the int-root management group name from the bicepparam file
173+
$intRootParamFile = "templates/core/governance/mgmt-groups/int-root/main.bicepparam"
174+
if (Test-Path $intRootParamFile) {
175+
$paramContent = Get-Content $intRootParamFile -Raw
176+
if ($paramContent -match 'managementGroupName:\s*[''"](\w+)[''"]+') {
177+
$intRootMgId = $matches[1]
178+
Write-Host "Found int-root management group name in param file: $intRootMgId"
179+
} else {
180+
Write-Warning "Could not parse managementGroupName from $intRootParamFile, using default 'alz'"
181+
$intRootMgId = "alz"
182+
}
183+
} else {
184+
Write-Warning "Int-root param file not found at $intRootParamFile, using default 'alz'"
185+
$intRootMgId = "alz"
186+
}
187+
160188
$managementGroups = Get-AzManagementGroup
161189
$intRootMg = $managementGroups | Where-Object { $_.Name -eq $intRootMgId }
162190

alz/azuredevops/pipelines/bicep-avm/templates/ci-template.yaml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,22 @@ stages:
7373
azurePowerShellVersion: LatestVersion
7474
ScriptType: "InlineScript"
7575
Inline: |
76-
$managementGroupId = $env:MANAGEMENT_GROUP_ID
77-
$intRootMgId = "$managementGroupId-int-root"
76+
# Read the int-root management group name from the bicepparam file
77+
$intRootParamFile = "templates/core/governance/mgmt-groups/int-root/main.bicepparam"
78+
if (Test-Path $intRootParamFile) {
79+
$paramContent = Get-Content $intRootParamFile -Raw
80+
if ($paramContent -match 'managementGroupName:\s*[''"](\w+)[''"]+') {
81+
$intRootMgId = $matches[1]
82+
Write-Host "Found int-root management group name in param file: $intRootMgId"
83+
} else {
84+
Write-Warning "Could not parse managementGroupName from $intRootParamFile, using default 'alz'"
85+
$intRootMgId = "alz"
86+
}
87+
} else {
88+
Write-Warning "Int-root param file not found at $intRootParamFile, using default 'alz'"
89+
$intRootMgId = "alz"
90+
}
91+
7892
$managementGroups = Get-AzManagementGroup
7993
$intRootMg = $managementGroups | Where-Object { $_.Name -eq $intRootMgId }
8094
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
param(
2+
[string]$managementGroupId
3+
)
4+
5+
# Read the int-root management group name from the bicepparam file
6+
$intRootParamFile = "templates/core/governance/mgmt-groups/int-root/main.bicepparam"
7+
if (Test-Path $intRootParamFile) {
8+
$paramContent = Get-Content $intRootParamFile -Raw
9+
if ($paramContent -match 'managementGroupName:\s*[''"](\w+)[''"]+') {
10+
$intRootMgId = $matches[1]
11+
Write-Host "Found int-root management group name in param file: $intRootMgId"
12+
} else {
13+
Write-Warning "Could not parse managementGroupName from $intRootParamFile, using default 'alz'"
14+
$intRootMgId = "alz"
15+
}
16+
} else {
17+
Write-Warning "Int-root param file not found at $intRootParamFile, using default 'alz'"
18+
$intRootMgId = "alz"
19+
}
20+
21+
$managementGroups = Get-AzManagementGroup
22+
$intRootMg = $managementGroups | Where-Object { $_.Name -eq $intRootMgId }
23+
24+
$firstDeployment = $true
25+
26+
if($null -eq $intRootMg) {
27+
Write-Warning "Cannot find the $intRootMgId Management Group, so assuming this is the first deployment. We must skip checking some deployments since their dependent resources do not exist yet."
28+
} else {
29+
Write-Host "Found the $intRootMgId Management Group, so assuming this is not the first deployment."
30+
$firstDeployment = $false
31+
}
32+
33+
return $firstDeployment

alz/local/scripts/bicep-first-deployment-check.ps1

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@ param(
22
[string]$managementGroupId
33
)
44

5+
# Read the int-root management group name from the bicepparam file
6+
$intRootParamFile = "templates/core/governance/mgmt-groups/int-root/main.bicepparam"
7+
if (Test-Path $intRootParamFile) {
8+
$paramContent = Get-Content $intRootParamFile -Raw
9+
if ($paramContent -match 'managementGroupName:\s*[''"](\w+)[''"]+') {
10+
$intRootMgId = $matches[1]
11+
Write-Host "Found int-root management group name in param file: $intRootMgId"
12+
} else {
13+
Write-Warning "Could not parse managementGroupName from $intRootParamFile, using default 'alz'"
14+
$intRootMgId = "alz"
15+
}
16+
} else {
17+
Write-Warning "Int-root param file not found at $intRootParamFile, using default 'alz'"
18+
$intRootMgId = "alz"
19+
}
20+
521
$managementGroups = Get-AzManagementGroup
6-
$managementGroup = $managementGroups | Where-Object { $_.Name -eq $managementGroupId }
22+
$intRootMg = $managementGroups | Where-Object { $_.Name -eq $intRootMgId }
723

824
$firstDeployment = $true
925

10-
if($null -eq $managementGroup) {
11-
Write-Warning "Cannot find the $managementGroupId Management Group, so assuming this is the first deployment. We must skip checking some deployments since their dependent resources do not exist yet."
26+
if ($null -eq $intRootMg) {
27+
Write-Warning "Cannot find the $intRootMgId Management Group, so assuming this is the first deployment. We must skip checking some deployments since their dependent resources do not exist yet."
1228
} else {
13-
Write-Host "Found the $managementGroupId Management Group, so assuming this is not the first deployment."
29+
Write-Host "Found the $intRootMgId Management Group, so assuming this is not the first deployment."
1430
$firstDeployment = $false
1531
}
1632

17-
return $firstDeployment
33+
return $firstDeployment

0 commit comments

Comments
 (0)