Skip to content

Commit ecd4796

Browse files
committed
Initial azure devops files
1 parent b7f6612 commit ecd4796

11 files changed

Lines changed: 749 additions & 3 deletions

File tree

alz/azuredevops/locals.files.tf

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

1616
target_folder_name = ".pipelines"
1717

18-
starter_module_config = local.is_bicep_iac_type ? jsondecode(file("${var.module_folder_path}/${var.bicep_config_file_path}")).starter_modules[var.starter_module_name] : null
18+
# Select config file based on IAC type
19+
config_file_path = local.is_bicep_avm ? var.bicep_avm_config_file_path : var.bicep_config_file_path
20+
21+
starter_module_config = local.is_bicep_iac_type ? jsondecode(file("${var.module_folder_path}/${local.config_file_path}")).starter_modules[var.starter_module_name] : null
1922
script_files_all = local.is_bicep_iac_type ? local.starter_module_config.deployment_files : []
2023
destroy_script_path = local.is_bicep_iac_type ? local.starter_module_config.destroy_script_path : ""
2124

@@ -55,6 +58,8 @@ locals {
5558
repository_name_templates = local.repository_name_templates
5659
ci_template_path = "${local.target_folder_name}/${local.ci_template_file_name}"
5760
cd_template_path = "${local.target_folder_name}/${local.cd_template_file_name}"
61+
ci_bicep_avm_template_path = "${local.target_folder_name}/${local.ci_template_file_name}"
62+
cd_bicep_avm_template_path = "${local.target_folder_name}/${local.cd_template_file_name}"
5863
script_files = local.script_files
5964
script_file_groups = local.script_file_groups
6065
root_module_folder_relative_path = var.root_module_folder_relative_path
@@ -84,6 +89,21 @@ locals {
8489
}
8590
}
8691

