-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.deploy_pipeline.yml
194 lines (190 loc) · 9.41 KB
/
.deploy_pipeline.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
name: Terraform-Deploy-Production
trigger:
branches:
exclude:
- "*"
variables:
- name: appDeploy
value: false
- name: projectURI
value: [ADO_PROJECT_URI]
- name: serviceConnection
value: [SERVICE_CONNECTION_NAME]
- name: skipApply
value: false
- name: subscriptionId
value: [TARGET_SUBSCRIPTION]
- name: tfplanfilename
value: $(Build.Repository.Name)_$(Build.SourceBranchName).tfplan
- name: tfvariables
value: " -var 'uiappid=[UID_APP_ID]' -var 'engineappid=[ENGINE_APP_ID]' -var 'engineappsecret=[ENGINE_APP_SECRET]'"
- name: validateNotifyUsers
value: [VALIDATE_NOTIFICATION_TEAM]
- name: workingDirectory
value: ""
pool:
vmImage: ubuntu-latest
stages:
- stage: Plan
displayName: Terraform Plan
jobs:
- job: plan
steps:
- checkout: self
persistCredentials: true
- task: AzureCLI@2
displayName: "Az Login"
name: azlogin
inputs:
azureSubscription: ${{ variables.serviceConnection }}
addSpnToEnvironment: true
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
# Terraform Environment Variables
Write-Host "##vso[task.setvariable variable=ARM_CLIENT_ID;issecret=true]$env:servicePrincipalId"
Write-Host "##vso[task.setvariable variable=ARM_CLIENT_SECRET;issecret=true]$env:servicePrincipalKey"
Write-Host "##vso[task.setvariable variable=ARM_TENANT_ID;issecret=true]$env:tenantId"
workingDirectory: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}"
- task: AzureCli@2
displayName: "Terraform Initialise"
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: "pscore"
addSpnToEnvironment: true
scriptLocation: inlineScript
inlineScript: |
az login --service-principal --username "$(ARM_CLIENT_ID)" --password "$(ARM_CLIENT_SECRET)" --tenant "$(ARM_TENANT_ID)"
$env:ARM_CLIENT_SECRET = "$(ARM_CLIENT_SECRET)"
$env:ARM_CLIENT_ID = "$(ARM_CLIENT_ID)"
$env:ARM_TENANT_ID = "$(ARM_TENANT_ID)"
# Tf uses local git to download modules from git repos.
# As AzDO git repos need an access token to auth, we put the pipelines access token into git config.
$accessToken = az account get-access-token --resource '499b84ac-1321-427f-aa17-267ca6975798' --query 'accessToken' --output tsv
git config --global http.${{ variables.projectURI }}.extraHeader "AUTHORIZATION: bearer $accessToken"
terraform init${{ variables.tfstate }}
# On private agents, the git config is not deleted automatically. Therefore deleting the access token manually here.
git config --global --unset-all http.${{ variables.projectURI }}.extraHeader
workingDirectory: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}"
env:
ARM_SUBSCRIPTION_ID: ${{ variables.subscriptionId }}
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
- task: AzureCli@2
displayName: "Terraform Validate"
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: "pscore"
addSpnToEnvironment: true
scriptLocation: inlineScript
inlineScript: |
terraform validate
workingDirectory: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}"
env:
ARM_SUBSCRIPTION_ID: ${{ variables.subscriptionId }}
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
- task: AzureCli@2
displayName: "Terraform Plan"
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: "pscore"
addSpnToEnvironment: true
scriptLocation: inlineScript
inlineScript: |
az login --service-principal --username "$(ARM_CLIENT_ID)" --password "$(ARM_CLIENT_SECRET)" --tenant "$(ARM_TENANT_ID)"
$env:ARM_CLIENT_SECRET = "$(ARM_CLIENT_SECRET)"
$env:ARM_CLIENT_ID = "$(ARM_CLIENT_ID)"
$env:ARM_TENANT_ID = "$(ARM_TENANT_ID)"
terraform plan -out='${{ variables.tfplanfilename }}'${{ variables.tfvariables }}
workingDirectory: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}"
env:
ARM_SUBSCRIPTION_ID: ${{ variables.subscriptionId }}
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
- task: PublishBuildArtifacts@1
displayName: "Publish Artifact - TFPlan"
condition: succeeded()
inputs:
PathtoPublish: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}/${{ variables.tfplanfilename }}"
ArtifactName: "TerraformPlan"
publishLocation: "Container"
- stage: Validate
displayName: Validate Resources
condition: and(eq(variables['Build.SourceBranch'], 'refs/heads/main'), succeeded())
jobs:
- job: validate
pool: server
steps:
- task: ManualValidation@0
timeoutInMinutes: 1440 # task times out in 1 day
inputs:
notifyUsers: ${{ variables.validateNotifyUsers }}
instructions: "Please validate the terraform environment and resume for deletion."
onTimeout: ${{ variables.timoutEffect }}
- stage: Apply
displayName: Terraform Apply
condition: and(eq(variables['Build.SourceBranch'], 'refs/heads/main'), eq(${{ variables.skipApply }}, false), succeeded())
jobs:
- job: apply
steps:
- checkout: self
persistCredentials: true
- task: AzureCLI@2
displayName: "Az Login"
name: azlogin
inputs:
azureSubscription: ${{ variables.serviceConnection }}
addSpnToEnvironment: true
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
# Terraform Environment Variables
Write-Host "##vso[task.setvariable variable=ARM_CLIENT_ID;issecret=true]$env:servicePrincipalId"
Write-Host "##vso[task.setvariable variable=ARM_CLIENT_SECRET;issecret=true]$env:servicePrincipalKey"
Write-Host "##vso[task.setvariable variable=ARM_TENANT_ID;issecret=true]$env:tenantId"
workingDirectory: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}"
- task: AzureCli@2
displayName: "Terraform Initialise"
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: "pscore"
addSpnToEnvironment: true
scriptLocation: inlineScript
inlineScript: |
az login --service-principal --username "$(ARM_CLIENT_ID)" --password "$(ARM_CLIENT_SECRET)" --tenant "$(ARM_TENANT_ID)"
$env:ARM_CLIENT_SECRET = "$(ARM_CLIENT_SECRET)"
$env:ARM_CLIENT_ID = "$(ARM_CLIENT_ID)"
$env:ARM_TENANT_ID = "$(ARM_TENANT_ID)"
# Tf uses local git to download modules from git repos.
# As AzDO git repos need an access token to auth, we put the pipelines access token into git config.
$accessToken = az account get-access-token --resource '499b84ac-1321-427f-aa17-267ca6975798' --query 'accessToken' --output tsv
git config --global http.${{ variables.projectURI }}.extraHeader "AUTHORIZATION: bearer $accessToken"
terraform init${{ variables.tfstate }}
# On private agents, the git config is not deleted automatically. Therefore deleting the access token manually here.
git config --global --unset-all http.${{ variables.projectURI }}.extraHeader
workingDirectory: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}"
env:
ARM_SUBSCRIPTION_ID: ${{ variables.subscriptionId }}
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
- task: DownloadBuildArtifacts@1
displayName: "Download Artifact - TFPlan"
inputs:
buildType: "current"
downloadType: "single"
artifactName: "TerraformPlan"
downloadPath: "$(Build.SourcesDirectory)"
- task: AzureCli@2
displayName: "Terraform Apply"
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: "pscore"
addSpnToEnvironment: true
scriptLocation: inlineScript
inlineScript: |
az login --service-principal --username "$(ARM_CLIENT_ID)" --password "$(ARM_CLIENT_SECRET)" --tenant "$(ARM_TENANT_ID)"
$env:ARM_CLIENT_SECRET = "$(ARM_CLIENT_SECRET)"
$env:ARM_CLIENT_ID = "$(ARM_CLIENT_ID)"
$env:ARM_TENANT_ID = "$(ARM_TENANT_ID)"
terraform apply -input=false -auto-approve${{ variables.tfvariables }}
workingDirectory: "$(Build.SourcesDirectory)${{ variables.workingDirectory }}"
env:
ARM_SUBSCRIPTION_ID: ${{ variables.subscriptionId }}
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)