-
Notifications
You must be signed in to change notification settings - Fork 75
/
azure-pipelines.yml
275 lines (243 loc) · 9.98 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
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
# Terraform release pipeline
variables:
# Directory of terraform files in repo
tf_directory: 'lecture7'
terraform_version: '0.14.7'
tflint_version: '0.25.0'
# Create this environment and set approval on it to the environment owners group
tf_approval_environment: 'TerraformApprovalEnv'
tf_init_command_options: '-input=false'
tf_validate_command_options: ''
tf_refresh_command_options: '-input=false -lock=false'
tf_plan_command_options: '-input=false -lock=false -refresh=false -out plan.out'
tf_apply_command_options: '-auto-approve -input=false -refresh=false'
# Set the pool
pool:
vmImage: 'ubuntu-latest'
# Automatically trigger pipeline when master branch updated (PR merged) in $(tf_directory) path
trigger:
branches:
include:
- master
paths:
include:
- $(tf_directory)
# Release instructions
stages:
- stage: Terraform_Plan
jobs:
- deployment: Terraform_Plan
displayName: Terraform_Plan
workspace:
clean: all
continueOnError: 'false'
environment: 'TerraformPlan_Environment'
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: Bash@3
displayName: 'Install pre-reqs'
inputs:
targetType: 'inline'
workingDirectory: $(System.DefaultWorkingDirectory)/lecture7
failOnStderr: false
script: |
sudo yum -y install git
wget https://github.com/terraform-linters/tflint/releases/download/v$(tflint_version)/tflint_linux_amd64.zip
unzip tflint_linux_amd64.zip
sudo mv tflint /usr/local/bin
wget https://releases.hashicorp.com/terraform/$(terraform_version)/terraform_$(terraform_version)_linux_amd64.zip
unzip terraform_$(terraform_version)_linux_amd64.zip
sudo mv terraform /usr/local/bin/
- task: Bash@3
displayName: 'Terraform Init'
inputs:
targetType: 'inline'
script: 'terraform init -backend-config="access_key=$(STORAGEKEY)" $(tf_init_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'TFLint Validation'
inputs:
targetType: 'inline'
script: 'tflint --module'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'Terraform Validation'
inputs:
targetType: 'inline'
script: 'terraform validate $(tf_validate_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'Terraform Refresh'
inputs:
targetType: 'inline'
script: 'terraform refresh $(tf_refresh_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'Terraform Plan'
inputs:
targetType: 'inline'
script: 'terraform plan $(tf_plan_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
name: AutoApprovalTest
displayName: Auto-Approval Test
inputs:
targetType: 'inline'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
script: |
# If no changes, no-op and don't continue
if terraform show plan.out | grep -q " 0 to add, 0 to change, 0 to destroy"; then
echo "##[section]No changes, terraform apply will not run";
# Check if resources destroyed. If no, don't require approval
elif terraform show plan.out | grep -q "to change, 0 to destroy"; then
echo "##[section]Approval not required";
echo "##[section]Automatic terraform apply triggered";
echo "##vso[task.setvariable variable=approvalRequired;isOutput=true]false"
# Check if resources destroyed. If yes, require approvals
else
echo "##[section]Terraform apply requires manual approval";
echo "##vso[task.setvariable variable=approvalRequired;isOutput=true]true"
fi
- task: DeleteFiles@1
displayName: 'Clean up working dir'
# Run directory cleanup always
condition: always()
inputs:
SourceFolder: '$(Agent.BuildDirectory)'
Contents: '*'
- stage: Auto_Terraform
dependsOn:
- Terraform_Plan # Explicitly set dependency graph
# Conditions
# 1. If TF Plan succeeded
# 2. If build reason isn't a pull request
# 3. If environment owner approval isn't required
condition: |
and
(
succeeded(),
ne(variables['Build.Reason'], 'PullRequest'),
eq(dependencies.Terraform_Plan.outputs['Terraform_Plan.Terraform_Plan.AutoApprovalTest.approvalRequired'], 'false')
)
jobs:
- deployment: Auto_Terraform
displayName: Auto Terraform
environment: 'TerraformAutoApprove_Environment'
workspace:
clean: all
continueOnError: 'false'
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: Bash@3
displayName: 'Install pre-reqs'
inputs:
targetType: 'inline'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: false
script: |
wget https://releases.hashicorp.com/terraform/$(terraform_version)/terraform_$(terraform_version)_linux_amd64.zip
unzip terraform_$(terraform_version)_linux_amd64.zip
sudo mv terraform /usr/local/bin/
- task: Bash@3
displayName: 'Terraform Init'
inputs:
targetType: 'inline'
script: 'terraform init -backend-config="access_key=$(STORAGEKEY)" $(tf_init_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'Terraform Refresh'
inputs:
targetType: 'inline'
script: 'terraform refresh $(tf_refresh_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'Terraform Apply'
inputs:
targetType: 'inline'
script: 'terraform apply $(tf_apply_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: DeleteFiles@1
displayName: 'Clean up working dir'
# Run directory cleanup always
condition: always()
inputs:
SourceFolder: '$(Agent.BuildDirectory)'
Contents: '*'
- stage: Approved_Terraform
dependsOn: Terraform_Plan # Explicitly set dependency graph
# Conditions
# 1. If TF Plan succeeded
# 2. If build reason isn't a pull request
# 3. If environment owner approval *is* required
condition: |
and
(
succeeded(),
ne(variables['Build.Reason'], 'PullRequest'),
eq(dependencies.Terraform_Plan.outputs['Terraform_Plan.Terraform_Plan.AutoApprovalTest.approvalRequired'], 'true')
)
jobs:
- deployment: Approved_Terraform
displayName: Approved Terraform
workspace:
clean: all
continueOnError: 'false'
environment: dev
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: Bash@3
displayName: 'Install pre-reqs'
inputs:
targetType: 'inline'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: false
script: |
wget https://releases.hashicorp.com/terraform/$(terraform_version)/terraform_$(terraform_version)_linux_amd64.zip
unzip terraform_$(terraform_version)_linux_amd64.zip
sudo mv terraform /usr/local/bin/
- task: Bash@3
displayName: 'Terraform Init'
inputs:
targetType: 'inline'
script: 'terraform init -backend-config="access_key=$(STORAGEKEY)" $(tf_init_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'Terraform Refresh'
inputs:
targetType: 'inline'
script: 'terraform refresh $(tf_refresh_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: Bash@3
displayName: 'Terraform Apply'
inputs:
targetType: 'inline'
script: 'terraform apply $(tf_apply_command_options)'
workingDirectory: $(System.DefaultWorkingDirectory)/$(tf_directory)
failOnStderr: true
- task: DeleteFiles@1
displayName: 'Clean up working dir'
# Run directory cleanup always
condition: always()
inputs:
SourceFolder: '$(Agent.BuildDirectory)'
Contents: '*'