-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathBest-Practice.yml
231 lines (205 loc) · 9.5 KB
/
Best-Practice.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
#Multi-stage YAML pipeline demo.
name: $(BuildDefinitionName).$(DayOfYear)$(Rev:.r)
schedules:
- cron: "45 5 * * 4"
branches:
include:
- master
displayName: Weekly Thursday 5h45 am UTC build
always: true
variables:
- group: terraform_binary # variable group containing Terraform information like the Terraform version (like terraform_version)
- name: vmImageName
value: "ubuntu-latest"
- name: backend_main_secret_file_id1 # secret file used by the following cmdlet Terraform init, plan, apply and destroy
value: "backend-main-jdld-1.json"
- name: main_secret_file_id1 # secret file used by the following cmdlet Terraform init, plan, apply and destroy
value: "main-jdld-1.json"
- name: artifact_name
value: "Best-Practice"
resources:
repositories:
- repository: Yaml_Templates # identifier (A-Z, a-z, 0-9, and underscore)
type: github
endpoint: JamesDLD # name of the service connection to use (for non-Azure Repos types)
name: JamesDLD/terraform
#ref: refs/tags/0.0.1 # ref name to use, defaults to 'refs/heads/master'
trigger:
batch: true # when a build is running, the system waits until the build is completed
branches:
include:
- master
- feature/*
- release/*
paths:
include:
- Best-Practice/*
stages:
- stage: Build
jobs:
- job: Terraform_Plan
displayName: Terraform Plan - Publish a package if Infrastructure changes are identified
continueOnError: false
pool:
vmImage: $(vmImageName)
steps:
- task: DownloadSecureFile@1
displayName: "Download secure file $(backend_main_secret_file_id1)"
inputs:
secureFile: $(backend_main_secret_file_id1)
- task: DownloadSecureFile@1
displayName: "Download secure file $(main_secret_file_id1)"
inputs:
secureFile: $(main_secret_file_id1)
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
path: "./Best-Practice/BestPractice-1/"
package_name: "BestPractice-1"
terraform_init: true
terraform_plan: true
backend_secret_file_id: $(backend_main_secret_file_id1)
main_secret_file_id: $(backend_main_secret_file_id1)
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
path: "./Best-Practice/BestPractice-2/"
package_name: "BestPractice-2"
terraform_init: true
terraform_plan: true
backend_secret_file_id: $(backend_main_secret_file_id1)
main_secret_file_id: $(backend_main_secret_file_id1)
main_file_path: "variable/main-jdld.tfvars"
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
path: "./Best-Practice/BestPractice-3/"
package_name: "BestPractice-3"
terraform_init: true
terraform_plan: true
backend_secret_file_id: $(backend_main_secret_file_id1)
main_secret_file_id: $(main_secret_file_id1)
main_file_path: "variable/main-jdld.tfvars"
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
path: "./Best-Practice/BestPractice-4/"
package_name: "BestPractice-4"
terraform_init: true
terraform_plan: true
backend_secret_file_id: $(backend_main_secret_file_id1)
main_secret_file_id: $(backend_main_secret_file_id1)
main_file_path: "variable/main-jdld.tfvars"
- publish: "./ArtifactPublishLocation" # Local path to include in the Artifact
artifact: "$(artifact_name)"
- stage: Deploy
dependsOn: Build
jobs:
# track deployments on the environment
- deployment: Terraform_Apply
displayName: Terraform Apply - Resources creation
pool:
vmImage: $(vmImageName)
environment: "Terraform_Apply"
strategy:
# default deployment strategy
runOnce:
deploy:
steps:
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-1"
terraform_apply: true
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-2"
terraform_apply: true
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-3"
terraform_apply: true
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-4"
terraform_apply: true
- stage: Deliver
dependsOn: Deploy
jobs:
# track deployments on the environment
- deployment: Terraform_Destroy
displayName: Terraform Destroy - Script ok, now deleting the resources
pool:
vmImage: $(vmImageName)
environment: "Terraform_Destroy"
strategy:
# default deployment strategy
runOnce:
deploy:
steps:
- task: DownloadSecureFile@1
displayName: Download secure file $(backend_main_secret_file_id1)
inputs:
secureFile: $(backend_main_secret_file_id1)
- task: DownloadSecureFile@1
displayName: "Download secure file $(main_secret_file_id1)"
inputs:
secureFile: $(main_secret_file_id1)
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-1"
terraform_destroy: true
main_secret_file_id: $(backend_main_secret_file_id1)
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-2"
terraform_destroy: true
main_secret_file_id: $(backend_main_secret_file_id1)
main_file_path: "variable/main-jdld.tfvars"
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-3"
terraform_destroy: true
main_secret_file_id: $(main_secret_file_id1)
main_file_path: "variable/main-jdld.tfvars"
- template: pipeline/terraform.yml@Yaml_Templates
parameters:
version: $(terraform_version)
artifact_path: $(Pipeline.Workspace)/$(artifact_name)
package_name: "BestPractice-4"
terraform_destroy: true
main_secret_file_id: $(backend_main_secret_file_id1)
main_file_path: "variable/main-jdld.tfvars"
- bash: |
#Variable
backend_secret_file_id_path="$(Agent.TempDirectory)/$(backend_main_secret_file_id1)"
rg_name="infr-jdld-noprd-rg1"
lb_public_name="bp4-outbound-publiclb1"
ip_public_name="bp4-outbound-publiclb1_pip1"
client_id=$(cat $backend_secret_file_id_path | jq -r '.client_id')
client_secret=$(cat $backend_secret_file_id_path | jq -r '.client_secret')
tenant_id=$(cat $backend_secret_file_id_path | jq -r '.tenant_id')
subscription_id=$(cat $backend_secret_file_id_path | jq -r '.subscription_id')
#Action
echo "Connecting to the Azure tenant id"
login=$(az login --service-principal -u $client_id -p $client_secret --tenant $tenant_id)
echo "Selecting the Azure subscription"
az account set --subscription $subscription_id
echo "Deleting the Azure load balancer $lb_public_name located in the resource group $rg_name"
az network lb delete -g $rg_name -n $lb_public_name
echo "Deleting the Azure public ip $ip_public_name located in the resource group $rg_name"
az network public-ip delete -g $rg_name -n $ip_public_name
displayName: BestPractice-4 - AZ CLI Delete Public LB and IP