-
Notifications
You must be signed in to change notification settings - Fork 243
/
cd-validate-plan-apply-separate-stages.yml
472 lines (428 loc) · 22 KB
/
cd-validate-plan-apply-separate-stages.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
name: $(BuildDefinitionName)_$(date:yyyyMMdd)$(rev:.r)
trigger: none
pr: none
variables:
- group: global-variables
- name: azureSubscription
value: AzureSubscription
- name: terraformVersion
value: 1.0.7
- name: terraformPath
value: terraform
- name: tfstatePath
value: terraform.tfstate
- name: environment
value: production
- name: prefix
value: Babosbird
- name: location
value: westeurope
- name: kubernetesVersion
value: '1.21.2'
- name: resourceGroupName
value: '$(prefix)RG'
- name: aksClusterName
value: '$(prefix)Aks'
- name: logAnalyticsWorkspaceName
value: '$(prefix)AksWorkspace'
- name: domainNameLabel
value: '$(prefix)testvm'
- name: firewallName
value: '$(prefix)Firewall'
- name: acrName
value: '$(prefix)Acr'
- name: bastionHostName
value: '$(prefix)BastionHost'
- name: keyVaultName
value: '$(prefix)AksKeyVault'
- name: vmName
value: '$(prefix)Vm'
- name: hubVNetName
value: '$(prefix)HubVNet'
- name: aksVNetName
value: '$(prefix)AksVNet'
- name: containerName
value: scripts
- name: scriptPath
value: terraform/scripts
- name: scriptName
value: configure-jumpbox-vm.sh
pool:
vmImage: ubuntu-latest
stages :
- stage: validate
displayName: 'Terraform Validate'
jobs:
- job: validate
displayName: 'Terraform Validate'
continueOnError: false
steps:
- task: TerraformInstaller@0
displayName: 'Terraform Install'
inputs:
terraformVersion: $(terraformVersion)
- task: TerraformTaskV2@2
displayName: 'Terraform Init'
inputs:
provider: 'azurerm'
command: 'init'
backendServiceArm: $(azureSubscription)
backendAzureRmResourceGroupName: $(terraformBackendResourceGroupName)
backendAzureRmStorageAccountName: $(terraformBackendStorageAccountName)
backendAzureRmContainerName: '$(terraformBackendContainerName)'
backendAzureRmKey: $(tfstatePath)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: TerraformTaskV2@2
displayName: 'Terraform Validate'
inputs:
provider: 'azurerm'
command: 'validate'
- stage: plan
displayName: 'Terraform Plan'
dependsOn: [validate]
condition: succeeded('validate')
jobs:
- job: plan
displayName: 'Terraform Plan'
steps:
- checkout: self
- task: AzureCLI@2
displayName: 'Create Temporary Storage Account'
continueOnError: true
inputs:
scriptType: bash
scriptLocation: inlineScript
azureSubscription: $(azureSubscription)
addSpnToEnvironment: true
inlineScript: |
#Variables
postfix=$(cat /dev/urandom | tr -dc 'a-z' | fold -w 8 | head -n 1)
storageAccountResourceGroupName="rg$postfix"
storageAccountName="scriptstorage$postfix"
sku="Standard_LRS"
subscriptionName=$(az account show --query name --output tsv)
# Create resource group
echo "Checking if [$storageAccountResourceGroupName] resource group actually exists in the [$subscriptionName] subscription..."
az group show --name $storageAccountResourceGroupName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$storageAccountResourceGroupName] resource group actually exists in the [$subscriptionName] subscription"
echo "Creating [$storageAccountResourceGroupName] resource group in the [$subscriptionName] subscription..."
# Create the resource group
az group create \
--name $storageAccountResourceGroupName \
--location $(location) 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$storageAccountResourceGroupName] resource group successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$storageAccountResourceGroupName] resource group in the [$subscriptionName] subscription"
exit -1
fi
else
echo "[$storageAccountResourceGroupName] resource group already exists in the [$subscriptionName] subscription"
fi
# Create storage account
echo "Checking if [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription..."
az storage account --name $storageAccountName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription"
echo "Creating [$storageAccountName] storage account in the [$subscriptionName] subscription..."
az storage account create \
--resource-group $storageAccountResourceGroupName \
--name $storageAccountName \
--sku $sku \
--encryption-services blob 1>/dev/null
# Create the storage account
if [[ $? == 0 ]]; then
echo "[$storageAccountName] storage account successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$storageAccountName] storage account in the [$subscriptionName] subscription"
exit -1
fi
else
echo "[$storageAccountName] storage account already exists in the [$subscriptionName] subscription"
fi
# Get storage account key
echo "Retrieving the primary key of the [$storageAccountName] storage account..."
storageAccountKey=$(az storage account keys list --resource-group $storageAccountResourceGroupName --account-name $storageAccountName --query [0].value -o tsv)
if [[ -n $storageAccountKey ]]; then
echo "Primary key of the [$storageAccountName] storage account successfully retrieved"
else
echo "Failed to retrieve the primary key of the [$storageAccountName] storage account"
exit -1
fi
# Create blob container
echo "Checking if [$(containerName)] container actually exists in the [$storageAccountName] storage account..."
az storage container show \
--name $(containerName) \
--account-name $storageAccountName \
--account-key $storageAccountKey &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$(containerName)] container actually exists in the [$storageAccountName] storage account"
echo "Creating [$(containerName)] container in the [$storageAccountName] storage account..."
# Create the container
az storage container create \
--name $(containerName) \
--account-name $storageAccountName \
--account-key $storageAccountKey 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$(containerName)] container successfully created in the [$storageAccountName] storage account"
else
echo "Failed to create [$(containerName)] container in the [$storageAccountName] storage account"
exit -1
fi
else
echo "[$(containerName)] container already exists in the [$storageAccountName] storage account"
fi
# Copy script as blob to the storage account container
az storage blob upload \
--container-name $(containerName) \
--name $(scriptName) \
--account-name $storageAccountName \
--account-key $storageAccountKey \
--file "$(System.DefaultWorkingDirectory)/$(scriptPath)/$(scriptName)"
if [[ $? == 0 ]]; then
echo "[$(scriptName)] successfully copied to the [$(containerName)] container in the [$storageAccountName] storage account"
else
echo "Failed to copy the [$(scriptName)] script to the [$(containerName)] container in the [$storageAccountName] storage account"
exit -1
fi
# Print data
echo "----------------------------------------------------------------------------------------------"
echo "storageAccountName: $storageAccountName"
echo "containerName: $(containerName)"
echo "##vso[task.setvariable variable=storageAccountResourceGroupName;]$storageAccountResourceGroupName"
echo "##vso[task.setvariable variable=storageAccountName;]$storageAccountName"
echo "##vso[task.setvariable variable=storageAccountKey;]$storageAccountKey"
echo "##vso[task.setvariable variable=ok;]true"
- bash: |
echo "Storage Account Name: $(storageAccountName)"
echo "Storage Account Key: $(storageAccountKey)"
displayName: 'Print Variables'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
- task: TerraformInstaller@0
displayName: 'Terraform Install'
inputs:
terraformVersion: $(terraformVersion)
- task: TerraformTaskV2@2
displayName: 'Terraform Init'
inputs:
provider: 'azurerm'
command: 'init'
backendServiceArm: $(azureSubscription)
backendAzureRmResourceGroupName: $(terraformBackendResourceGroupName)
backendAzureRmStorageAccountName: $(terraformBackendStorageAccountName)
backendAzureRmContainerName: '$(terraformBackendContainerName)'
backendAzureRmKey: $(tfstatePath)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: TerraformTaskV2@2
displayName: 'Terraform Plan'
inputs:
provider: 'azurerm'
command: 'plan'
commandOptions: '-input=false -var ssh_public_key=$(sshPublicKey) -var location=$(location) -var="script_storage_account_name=$(storageAccountName)" -var="script_storage_account_key=$(storageAccountKey)" -var="hub_vnet_name=$(hubVNetName)" -var="aks_vnet_name=$(aksVNetName)" -var="vm_name=$(vmName)" -var kubernetes_version=$(kubernetesVersion) -var resource_group_name=$(resourceGroupName) -var aks_cluster_name=$(aksClusterName) -var log_analytics_workspace_name=$(logAnalyticsWorkspaceName) -var domain_name_label=$(domainNameLabel) -var firewall_name=$(firewallName) -var acr_name=$(acrName) -var bastion_host_name=$(bastionHostName) -var key_vault_name=$(keyVaultName)'
environmentServiceNameAzureRM: $(azureSubscription)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: AzureCLI@2
displayName: 'Delete Temporary Storage Account'
condition: always()
inputs:
scriptType: bash
scriptLocation: inlineScript
azureSubscription: $(azureSubscription)
addSpnToEnvironment: true
inlineScript: |
az group delete \
--name $(storageAccountResourceGroupName) \
--yes
if [[ $? == 0 ]]; then
echo "Temporary storage account successfully deleted"
else
echo "Failed to delete the temporary storage account"
exit -1
fi
- stage: apply
displayName: 'Terraform Apply'
dependsOn: [plan]
condition: succeeded('plan')
jobs:
- job: apply
displayName: 'Terraform Apply'
steps:
- checkout: self
- task: AzureCLI@2
displayName: 'Create Temporary Storage Account'
continueOnError: true
inputs:
scriptType: bash
scriptLocation: inlineScript
azureSubscription: $(azureSubscription)
addSpnToEnvironment: true
inlineScript: |
#Variables
postfix=$(cat /dev/urandom | tr -dc 'a-z' | fold -w 8 | head -n 1)
storageAccountResourceGroupName="rg$postfix"
storageAccountName="scriptstorage$postfix"
sku="Standard_LRS"
subscriptionName=$(az account show --query name --output tsv)
# Create resource group
echo "Checking if [$storageAccountResourceGroupName] resource group actually exists in the [$subscriptionName] subscription..."
az group show --name $storageAccountResourceGroupName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$storageAccountResourceGroupName] resource group actually exists in the [$subscriptionName] subscription"
echo "Creating [$storageAccountResourceGroupName] resource group in the [$subscriptionName] subscription..."
# Create the resource group
az group create \
--name $storageAccountResourceGroupName \
--location $(location) 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$storageAccountResourceGroupName] resource group successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$storageAccountResourceGroupName] resource group in the [$subscriptionName] subscription"
exit -1
fi
else
echo "[$storageAccountResourceGroupName] resource group already exists in the [$subscriptionName] subscription"
fi
# Create storage account
echo "Checking if [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription..."
az storage account --name $storageAccountName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription"
echo "Creating [$storageAccountName] storage account in the [$subscriptionName] subscription..."
az storage account create \
--resource-group $storageAccountResourceGroupName \
--name $storageAccountName \
--sku $sku \
--encryption-services blob 1>/dev/null
# Create the storage account
if [[ $? == 0 ]]; then
echo "[$storageAccountName] storage account successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$storageAccountName] storage account in the [$subscriptionName] subscription"
exit -1
fi
else
echo "[$storageAccountName] storage account already exists in the [$subscriptionName] subscription"
fi
# Get storage account key
echo "Retrieving the primary key of the [$storageAccountName] storage account..."
storageAccountKey=$(az storage account keys list --resource-group $storageAccountResourceGroupName --account-name $storageAccountName --query [0].value -o tsv)
if [[ -n $storageAccountKey ]]; then
echo "Primary key of the [$storageAccountName] storage account successfully retrieved"
else
echo "Failed to retrieve the primary key of the [$storageAccountName] storage account"
exit -1
fi
# Create blob container
echo "Checking if [$(containerName)] container actually exists in the [$storageAccountName] storage account..."
az storage container show \
--name $(containerName) \
--account-name $storageAccountName \
--account-key $storageAccountKey &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$(containerName)] container actually exists in the [$storageAccountName] storage account"
echo "Creating [$(containerName)] container in the [$storageAccountName] storage account..."
# Create the container
az storage container create \
--name $(containerName) \
--account-name $storageAccountName \
--account-key $storageAccountKey 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$(containerName)] container successfully created in the [$storageAccountName] storage account"
else
echo "Failed to create [$(containerName)] container in the [$storageAccountName] storage account"
exit -1
fi
else
echo "[$(containerName)] container already exists in the [$storageAccountName] storage account"
fi
# Copy script as blob to the storage account container
az storage blob upload \
--container-name $(containerName) \
--name $(scriptName) \
--account-name $storageAccountName \
--account-key $storageAccountKey \
--file "$(System.DefaultWorkingDirectory)/$(scriptPath)/$(scriptName)"
if [[ $? == 0 ]]; then
echo "[$(scriptName)] successfully copied to the [$(containerName)] container in the [$storageAccountName] storage account"
else
echo "Failed to copy the [$(scriptName)] script to the [$(containerName)] container in the [$storageAccountName] storage account"
exit -1
fi
# Print data
echo "----------------------------------------------------------------------------------------------"
echo "storageAccountName: $storageAccountName"
echo "containerName: $(containerName)"
echo "##vso[task.setvariable variable=storageAccountResourceGroupName;]$storageAccountResourceGroupName"
echo "##vso[task.setvariable variable=storageAccountName;]$storageAccountName"
echo "##vso[task.setvariable variable=storageAccountKey;]$storageAccountKey"
echo "##vso[task.setvariable variable=ok;]true"
- bash: |
echo "Storage Account Name: $(storageAccountName)"
echo "Storage Account Key: $(storageAccountKey)"
displayName: 'Print Variables'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
- task: AzureCLI@2
displayName: 'Get Latest Kubernetes Version'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
scriptType: bash
scriptLocation: inlineScript
azureSubscription: $(azureSubscription)
addSpnToEnvironment: true
inlineScript: |
version=$(az aks get-versions --location $(location) --query "orchestrators[?isPreview==false].orchestratorVersion | sort(@) | [-1]" --output tsv)
echo "##vso[task.setvariable variable=kubernetesVersion;]$version"
- task: TerraformInstaller@0
displayName: 'Terraform Install'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
terraformVersion: $(terraformVersion)
- task: TerraformTaskV2@2
displayName: 'Terraform Init'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
provider: 'azurerm'
command: 'init'
backendServiceArm: $(azureSubscription)
backendAzureRmResourceGroupName: $(terraformBackendResourceGroupName)
backendAzureRmStorageAccountName: $(terraformBackendStorageAccountName)
backendAzureRmContainerName: '$(terraformBackendContainerName)'
backendAzureRmKey: $(tfstatePath)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: TerraformTaskV2@2
displayName: 'Terraform Plan'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
provider: 'azurerm'
command: 'plan'
commandOptions: '-input=false -var ssh_public_key=$(sshPublicKey) -var location=$(location) -var="script_storage_account_name=$(storageAccountName)" -var="script_storage_account_key=$(storageAccountKey)" -var="container_name=$(containerName)" -var="script_name=$(scriptName)" -var="hub_vnet_name=$(hubVNetName)" -var="aks_vnet_name=$(aksVNetName)" -var="vm_name=$(vmName)" -var kubernetes_version=$(kubernetesVersion) -var resource_group_name=$(resourceGroupName) -var aks_cluster_name=$(aksClusterName) -var log_analytics_workspace_name=$(logAnalyticsWorkspaceName) -var domain_name_label=$(domainNameLabel) -var firewall_name=$(firewallName) -var acr_name=$(acrName) -var bastion_host_name=$(bastionHostName) -var key_vault_name=$(keyVaultName)'
environmentServiceNameAzureRM: $(azureSubscription)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: TerraformTaskV2@2
displayName: 'Terraform Apply'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
provider: 'azurerm'
command: 'apply'
commandOptions: '-input=false -auto-approve -var ssh_public_key=$(sshPublicKey) -var location=$(location) -var="script_storage_account_name=$(storageAccountName)" -var="script_storage_account_key=$(storageAccountKey)" -var="container_name=$(containerName)" -var="script_name=$(scriptName)" -var="hub_vnet_name=$(hubVNetName)" -var="aks_vnet_name=$(aksVNetName)" -var="vm_name=$(vmName)" -var kubernetes_version=$(kubernetesVersion) -var resource_group_name=$(resourceGroupName) -var aks_cluster_name=$(aksClusterName) -var log_analytics_workspace_name=$(logAnalyticsWorkspaceName) -var domain_name_label=$(domainNameLabel) -var firewall_name=$(firewallName) -var acr_name=$(acrName) -var bastion_host_name=$(bastionHostName) -var key_vault_name=$(keyVaultName)'
environmentServiceNameAzureRM: $(azureSubscription)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: AzureCLI@2
displayName: 'Delete Temporary Storage Account'
condition: always()
inputs:
scriptType: bash
scriptLocation: inlineScript
azureSubscription: $(azureSubscription)
addSpnToEnvironment: true
inlineScript: |
az group delete \
--name $(storageAccountResourceGroupName) \
--yes
if [[ $? == 0 ]]; then
echo "Temporary storage account successfully deleted"
else
echo "Failed to delete the temporary storage account"
exit -1
fi