-
Notifications
You must be signed in to change notification settings - Fork 7
/
azure-pipelines.yml
237 lines (218 loc) · 9.43 KB
/
azure-pipelines.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
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- remote_stats
pool:
vmImage: ubuntu-latest
parameters:
- name: deployEnv
displayName: Selecting a Deployment Environment???
type: string
default: 'dev'
values:
- dev
- uat
- prd
variables:
- name: tf_version
value: 'latest'
- name: env_name
${{ if eq(parameters['deployEnv'],'dev') }}:
value: 'dev'
${{elseif eq(parameters['DeployEnv'],'uat') }}:
value: 'uat'
${{elseif eq(parameters['DeployEnv'],'prd') }}:
value: 'prd'
stages:
- stage: script
jobs:
- job: azure_cli_script
steps:
- task: AzureCLI@2
displayName: 'Azure CLI :Create Storage Account,Key Vault And Set KeyVault Secret'
inputs:
azureSubscription: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
# create azure resource group
az group create --location eastasia --name $(terraform_rg)
# create azure storage account
az storage account create --name $(storage_account) --resource-group $(terraform_rg) --location eastasia --sku Standard_LRS
# create storage account container for tf state
az storage container create --name $(storage_account_container) --account-name $(storage_account)
# query storage key and set variable
ACCOUNT_KEY=$(az storage account keys list --resource-group $(terraform_rg) --account-name $(storage_account) --query "[?keyName == 'key1'][value]" --output tsv)
# create azure keyvault
az keyvault create --name $(keyvault) --resource-group $(terraform_rg) --location eastasia --enable-soft-delete false
# set keyvault secret,secret value is ACCOUNT_KEY
az keyvault secret set --name $(keyvault_sc) --vault-name $(keyvault) --value $ACCOUNT_KEY
- task: AzureKeyVault@2
displayName: 'Azure Key Vault :Get Storage Access Secret'
inputs:
azureSubscription: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
KeyVaultName: '$(keyvault)'
SecretsFilter: 'terraform-stste-storage-key'
RunAsPreJob: false
- stage: terraform_validate
jobs:
- job: terraform_validate
steps:
- task: TerraformInstaller@0
inputs:
terraformVersion: ${{variables.tf_version}}
- task: TerraformTaskV2@2
displayName: 'terraform init'
inputs:
provider: 'azurerm'
command: 'init'
# commandOptions: '-backend-config="access_key=$(terraform-stste-storage-key)"'
backendServiceArm: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
backendAzureRmResourceGroupName: $(terraform_rg)
backendAzureRmStorageAccountName: $(storage_account)
backendAzureRmContainerName: $(storage_account_container)
backendAzureRmKey: $(container_key)
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- task: TerraformTaskV2@2
inputs:
provider: 'azurerm'
command: 'validate'
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- stage: terraform_plan
dependsOn: [terraform_validate]
condition: succeeded('terraform_validate')
jobs:
- job: terraform_plan
steps:
- task: TerraformInstaller@0
inputs:
terraformVersion: ${{ variables.tf_version }}
- task: TerraformTaskV2@2
displayName: 'terraform init'
inputs:
provider: 'azurerm'
command: 'init'
# commandOptions: '-backend-config="access_key=$(terraform-stste-storage-key)"'
backendServiceArm: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
backendAzureRmResourceGroupName: $(terraform_rg)
backendAzureRmStorageAccountName: $(storage_account)
backendAzureRmContainerName: $(storage_account_container)
backendAzureRmKey: $(container_key)
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- task: TerraformTaskV2@2
inputs:
provider: 'azurerm'
command: 'plan'
environmentServiceNameAzureRM: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- stage: terraform_apply
dependsOn: [terraform_plan]
condition: succeeded('terraform_plan')
jobs:
- deployment: terraform_apply
continueOnError: false
environment: 'Approve_Production'
timeoutInMinutes: 120
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: TerraformInstaller@0
inputs:
terraformVersion: ${{ variables.tf_version }}
- task: TerraformTaskV2@2
displayName: 'terraform init'
inputs:
provider: 'azurerm'
command: 'init'
# commandOptions: '-backend-config="access_key=$(terraform-stste-storage-key)"'
backendServiceArm: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
backendAzureRmResourceGroupName: $(terraform_rg)
backendAzureRmStorageAccountName: $(storage_account)
backendAzureRmContainerName: $(storage_account_container)
backendAzureRmKey: $(container_key)
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- task: TerraformTaskV2@2
inputs:
provider: 'azurerm'
command: 'plan'
environmentServiceNameAzureRM: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- task: TerraformTaskV2@2
inputs:
provider: 'azurerm'
command: 'apply'
commandOptions: '-auto-approve'
environmentServiceNameAzureRM: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
# - stage: terraform_apply
# dependsOn: [terraform_plan]
# condition: succeeded('terraform_plan')
# jobs:
# - job: terraform_apply
# steps:
# - task: TerraformInstaller@0
# inputs:
# terraformVersion: ${{ variables.tf_version }}
# - task: TerraformTaskV2@2
# displayName: 'terraform init'
# inputs:
# provider: 'azurerm'
# command: 'init'
# # commandOptions: '-backend-config="access_key=$(terraform-stste-storage-key)"'
# backendServiceArm: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
# backendAzureRmResourceGroupName: $(terraform_rg)
# backendAzureRmStorageAccountName: $(storage_account)
# backendAzureRmContainerName: $(storage_account_container)
# backendAzureRmKey: $(container_key)
# workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
# - task: TerraformTaskV2@2
# inputs:
# provider: 'azurerm'
# command: 'plan'
# environmentServiceNameAzureRM: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
# workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
# - task: TerraformTaskV2@2
# inputs:
# provider: 'azurerm'
# command: 'apply'
# commandOptions: '-auto-approve'
# environmentServiceNameAzureRM: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
# workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- stage: terraform_destroy
dependsOn: [terraform_apply]
condition: succeeded('terraform_apply')
jobs:
- job: terraform_destroy
steps:
- task: TerraformInstaller@0
inputs:
terraformVersion: ${{ variables.tf_version }}
- task: TerraformTaskV2@2
displayName: 'terraform init'
inputs:
provider: 'azurerm'
command: 'init'
# commandOptions: '-backend-config="access_key=$(terraform-stste-storage-key)"'
backendServiceArm: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
backendAzureRmResourceGroupName: $(terraform_rg)
backendAzureRmStorageAccountName: $(storage_account)
backendAzureRmContainerName: $(storage_account_container)
backendAzureRmKey: $(container_key)
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- task: TerraformTaskV2@2
inputs:
provider: 'azurerm'
command: 'plan'
environmentServiceNameAzureRM: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'
- task: TerraformTaskV2@2
inputs:
provider: 'azurerm'
command: 'destroy'
commandOptions: '-auto-approve'
environmentServiceNameAzureRM: 'Microsoft Azure MVP(8558b627-87ea-4575-858d-c3c6722dbe38)'
workingDirectory: '$(System.DefaultWorkingDirectory)/src/model/'