92+
# Add parameters.json for bicep-avm
93+
parameters_json_file = local.is_bicep_avm ? {
94+
"parameters.json" = {
95+
content = templatefile("${path.module}/scripts-bicep-avm/parameters.json.tftpl", {
96+
management_group_id = local.root_parent_management_group_id
97+
subscription_id_management = try(var.subscription_ids["management"], var.subscription_id_management, "")
98+
subscription_id_identity = try(var.subscription_ids["identity"], var.subscription_id_identity, "")
99+
subscription_id_connectivity = try(var.subscription_ids["connectivity"], var.subscription_id_connectivity, "")
100+
subscription_id_security = try(var.subscription_ids["security"], var.subscription_id_security, "")
101+
location = var.bootstrap_location
102+
network_type = local.networking_type
103+
})
104+
}
105+
} : {}
106+
87107
# Build a map of module files and turn on the terraform backend block
88108
module_files = { for key, value in module.files.files : key =>
89109
{
@@ -132,6 +152,6 @@ locals {
132152
module_files_filtered = { for key, value in local.module_files_supported : key => value if !contains(local.excluded_module_files, key) }
133153

134154
# Create final maps of all files to be included in the repositories
135-
repository_files = merge(local.cicd_files, local.module_files_filtered, var.use_separate_repository_for_templates ? {} : local.cicd_template_files, local.architecture_definition_file)
155+
repository_files = merge(local.cicd_files, local.module_files_filtered, var.use_separate_repository_for_templates ? {} : local.cicd_template_files, local.architecture_definition_file, local.parameters_json_file)
136156
template_repository_files = var.use_separate_repository_for_templates ? local.cicd_template_files : {}
137157
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
trigger:
3+
branches:
4+
include:
5+
- main
6+
7+
parameters:
8+
- name: skip_what_if
9+
displayName: 'Skip What If Check?'
10+
type: boolean
11+
default: false
12+
%{ for script_file in script_files ~}
13+
- name: ${script_file.name}
14+
displayName: 'Run Step: ${script_file.displayName}'
15+
type: boolean
16+
default: true
17+
%{ endfor ~}
18+
19+
resources:
20+
repositories:
21+
- repository: templates
22+
type: git
23+
name: ${project_name}/${repository_name_templates}
24+
25+
lockBehavior: sequential
26+
27+
extends:
28+
template: ${cd_bicep_avm_template_path}@templates
29+
parameters:
30+
skip_what_if: $${{ parameters.skip_what_if }}
31+
%{ for script_file in script_files ~}
32+
${script_file.name}: $${{ parameters['${script_file.name}'] }}
33+
%{ endfor ~}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
trigger:
3+
- none
4+
5+
resources:
6+
repositories:
7+
- repository: templates
8+
type: git
9+
name: ${project_name}/${repository_name_templates}
10+
11+
lockBehavior: sequential
12+
13+
extends:
14+
template: ${ci_bicep_avm_template_path}@templates
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
trigger: none
3+
4+
parameters:
5+
- name: skip_what_if
6+
displayName: 'Skip What-If Stage'
7+
type: boolean
8+
default: false
9+
%{ for script_file in script_files ~}
10+
- name: ${script_file.name}
11+
displayName: '${script_file.displayName}'
12+
type: boolean
13+
default: true
14+
%{ endfor ~}
15+
16+
variables:
17+
PARAMETERS_FILE_NAME: 'parameters.json'
18+
19+
stages:
20+
- stage: WhatIf
21+
displayName: 'What If'
22+
condition: ne('$${{ parameters.skip_what_if }}', true)
23+
jobs:
24+
- deployment: whatif
25+
displayName: 'What If Deployment'
26+
pool:
27+
${agent_pool_configuration}
28+
environment: '${environment_name_plan}'
29+
strategy:
30+
runOnce:
31+
deploy:
32+
steps:
33+
- task: PowerShell@2
34+
displayName: 'Show Parameters'
35+
inputs:
36+
targetType: 'inline'
37+
pwsh: true
38+
script: |
39+
Write-Host "Parameters:"
40+
Write-Host "skip_what_if: $${{ parameters.skip_what_if }}"
41+
%{ for script_file in script_files ~}
42+
Write-Host "${script_file.name}: $${{ parameters['${script_file.name}'] }}"
43+
%{ endfor ~}
44+
45+
- checkout: self
46+
displayName: 'Checkout Bicep Module'
47+
48+
- template: ./helpers/bicep-avm-variables.yaml
49+
parameters:
50+
parametersFileName: $(PARAMETERS_FILE_NAME)
51+
52+
- task: AzurePowerShell@5
53+
displayName: 'Check for First Deployment'
54+
inputs:
55+
azureSubscription: '${service_connection_name_plan}'
56+
pwsh: true
57+
azurePowerShellVersion: LatestVersion
58+
ScriptType: "InlineScript"
59+
Inline: |
60+
$managementGroupId = "$$(MANAGEMENT_GROUP_ID)"
61+
$intRootMgId = "$$managementGroupId-int-root"
62+
$managementGroups = Get-AzManagementGroup
63+
$intRootMg = $$managementGroups | Where-Object { $$.Name -eq $$intRootMgId }
64+
65+
$firstDeployment = $$true
66+
67+
if($$intRootMg -eq $$null) {
68+
Write-Warning "Cannot find the $$intRootMgId Management Group, so assuming this is the first deployment. We must skip what-if for some deployments since their dependent resources do not exist yet."
69+
} else {
70+
Write-Host "Found the $$intRootMgId Management Group, so assuming this is not the first deployment."
71+
$$firstDeployment = $$false
72+
}
73+
74+
Write-Host "##vso[task.setvariable variable=FIRST_DEPLOYMENT;]$$firstDeployment"
75+
76+
- template: ./helpers/bicep-avm-installer.yaml
77+
parameters:
78+
serviceConnection: '${service_connection_name_plan}'
79+
80+
%{ for script_file in script_files ~}
81+
%{ if try(script_file.networkType, "") != "" ~}
82+
- $${{ if and(eq(parameters['${script_file.name}'], true), eq(variables['NETWORK_TYPE'], '${script_file.networkType}')) }}:
83+
- template: ./helpers/bicep-avm-deploy.yaml
84+
parameters:
85+
displayName: '${script_file.displayName} (Network Type: ${script_file.networkType})'
86+
serviceConnection: '${service_connection_name_plan}'
87+
templateFilePath: '${script_file.templateFilePath}'
88+
templateParametersFilePath: '${script_file.templateParametersFilePath}'
89+
managementGroupId: '${script_file.managementGroupIdVariable}'
90+
subscriptionId: '${script_file.subscriptionIdVariable}'
91+
resourceGroupName: '${script_file.resourceGroupNameVariable}'
92+
location: '$(LOCATION)'
93+
deploymentType: '${script_file.deploymentType}'
94+
firstRunWhatIf: ${script_file.firstRunWhatIf}
95+
networkType: '${script_file.networkType}'
96+
whatIfEnabled: true
97+
98+
%{ else ~}
99+
- $${{ if eq(parameters['${script_file.name}'], true) }}:
100+
- template: ./helpers/bicep-avm-deploy.yaml
101+
parameters:
102+
displayName: '${script_file.displayName}'
103+
serviceConnection: '${service_connection_name_plan}'
104+
templateFilePath: '${script_file.templateFilePath}'
105+
templateParametersFilePath: '${script_file.templateParametersFilePath}'
106+
managementGroupId: '${script_file.managementGroupIdVariable}'
107+
subscriptionId: '${script_file.subscriptionIdVariable}'
108+
resourceGroupName: '${script_file.resourceGroupNameVariable}'
109+
location: '$(LOCATION)'
110+
deploymentType: '${script_file.deploymentType}'
111+
firstRunWhatIf: ${script_file.firstRunWhatIf}
112+
whatIfEnabled: true
113+
114+
%{ endif ~}
115+
%{ endfor ~}
116+
117+
- stage: Deploy
118+
displayName: 'Deploy'
119+
dependsOn: WhatIf
120+
condition: and(not(failed()), not(canceled()))
121+
jobs:
122+
- deployment: deploy
123+
displayName: 'Deploy Bicep'
124+
pool:
125+
${agent_pool_configuration}
126+
environment: '${environment_name_apply}'
127+
strategy:
128+
runOnce:
129+
deploy:
130+
steps:
131+
- task: PowerShell@2
132+
displayName: 'Show Parameters'
133+
inputs:
134+
targetType: 'inline'
135+
pwsh: true
136+
script: |
137+
Write-Host "Parameters:"
138+
Write-Host "skip_what_if: $${{ parameters.skip_what_if }}"
139+
%{ for script_file in script_files ~}
140+
Write-Host "${script_file.name}: $${{ parameters['${script_file.name}'] }}"
141+
%{ endfor ~}
142+
143+
- checkout: self
144+
displayName: 'Checkout Bicep Module'
145+
146+
- template: ./helpers/bicep-avm-variables.yaml
147+
parameters:
148+
parametersFileName: $(PARAMETERS_FILE_NAME)
149+
150+
- task: AzurePowerShell@5
151+
displayName: 'Check for First Deployment'
152+
inputs:
153+
azureSubscription: '${service_connection_name_apply}'
154+
pwsh: true
155+
azurePowerShellVersion: LatestVersion
156+
ScriptType: "InlineScript"
157+
Inline: |
158+
$managementGroupId = "$$(MANAGEMENT_GROUP_ID)"
159+
$intRootMgId = "$$managementGroupId-int-root"
160+
$managementGroups = Get-AzManagementGroup
161+
$intRootMg = $$managementGroups | Where-Object { $$.Name -eq $$intRootMgId }
162+
163+
$firstDeployment = $$true
164+
165+
if($$intRootMg -eq $$null) {
166+
Write-Warning "Cannot find the $$intRootMgId Management Group, so assuming this is the first deployment."
167+
} else {
168+
Write-Host "Found the $$intRootMgId Management Group, so assuming this is not the first deployment."
169+
$$firstDeployment = $$false
170+
}
171+
172+
Write-Host "##vso[task.setvariable variable=FIRST_DEPLOYMENT;]$$firstDeployment"
173+
174+
- template: ./helpers/bicep-avm-installer.yaml
175+
parameters:
176+
serviceConnection: '${service_connection_name_apply}'
177+
178+
%{ for script_file in script_files ~}
179+
%{ if try(script_file.networkType, "") != "" ~}
180+
- $${{ if and(eq(parameters['${script_file.name}'], true), eq(variables['NETWORK_TYPE'], '${script_file.networkType}')) }}:
181+
- template: ./helpers/bicep-avm-deploy.yaml
182+
parameters:
183+
displayName: '${script_file.displayName} (Network Type: ${script_file.networkType})'
184+
serviceConnection: '${service_connection_name_apply}'
185+
templateFilePath: '${script_file.templateFilePath}'
186+
templateParametersFilePath: '${script_file.templateParametersFilePath}'
187+
managementGroupId: '${script_file.managementGroupIdVariable}'
188+
subscriptionId: '${script_file.subscriptionIdVariable}'
189+
resourceGroupName: '${script_file.resourceGroupNameVariable}'
190+
location: '$(LOCATION)'
191+
deploymentType: '${script_file.deploymentType}'
192+
firstRunWhatIf: ${script_file.firstRunWhatIf}
193+
networkType: '${script_file.networkType}'
194+
whatIfEnabled: false
195+
196+
%{ else ~}
197+
- $${{ if eq(parameters['${script_file.name}'], true) }}:
198+
- template: ./helpers/bicep-avm-deploy.yaml
199+
parameters:
200+
displayName: '${script_file.displayName}'
201+
serviceConnection: '${service_connection_name_apply}'
202+
templateFilePath: '${script_file.templateFilePath}'
203+
templateParametersFilePath: '${script_file.templateParametersFilePath}'
204+
managementGroupId: '${script_file.managementGroupIdVariable}'
205+
subscriptionId: '${script_file.subscriptionIdVariable}'
206+
resourceGroupName: '${script_file.resourceGroupNameVariable}'
207+
location: '$(LOCATION)'
208+
deploymentType: '${script_file.deploymentType}'
209+
firstRunWhatIf: ${script_file.firstRunWhatIf}
210+
whatIfEnabled: false
211+
212+
%{ endif ~}
213+
%{ endfor ~}

0 commit comments

Comments
 (0)