-
Notifications
You must be signed in to change notification settings - Fork 517
/
bicep-build-to-validate.yml
319 lines (289 loc) · 20.5 KB
/
bicep-build-to-validate.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
name: "ALZ-Bicep unit tests"
trigger: none
variables:
- group: csu-bicep-environment
- name: ResourceGroupName
value: "rsg-github-pr-$(System.PullRequest.PullRequestNumber)"
- name: ManagementGroupPrefix
value: "PR-$(System.PullRequest.PullRequestNumber)"
- name: TopLevelManagementGroupDisplayName
value: "PR $(System.PullRequest.PullRequestNumber) Azure Landing Zones"
- name: SubscriptionName
value: "sub-unit-test-pr-$(System.PullRequest.PullRequestNumber)"
jobs:
- job: bicep_deploy
displayName: Deploy Bicep Files for PR
pool:
vmImage: ubuntu-latest
steps:
- task: Bash@3
displayName: Check for managementGroup Changes
name: git_management_diff
inputs:
targetType: "inline"
script: |
git_diff1=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/managementGroups/managementGroups.bicep)
git_diff2=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/customRoleDefinitions/customRoleDefinitions.bicep)
git_diff3=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/policy/definitions/customPolicyDefinitions.bicep)
git_diff4=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/policy/assignments/policyAssignmentManagementGroup.bicep)
git_diff5=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/policy/assignments/alzDefaults/alzDefaultPolicyAssignments.bicep)
git_diff6=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/roleAssignments/roleAssignmentManagementGroup.bicep)
git_diff7=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/subscriptionPlacement/subscriptionPlacement.bicep)
git_diff8=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/orchestration/subPlacementAll/subPlacementAll.bicep)
git_diff9=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/mgDiagSettings/mgDiagSettings.bicep)
git_diff10=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/orchestration/mgDiagSettingsAll/mgDiagSettingsAll.bicep)
if [[ $git_diff1 != '' ]] || [[ $git_diff2 != '' ]] || [[ $git_diff3 != '' ]] || [[ $git_diff4 != '' ]] || [[ $git_diff5 != '' ]] || [[ $git_diff6 != '' ]] || [[ $git_diff7 != '' ]] || [[ $git_diff8 != '' ]] || [[ $git_diff9 != '' ]] || [[ $git_diff10 != '' ]]
then echo "##vso[task.setvariable variable=gitManagementOutput]setmgmt"
fi
echo
- task: Bash@3
displayName: Check for logging Changes
name: git_logging_diff
inputs:
targetType: "inline"
script: |
git_diff_logging1=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/logging/logging.bicep)
git_diff_logging2=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/mgDiagSettings/mgDiagSettings.bicep)
git_diff_logging3=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/orchestration/mgDiagSettingsAll/mgDiagSettingsAll.bicep)
if [[ $git_diff_logging1 != '' ]] || [[ $git_diff_logging2 != '' ]] || [[ $git_diff_logging3 != '' ]]
then echo "##vso[task.setvariable variable=gitLoggingOUTPUT]setlogging"
fi
echo
- task: Bash@3
displayName: Check for hubNetworking Changes
name: git_hubnetworking_diff
inputs:
targetType: "inline"
script: |
git_hub=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/hubNetworking/hubNetworking.bicep)
echo "##vso[task.setvariable variable=gitHubOUTPUT]$git_hub"
- task: Bash@3
displayName: Check for virtual network peer Changes
name: git_vnetpeer_diff
inputs:
targetType: "inline"
script: |
git_vnetpeer=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/vnetPeering ':(exclude)*.md' ':(exclude)*.png')
echo "##vso[task.setvariable variable=gitVnetPeerOUTPUT]$git_vnetpeer"
- task: Bash@3
displayName: Check for vwanNetworking Changes
name: git_vwannetworking_diff
inputs:
targetType: "inline"
script: |
git_vwan=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/vwanConnectivity/vwanConnectivity.bicep )
echo "##vso[task.setvariable variable=gitVwanOUTPUT]$git_vwan"
- task: Bash@3
displayName: Check for vwanNetwork Connection Changes
name: git_vwannetworkconnection_diff
inputs:
targetType: "inline"
script: |
git_vwannwc=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/vnetPeeringVwan ':(exclude)*.md' ':(exclude)*.png')
echo "##vso[task.setvariable variable=gitVwanNwcOUTPUT]$git_vwannwc"
- task: Bash@3
displayName: Check for spokeNetworking Changes
name: git_spokenetworking_diff
inputs:
targetType: "inline"
script: |
git_spoke=$(git diff --name-only HEAD^ HEAD infra-as-code/bicep/modules/spokeNetworking/spokeNetworking.bicep)
echo "gitSpokeOUTPUT=$git_spoke" >> $GITHUB_ENV
echo "##vso[task.setvariable variable=gitSpokeOUTPUT]$git_spoke"
- task: AzureCLI@2
displayName: 'Azure CLI Get Federated Token'
inputs:
azureSubscription: azserviceconnection
addSpnToEnvironment: true
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
echo "##vso[task.setvariable variable=ARM_CLIENT_ID]$servicePrincipalId"
echo "##vso[task.setvariable variable=ARM_ID_TOKEN]$idToken"
echo "##vso[task.setvariable variable=ARM_TENANT_ID]$tenantId"
- task: Bash@3
displayName: Login to Azure for Subsequent Tasks
name: git_azlogin
inputs:
targetType: "inline"
script: |
az login --service-principal -u $(ARM_CLIENT_ID) --tenant $(ARM_TENANT_ID) --allow-no-subscriptions --federated-token $(ARM_ID_TOKEN)
- task: Bash@3
displayName: Az CLI Create Subscription for PR
name: create_subscription
condition: or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], ''))
inputs:
targetType: "inline"
script: |
subid=$(az deployment tenant create --name "deploy-$(SubscriptionName)" --location $(Location) --template-file infra-as-code/bicep/CRML/subscriptionAlias/subscriptionAlias.bicep --parameters @infra-as-code/bicep/CRML/subscriptionAlias/parameters/subscriptionAlias.parameters.min.json --parameters parSubscriptionBillingScope=$(ALZ-AZURE-SECRET-EA-BILLING-ACCOUNT) parSubscriptionName=$(SubscriptionName) | jq .properties.outputs.outSubscriptionId.value | tr -d '"')
echo $subId
echo "##vso[task.setvariable variable=subscriptionId]$subid"
echo "##vso[task.setvariable variable=IsDeployed;isoutput=true]$subid"
- task: Bash@3
displayName: Az CLI Refresh subscription list
name: refresh_subscription
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az account list --refresh
- task: Bash@3
displayName: Az CLI Create Resource Group for PR
name: create_rsg
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az account set --subscription $(subscriptionId)
if [ $(az group exists --name $(ResourceGroupName) ) == false ]; then
sleep 300
fi
az group create --name $(ResourceGroupName) --location $(Location)
- task: Bash@3
displayName: Az CLI Register Resource Providers for PR
name: register_providers
inputs:
targetType: "inline"
script: |
az account set --subscription $(subscriptionId)
az provider register -n 'Microsoft.Insights'
- task: Bash@3
displayName: Az CLI Deploy Management Groups for PR
name: create_mgs
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment tenant create --template-file infra-as-code/bicep/modules/managementGroups/managementGroups.bicep --parameters @infra-as-code/bicep/modules/managementGroups/parameters/managementGroups.parameters.min.json parTopLevelManagementGroupPrefix=$(ManagementGroupPrefix) --location $(Location)
- task: Bash@3
displayName: Az CLI Deploy Custom Role Definitions for PR
name: create_rbac_roles
condition: and(ne(variables['gitManagementOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment mg create --template-file infra-as-code/bicep/modules/customRoleDefinitions/customRoleDefinitions.bicep --parameters @infra-as-code/bicep/modules/customRoleDefinitions/parameters/customRoleDefinitions.parameters.min.json parAssignableScopeManagementGroupId=$(ManagementGroupPrefix) --location $(Location) --management-group-id $(ManagementGroupPrefix)
- task: Bash@3
displayName: Az CLI Deploy Custom Policy Definitions for PR
name: create_policy_defs
condition: and(ne(variables['gitManagementOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment mg create --template-file infra-as-code/bicep/modules/policy/definitions/customPolicyDefinitions.bicep --parameters @infra-as-code/bicep/modules/policy/definitions/parameters/customPolicyDefinitions.parameters.min.json parTargetManagementGroupId=$(ManagementGroupPrefix) --location $(Location) --management-group-id $(ManagementGroupPrefix)
- task: Bash@3
displayName: Az CLI Deploy Logging for PR
name: create_logging
condition: and(ne(variables['gitLoggingOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment group create --resource-group $(ResourceGroupName) --template-file infra-as-code/bicep/modules/logging/logging.bicep --parameters @infra-as-code/bicep/modules/logging/parameters/logging.parameters.min.json
- task: Bash@3
displayName: Az CLI Deploy mgDiagSettingsAll for PR
name: create_mgDiagSettingsAll
condition: and(ne(variables['gitLoggingOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment mg create --template-file infra-as-code/bicep/orchestration/mgDiagSettingsAll/mgDiagSettingsAll.bicep --parameters @infra-as-code/bicep/orchestration/mgDiagSettingsAll/parameters/mgDiagSettingsAll.parameters.min.json parTopLevelManagementGroupPrefix="$(ManagementGroupPrefix)" parLogAnalyticsWorkspaceResourceId="/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.OperationalInsights/workspaces/alz-log-analytics" --location $(Location) --management-group-id $(ManagementGroupPrefix)
- task: Bash@3
displayName: Az CLI Subscription Placement for PR
name: move_sub
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment mg create --template-file infra-as-code/bicep/orchestration/subPlacementAll/subPlacementAll.bicep --parameters @infra-as-code/bicep/orchestration/subPlacementAll/parameters/subPlacementAll.parameters.min.json parTopLevelManagementGroupPrefix="$(ManagementGroupPrefix)" parPlatformConnectivityMgSubs='["$(subscriptionId)"]' --location $(Location) --management-group-id $(ManagementGroupPrefix)
- task: AzurePowerShell@5
displayName: Az PwSh alzDefaultPolicyAssignments for PR
name: alz_default_policy_assignments
condition: and(ne(variables['gitManagementOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
azureSubscription: "azserviceconnection"
ScriptType: "FilePath"
ScriptPath: ".github/scripts/Set-AlzDefaultPolicyAssignment.ps1"
ScriptArguments: '-ManagementGroupId "$(ManagementGroupPrefix)-platform" -parLocation $(Location) -templateFile ./infra-as-code/bicep/modules/policy/assignments/alzDefaults/alzDefaultPolicyAssignments.bicep -parameterFile .\infra-as-code\bicep\modules\policy\assignments\alzDefaults\parameters\alzDefaultPolicyAssignments.parameters.min.json -parTopLevelManagementGroupPrefix $(ManagementGroupPrefix) -parLogAnalyticsWorkSpaceAndAutomationAccountLocation $(Location) -parLogAnalyticsWorkspaceResourceId "/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.OperationalInsights/workspaces/alz-log-analytics" -parDdosProtectionPlanId "/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.Network/ddosProtectionPlans/alz-ddos-plan" -parPrivateDnsResourceGroupId "/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)"'
azurePowerShellVersion: "LatestVersion"
pwsh: true
- task: Bash@3
displayName: Az CLI Deploy Hub Networking for PR
name: create_hub_network
condition: and(or(ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment group create --resource-group $(ResourceGroupName) --template-file infra-as-code/bicep/modules/hubNetworking/hubNetworking.bicep --parameters @infra-as-code/bicep/modules/hubNetworking/parameters/hubNetworking.parameters.min.json
- task: Bash@3
displayName: Az CLI Deploy Spoke Networking for PR
name: create_spoke_network
condition: and(or(ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment group create --resource-group $(ResourceGroupName) --template-file infra-as-code/bicep/modules/spokeNetworking/spokeNetworking.bicep --parameters @infra-as-code/bicep/modules/spokeNetworking/parameters/spokeNetworking.parameters.min.json
- task: Bash@3
displayName: Az CLI Deploy vWan Networking for PR
name: create_vwan_network
condition: and(or(ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment group create --resource-group $(ResourceGroupName) --template-file infra-as-code/bicep/modules/vwanConnectivity/vwanConnectivity.bicep --parameters parVirtualNetworkIdToLink="/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/vnet-spoke"
- task: Bash@3
displayName: Az CLI Deploy vWan Network connection for PR
name: create_vwan_network_connection
condition: and(ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment sub create --location $(Location) --template-file infra-as-code/bicep/modules/vnetPeeringVwan/vnetPeeringVwan.bicep --parameters @infra-as-code/bicep/modules/vnetPeeringVwan/parameters/vnetPeeringVwan.parameters.min.json parVirtualWanHubResourceId="/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.Network/virtualHubs/alz-vhub-$(Location)" parRemoteVirtualNetworkResourceId="/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/vnet-spoke"
- task: Bash@3
displayName: Az CLI Deploy vNet Peer for PR spoke to hub
name: create_vnet_peer_spoke_2_hub
condition: and(ne(variables['gitVnetPeerOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment group create --resource-group $(ResourceGroupName) --template-file infra-as-code/bicep/modules/vnetPeering/vnetPeering.bicep --parameters @infra-as-code/bicep/modules/vnetPeering/parameters/vnetPeering.parameters.min.json parDestinationVirtualNetworkId="/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/alz-hub-$(Location)"
- task: Bash@3
displayName: Az CLI Deploy vNet Peer for PR hub to spoke
name: create_vnet_peer_hub_2_spoke
condition: and(ne(variables['gitVnetPeerOUTPUT'], ''), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
az deployment group create --resource-group $(ResourceGroupName) --template-file infra-as-code/bicep/modules/vnetPeering/vnetPeering.bicep --parameters @infra-as-code/bicep/modules/vnetPeering/parameters/vnetPeering.parameters.min.json parDestinationVirtualNetworkId="/subscriptions/$(subscriptionId)/resourceGroups/$(ResourceGroupName)/providers/Microsoft.Network/virtualNetworks/vnet-spoke" parSourceVirtualNetworkName="alz-hub-$(Location)" parDestinationVirtualNetworkName="vnet-spoke"
# Verify that WhatIf does not find differences between code and environment thats just been deployed
- task: Bash@3
displayName: Az CLI After Deployment What-If Management Groups for PR
name: whatif_mgs
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], ''), ne(variables['gitVwanNwcOUTPUT'], ''), ne(variables['gitVnetPeerOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: "inline"
script: |
result=$(az deployment tenant what-if --template-file infra-as-code/bicep/modules/managementGroups/managementGroups.bicep --parameters @infra-as-code/bicep/modules/managementGroups/parameters/managementGroups.parameters.min.json parTopLevelManagementGroupPrefix=$(ManagementGroupPrefix) --location $(Location) --exclude-change-types Ignore NoChange --only-show-errors)
if [[ $result != *'Resource changes: no change.'* ]]
then
echo "##vso[task.logissue type=error]WhatIf reports difference between code and environment thats just been deployed"
echo "$result"
exit 1
fi
- job: bicep_cleanup
dependsOn: bicep_deploy
variables:
isDeployed: $[ dependencies.bicep_deploy.outputs['create_subscription.IsDeployed'] ]
displayName: Cleanup Bicep Deployment for PR
pool:
vmImage: ubuntu-latest
steps:
- task: AzurePowerShell@5
displayName: Az CLI Remove/Cleanup Deployment
condition: ne(variables['isDeployed'], '')
inputs:
azureSubscription: "azserviceconnection"
ScriptType: "FilePath"
ScriptPath: ".github/scripts/Wipe-AlzTenant.ps1"
ScriptArguments: '-tenantRootGroupID $(azclitenant) -intermediateRootGroupID "$(ManagementGroupPrefix)" -subscriptionName "$(SubscriptionName)"'
azurePowerShellVersion: "LatestVersion"
pwsh: true