-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeployARMtoAzure.ps1
392 lines (344 loc) · 17.6 KB
/
DeployARMtoAzure.ps1
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<#
.DESCRIPTION
This Script deploys ARM templates to Microsoft Azure from a given folder structure via PowerShell
Attention: To run properly, it needs the Az Module for PowerShell, at leat in Version 4.1.0
To install it, run
Install-Module -Name Az -AllowClobber -MinimumVersion 4.1.0
Be aware, that you need to either set a GitHub Secret for AZURE_CREDENTIALS when using GitHub Actions or use Connect-AzAccount when using the script without GitHub Actions
When deploying to Tenant level, you need to set proper permission. To do so, use
New-AzRoleAssignment -SignInName "[userId]" -Scope "/" -RoleDefinitionName "Owner"
or
az role assignment create --role "Owner" --assignee "[userId]" --scope "/"
A more detailed description and explanations can be found here: https://www.hertes.net/2020/06/deploy-multiple-arm-templates-to-azure-using-powershell-and-github-actions/
As this script is intended to be used with GitHub actions, there is no login to Azure in here. If you are going to use the script without GitHub Actions, do something like "Connect-AzAccount" right after the last function declaration.
.NOTES
AUTHOR: Haiko Hertes
Microsoft MVP & Azure Architect
LASTEDIT: 2020/06/03
#>
$ARMSubfolderName = "arm"
$TenantLevelFolderName = "1_TenantLevelDeployments"
$ManagementGroupLevelFolderName = "2_ManagementGroupLevelDeployments"
$SubscriptionLevelFolderName = "3_SubscriptionLevelDeployments"
$ResourceGroupLevelFolderName = "4_ResourceGroupLevelDeployments"
$SortOrderFile = "order.txt"
$SortOrderByFolderValue = "SortByFolderName"
$SortOrderByFileValue = "SortByFileName"
$DefaultLocationFileName = "DefaultLocation.txt"
$LocationFileName = "location.txt"
$UseRGLocationInsteadOfDefaultLocation = $true # When set to true, resource deployments without given location.txt file will just inherit RGs location
$ScopeFileName = "scope.txt"
$DefaultLocation = ((Get-Content "$(Join-Path -Path $PSScriptRoot -ChildPath $ARMSubfolderName)\$DefaultLocationFileName") -notmatch '^#')[0]
Clear-Host
Write-Host "$DefaultLocation will be used as default location."
function GetLocation([string]$Directory){
# Gets the location from given Path or uses default location
If(Test-Path -Path (Join-Path $Directory -ChildPath $LocationFileName))
{ # This is to ignore comments with '#'
Return ((Get-Content -Path (Join-Path $Directory -ChildPath $LocationFileName)) | Where {$_ -notmatch '^#.*'} | Select-Object -First 1)
}
else
{
Write-Host " No $LocationFileName file found - using default location"
Return $DefaultLocation
}
}
function GetScope([string]$Directory){
# Gets the scope from given path - file content or foldername
If(Test-Path -Path (Join-Path $Directory -ChildPath $ScopeFileName))
{ # This is to ignore comments with '#'
Return ((Get-Content -Path (Join-Path $Directory -ChildPath $ScopeFileName)) | Where {$_ -notmatch '^#.*'} | Select-Object -First 1)
}
else
{
Write-Host " No $ScopeFileName file found - using foldername as scope"
Return (Split-Path $Directory -Leaf)
}
}
Write-Host "`n-----------------------------------------------------------`n"
#region Tenant Level Deployments
# Checking if folder structure exists
$FolderName = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath $ARMSubfolderName) -ChildPath $TenantLevelFolderName -Resolve -ErrorVariable folderDoesNotExist
If($folderDoesNotExist)
{
Write-Host -ForegroundColor Red ">>> Tenant Level Folder $TenantLevelFolderName does not exist under $ARMSubfolderName - skipping this part!"
}
else
{
# Tenant Level Deployment
Write-Host ">>> Starting with the Tenant Level Deployments..."
# Getting all template files that are json files but not parameters
$templates = Get-ChildItem -Path $FolderName -Filter "*.json" -File -Recurse | Where Name -notlike "*parameters.json"
Write-Host ">> Found $($templates.Count) JSON template(s)..."
# Sorting the files in the given order
If(Test-Path "$FolderName\$SortOrderFile")
{ # This is to ignore comments with '#'
If(((Get-Content "$FolderName\$SortOrderFile")| Where {$_ -notmatch '^#.*'} | Select-Object -First 1) -like "*$SortOrderByFileValue*")
{
Write-Host " Sorting ByFileName..."
$templates = $templates | Sort-Object -Property BaseName
}
else # Default - sort by foldername and filename
{
Write-Host " Sorting ByFolderName..."
$templates = $templates | Sort-Object -Property Directory,BaseName
}
}
Write-Host "`n>> Will handle these files in the named order:"
$templates.FullName.replace($PSScriptRoot,"")
# Iterate through all template files and deploy them
ForEach($template in $templates)
{
Write-Host "`n> Using $($template.Name)..."
# Getting location from file or use default location
$Location = GetLocation -Directory $template.DirectoryName
Write-Host " Location will be $Location"
# Getting scope from file or folder name
$Scope = GetScope -Directory $template.DirectoryName
Write-Host " Scope will be $Scope"
# Setting scope to given value
$Context = Set-AzContext -Tenant $Scope
$Tags = @{}
# Load Tags if tags.txt does exist
# This needs to be so ugly for now as -Tag does not accept empty hashtable - so we need to have both options - with and without Tags
If(Test-Path -Path "$($template.DirectoryName)\tags.txt")
{
$Tags = Get-Content "$($template.DirectoryName)\tags.txt" -Raw | ConvertFrom-StringData
Write-Host " Found and loaded Tags."
# Starting the deployment
Write-Host " Deploying $($template.Name)..."
$AzTenantDeployment = New-AzTenantDeployment `
-Name ($template.Basename) `
-Location $Location `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json" `
-Tag $Tags
}
else
{
# Deployment without Tags
Write-Host " Deploying $($template.Name)..."
$AzTenantDeployment = New-AzTenantDeployment `
-Name ($template.Basename) `
-Location $Location `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json"
}
}
Write-Host ">>> Done with the Tenant Level Deployments."
}
#endregion
Write-Host "`n-----------------------------------------------------------`n"
#region Management Group Level Deployments
$FolderName = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath $ARMSubfolderName) -ChildPath $ManagementGroupLevelFolderName -Resolve -ErrorVariable folderDoesNotExist
If($folderDoesNotExist)
{
Write-Host -ForegroundColor Red ">>> Management Group Level Folder $ManagementGroupLevelFolderName does not exist under $ARMSubfolderName - skipping this part!"
}
else
{
# Management Group Level Deployment
Write-Host ">>> Starting with the Management Group Level Deployments..."
# Getting all template files that are json files but not parameters
$templates = Get-ChildItem -Path $FolderName -Filter "*.json" -File -Recurse | Where Name -notlike "*parameters.json"
Write-Host ">> Found $($templates.Count) JSON template(s)..."
# Sorting the files in the given order
If(Test-Path "$FolderName\$SortOrderFile")
{
If(((Get-Content "$FolderName\$SortOrderFile") -notmatch '^#')[0] -like "*$SortOrderByFileValue*")
{
Write-Host " Sorting ByFileName..."
$templates = $templates | Sort-Object -Property BaseName
}
else # Default - sort by foldername and filename
{
Write-Host " Sorting ByFolderName..."
$templates = $templates | Sort-Object -Property Directory,BaseName
}
}
Write-Host "`n>> Will handle these files in the named order:"
$templates.FullName.replace($PSScriptRoot,"")
# Iterate through all template files and deploy them
ForEach($template in $templates)
{
Write-Host "`n> Using $($template.Name)..."
# Getting location from file or use default location
$Location = GetLocation -Directory $template.DirectoryName
Write-Host " Location will be $Location"
# Getting scope from file or folder name
$Scope = GetScope -Directory $template.DirectoryName
Write-Host " Scope will be $Scope"
$Tags = @{}
# Load Tags if tags.txt does exist
# This needs to be so ugly for now as -Tag does not accept empty hashtable - so we need to have both options - with and without Tags
If(Test-Path -Path "$($template.DirectoryName)\tags.txt")
{
$Tags = Get-Content "$($template.DirectoryName)\tags.txt" -Raw | ConvertFrom-StringData
Write-Host " Found and loaded Tags."
# Starting the deployment
Write-Host " Deploying $($template.Name)..."
$AzManagementGroupDeployment = New-AzManagementGroupDeployment `
-Name ($template.Basename) `
-ManagementGroupId $Scope `
-Location $Location `
-Tag $Tags `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json"
}
else
{
# Deployment without Tags
Write-Host " Deploying $($template.Name)..."
$AzManagementGroupDeployment = New-AzManagementGroupDeployment `
-Name ($template.Basename) `
-ManagementGroupId $Scope `
-Location $Location `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json"
}
}
Write-Host ">>> Done with the Management Group Level Deployments."
}
#endregion
Write-Host "`n-----------------------------------------------------------`n"
#region Subscription Level Deployments
$FolderName = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath $ARMSubfolderName) -ChildPath $SubscriptionLevelFolderName -Resolve -ErrorVariable folderDoesNotExist
If($folderDoesNotExist)
{
Write-Host -ForegroundColor Red ">>> Subscription Level Folder $SubscriptionLevelFolderName does not exist under $ARMSubfolderName - skipping this part!"
}
else
{
# Subscription Level Deployment
Write-Host ">>> Starting with the Subscription Level Deployments..."
# Getting all template files that are json files but not parameters
$templates = Get-ChildItem -Path $FolderName -Filter "*.json" -File -Recurse | Where Name -notlike "*parameters.json"
Write-Host ">> Found $($templates.Count) JSON template(s)..."
# Sorting the files in the given order
If(Test-Path "$FolderName\$SortOrderFile")
{
If(((Get-Content "$FolderName\$SortOrderFile") -notmatch '^#')[0] -like "*$SortOrderByFileValue*")
{
Write-Host " Sorting ByFileName..."
$templates = $templates | Sort-Object -Property BaseName
}
else # Default - sort by foldername and filename
{
Write-Host " Sorting ByFolderName..."
$templates = $templates | Sort-Object -Property Directory,BaseName
}
}
Write-Host "`n>> Will handle these files in the named order:"
$templates.FullName.replace($PSScriptRoot,"")
# Iterate through all template files and deploy them
ForEach($template in $templates)
{
Write-Host "`n> Using $($template.Name)..."
# Getting location from file or use default location
$Location = GetLocation -Directory $template.DirectoryName
Write-Host " Location will be $Location"
# Getting scope from file or folder name
$Scope = GetScope -Directory $template.DirectoryName
Write-Host " Scope will be $Scope"
# Setting scope to given value
$Context = Set-AzContext -Subscription $Scope
$Tags = @{}
# Load Tags if tags.txt does exist
# This needs to be so ugly for now as -Tag does not accept empty hashtable - so we need to have both options - with and without Tags
If(Test-Path -Path "$($template.DirectoryName)\tags.txt")
{
$Tags = Get-Content "$($template.DirectoryName)\tags.txt" -Raw | ConvertFrom-StringData
Write-Host " Found and loaded Tags."
# Starting the deployment
Write-Host " Deploying $($template.Name)..."
$AzSubscriptionDeployment = New-AzSubscriptionDeployment `
-Name ($template.Basename) `
-Location $Location `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json" `
-Tag $Tags
}
else
{
# Deployment without Tags
Write-Host " Deploying $($template.Name)..."
$AzSubscriptionDeployment = New-AzSubscriptionDeployment `
-Name ($template.Basename) `
-Location $Location `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json"
}
}
Write-Host ">>> Done with the Subscription Level Deployments."
}
#endregion
Write-Host "`n-----------------------------------------------------------`n"
#region Resource Group Level Deployments
$FolderName = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath $ARMSubfolderName) -ChildPath $ResourceGroupLevelFolderName -Resolve -ErrorVariable folderDoesNotExist
If($folderDoesNotExist)
{
Write-Host -ForegroundColor Red ">>> Resource Group Level Folder $ResourceGroupLevelFolderName does not exist under $ARMSubfolderName - skipping this part!"
}
else
{
# Resource Group Level Deployment
Write-Host ">>> Starting with the Resource Group Level Deployments..."
# Getting all template files that are json files but not parameters
$templates = Get-ChildItem -Path $FolderName -Filter "*.json" -File -Recurse | Where Name -notlike "*parameters.json"
Write-Host ">> Found $($templates.Count) JSON template(s)..."
# Sorting the files in the given order
If(Test-Path "$FolderName\$SortOrderFile")
{
If(((Get-Content "$FolderName\$SortOrderFile") -notmatch '^#')[0] -like "*$SortOrderByFileValue*")
{
Write-Host " Sorting ByFileName..."
$templates = $templates | Sort-Object -Property BaseName
}
else # Default - sort by foldername and filename
{
Write-Host " Sorting ByFolderName..."
$templates = $templates | Sort-Object -Property Directory,BaseName
}
}
Write-Host "`n>> Will handle these files in the named order:"
$templates.FullName.replace($PSScriptRoot,"")
# Iterate through all template files and deploy them
ForEach($template in $templates)
{
Write-Host "`n> Using $($template.Name)..."
# Getting scope from file or folder name
$Scope = GetScope -Directory $template.DirectoryName
Write-Host " Scope will be $Scope"
## Setting scope to given value
#$Context = Set-AzContext -Subscription $Scope
$Tags = @{}
# Load Tags if tags.txt does exist
# This needs to be so ugly for now as -Tag does not accept empty hashtable - so we need to have both options - with and without Tags
If(Test-Path -Path "$($template.DirectoryName)\tags.txt")
{
$Tags = Get-Content "$($template.DirectoryName)\tags.txt" -Raw | ConvertFrom-StringData
Write-Host " Found and loaded Tags."
# Starting the deployment
Write-Host " Deploying $($template.Name)..."
$AzResourceGroupDeployment = New-AzResourceGroupDeployment `
-Name ($template.Basename) `
-ResourceGroupName $Scope `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json" `
-Tag $Tags
}
else
{
# Deployment without Tags
Write-Host " Deploying $($template.Name)..."
$AzResourceGroupDeployment = New-AzResourceGroupDeployment `
-Name ($template.Basename) `
-ResourceGroupName $Scope `
-TemplateFile $template.FullName `
-TemplateParameterFile "$($template.DirectoryName)\$($template.BaseName).parameters.json"
}
}
Write-Host ">>> Done with the Resource Group Level Deployments."
}
#endregion
Write-Host "Done with the whole script!"