generated from segraef/Template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jobs.solution.deploy.yml
299 lines (268 loc) · 17.9 KB
/
jobs.solution.deploy.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
#########################################################
## DEPLOYMENT PIPELINE ##
#########################################################
##
## This pipeline template contains the logic to deploy a given module's ARM template using the provided parameter file(s)
##
## Enabled levels of deployment
## - Resource-Group-Level
## - Subscription-Level
## - Management-Group-Level
## - Tenant-Level
##
########################################################
##
##---------------------------------------------##
## TEMPLATE PARAMETERS ##
##---------------------------------------------##
##
## By default it uses the variables specified in the below [parameters] section. However, you can overwrite these variables in the
## referencing pipeline by providing the parameter explicitly.
##
## NOTE: If you don't need to overwrite a shared value, you can IGNORE this section
##
## |============================================================================================================================================================================================================================================|
## | Parameter | Default Value | Description | Example |
## |---------------------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
## | serviceConnection | '$(serviceConnection)' | The service connection that connects to Azure. | 'demo-internal' |
## | poolName | '$(poolName)' | You can provide either a [poolname] or [vmImage] to run the job on. | 'Custom Deployment Pool' |
## | vmImage | '$(vmImage)' | You can provide either a [poolname] or [vmImage] to run the job on. | 'ubuntu20.04' |
## | defaultJobTimeoutInMinutes | 120 | The timeout for the job in this pipeline. | 120 |
## | removeDeployment | 'true' | Set to [true] to flag resources for removal. If not provided, defaults to true. | 'true' |
## | templateFilePath | '' | Path to the template file to deploy. | 'arm/Microsoft.AnalysisServices/servers/deploy.bicep' |
## | customParameterFileTokens | '' | | |
## | jobDisplayName | '' | The display name of the job. | 'Deploy module' |
## | modulePath | '$(modulePath)' | The path to the module to deploy. | 'c:/KeyVault' |
## | location | '$(location)' | The location to deploy resources to. | 'EastUs2' |
## | resourceGroupName | '$(resourceGroupName)' | The resourcegroup to deploy into. Required only for Resource-Group-Level deployments. | 'validation-rg' |
## | subscriptionId | '$(ARM_SUBSCRIPTION_ID)' | The id of the subscription to deploy into when using a Management group service connection. | 'aed7c000-6387-412e-bed0-24dfddf4bbc6' |
## | managementGroupId | '$(ARM_MGMTGROUP_ID)' | The id of the management group to deploy into. Required only for Management-Group-Level deployments. | '6ycc9620-cb01-454f-9ebc-fc6b1df48d64' |
## | azurePowerShellVersion | '$(azurePowerShellVersion)' | Used for configuring the Azure PowerShellModules Version, one of the example values. | 'latestVersion' or 'OtherVersion' |
## | preferredAzurePowerShellVersion | '$(preferredAzurePowerShellVersion)' | Used for configuring the Azure PowerShellModules Version, either an empty string or the specific version. | '4.4.0' |
## |============================================================================================================================================================================================================================================|
##
##---------------------------------------------##
parameters:
# Pipeline-related parameters
jobName: ''
displayName: ''
moduleName: ''
moduleVersion: '$(moduleVersion)'
artifactFeedPath: '$(artifactFeedPath)'
checkoutRepositories: ''
environment: 'DEV'
serviceConnection: '$(serviceConnection)'
poolName: '$(poolName)'
vmImage: '$(vmImage)'
defaultJobTimeoutInMinutes: 120
whatif: false
# Logic-related parameters
removeDeployment: false
templateFilePath: ''
moduleTestFilePath: ''
customParameterFileTokens: ''
modulePath: '$(modulePath)'
location: '$(location)'
resourceGroupName: '$(resourceGroupName)'
subscriptionId: '$(subscriptionId)'
managementGroupId: '$(managementGroupId)'
# Azure PowerShell Version parameters
azurePowerShellVersion: '$(azurePowerShellVersion)'
preferredAzurePowerShellVersion: '$(preferredAzurePowerShellVersion)'
##---------------------------------------------##
## TEMPLATE LOGIC ##
##---------------------------------------------##
jobs:
- deployment: ${{ parameters.jobName }}${{ parameters.whatif }}
${{ if eq( parameters.whatif, true) }}:
displayName: ${{ parameters.displayName }} WhatIf
${{ if ne( parameters.whatif, true) }}:
displayName: ${{ parameters.displayName }}
${{ if ne( parameters.dependsOn, '') }}:
dependsOn:
- ${{ each dependency in parameters.dependsOn }}:
- ${{ dependency }}${{ parameters.whatif }}
environment: ${{ parameters.environment }}
timeoutInMinutes: ${{ parameters.defaultJobTimeoutInMinutes }}
pool:
${{ if ne(parameters.vmImage, '') }}:
vmImage: ${{ parameters.vmImage }}
${{ if ne(parameters.poolName, '') }}:
name: ${{ parameters.poolName }}
strategy:
runOnce:
deploy:
##---------------------------------------------##
## TEMPLATE LOGIC ##
##---------------------------------------------##
steps:
# [Environment Variables] task(s)
#--------------------------------
- pwsh: |
$modulePath = Join-Path '$(System.DefaultWorkingDirectory)' '$(environmentPath)' '${{ parameters.modulePath }}'
$sourceDirectory = '$(System.DefaultWorkingDirectory)'
Write-Output "##vso[task.setvariable variable=ENVMODULEPATH]$modulePath"
Write-Output "##vso[task.setvariable variable=ENVSOURCEDIRECTORY]$sourceDirectory"
# [Checkout Repositories] task(s)
#--------------------------------
- checkout: self
- ${{ if ne(parameters.checkoutRepositories, '') }}:
- ${{ each checkoutRepository in parameters.checkoutRepositories }}:
- checkout: ${{ checkoutRepository }}
fetchDepth: 1 # the depth of commits to ask Git to fetch; if not set defaults to no limit
path: 's/${{ checkoutRepository }}'
- ${{ each checkoutRepository in parameters.checkoutRepositories }}:
# [Multi Repo] Support task
#--------------------------
- task: PowerShell@2
displayName: Handle Multi-Repo Invocation
inputs:
targetType: inline
pwsh: true
script: |
# ---------------------------- #
# HANDLE MULTI-REPO INVOCATION #
# ---------------------------- #
# Handle multiple-repositories
Write-Verbose "Multi-Repo Checkout" -Verbose
$sourceDirectory = '$(Build.Repository.Name)'
$sourceDirectory = $sourceDirectory.Split('/')[-1]
$modulePath = Join-Path '$(System.DefaultWorkingDirectory)' ${{ checkoutRepository }} '${{ parameters.modulePath }}'
Write-Verbose "modulePath: $modulePath" -Verbose
Write-Verbose "sourceDirectory: $sourceDirectory" -Verbose
Write-Output "##vso[task.setvariable variable=ENVMODULEPATH]$modulePath"
Write-Output "##vso[task.setvariable variable=ENVSOURCEDIRECTORY]$sourceDirectory"
# [Download Azure Artifacts] task(s)
#---------------------
- ${{ if ne(parameters.moduleName, '') }}:
- pwsh : |
$lowerModuleName = "${{ parameters.moduleName }}".ToLower()
Write-Host "##vso[task.setVariable variable=lowerModuleName]$lowerModuleName"
$modulePath = Join-Path '$(downloadDirectory)/${{ parameters.moduleName }}' 'deploy.json'
Write-Output "##vso[task.setvariable variable=ENVMODULEPATH]$modulePath"
displayName: 'Prepare download from artifacts feed'
- task: UniversalPackages@0
displayName: 'Download module [${{ parameters.moduleName }}] version [${{ parameters.moduleVersion }}] from feed [${{ parameters.artifactFeedPath }}]'
inputs:
command: download
vstsFeed: '${{ parameters.artifactFeedPath }}'
vstsFeedPackage: '$(lowerModuleName)'
vstsPackageVersion: '${{ parameters.moduleVersion }}'
downloadDirectory: '$(downloadDirectory)/$(lowerModuleName)'
# [Validation] task(s)
#---------------------
- ${{ if eq( parameters.whatif, true) }}:
- task: AzurePowerShell@5
displayName: 'Validate template file via connection [${{ parameters.serviceConnection }}]'
inputs:
azureSubscription: ${{ parameters.serviceConnection }}
azurePowerShellVersion: 'latestVersion'
preferredAzurePowerShellVersion: ''
ScriptType: InlineScript
pwsh: true
inline: |
# Load used functions
. (Join-Path '$(ENVSOURCEDIRECTORY)' '$(pipelineFunctionsPath)' 'resourceDeployment' 'Test-TemplateDeployment.ps1')
# Fetching parameters
$location = '${{ parameters.location }}'
$resourceGroupName = '${{ parameters.resourceGroupName }}'
$subscriptionId = '${{ parameters.subscriptionId }}'
$managementGroupId = '${{ parameters.managementGroupId }}'
$moduleTestFilePath = Join-Path '$(ENVSOURCEDIRECTORY)' '$(environmentPath)' '${{ parameters.moduleTestFilePath }}'
# ----------- #
# INVOKE TEST #
# ----------- #
# Building input object
$functionInput = @{
templateFilePath = '$(ENVMODULEPATH)'
parameterFilePath = $moduleTestFilePath
location = $location
resourceGroupName = $resourceGroupName
subscriptionId = $subscriptionId
managementGroupId = $managementGroupId
additionalParameters = @{}
}
Write-Verbose "Invoke task with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
Test-TemplateDeployment @functionInput -Verbose
# [Deployment] task(s)
#---------------------
- ${{ if ne( parameters.whatif, true) }}:
- task: AzurePowerShell@5
name: deployModule
displayName: 'Deploy template file via connection [${{ parameters.serviceConnection }}]'
inputs:
azureSubscription: ${{ parameters.serviceConnection }}
azurePowerShellVersion: 'latestVersion'
preferredAzurePowerShellVersion: ''
pwsh: true
ScriptType: InlineScript
inline: |
# Load used functions
. (Join-Path '$(ENVSOURCEDIRECTORY)' '$(pipelineFunctionsPath)' 'resourceDeployment' 'New-TemplateDeployment.ps1')
$location = '${{ parameters.location }}'
$resourceGroupName = '${{ parameters.resourceGroupName }}'
$subscriptionId = '${{ parameters.subscriptionId }}'
$managementGroupId = '${{ parameters.managementGroupId }}'
$moduleTestFilePath = Join-Path '$(ENVSOURCEDIRECTORY)' '$(environmentPath)' '${{ parameters.moduleTestFilePath }}'
# ----------- #
# INVOKE TEST #
# ----------- #
# Building input object
$functionInput = @{
templateFilePath = '$(ENVMODULEPATH)'
parameterFilePath = $moduleTestFilePath
location = $location
resourceGroupName = $resourceGroupName
subscriptionId = $subscriptionId
managementGroupId = $managementGroupId
additionalParameters = @{}
}
Write-Verbose 'Invoke task with' -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
# Invoke deployment
$res = New-TemplateDeployment @functionInput -Verbose
# Get deployment name
$deploymentName = $res.deploymentName
Write-Verbose "Deployment name: $deploymentName" -Verbose
Write-Host "##vso[task.setvariable variable=deploymentName]$deploymentName"
Write-Host "##vso[task.setvariable variable=deploymentName;isOutput=true]$deploymentName"
# Populate further outputs
$deploymentOutputHash=@{}
foreach ($outputKey in $res.deploymentOutput.Keys) {
Write-Output ('##vso[task.setvariable variable={0}]{1}' -f $outputKey, $res.deploymentOutput[$outputKey].Value)
$deploymentOutputHash.add($outputKey,$res.deploymentOutput[$outputKey].Value)
}
$deploymentOutput = $deploymentOutputHash | ConvertTo-Json -Compress -Depth 100
Write-Verbose "Deployment output: $deploymentOutput" -Verbose
Write-Output "##vso[task.setvariable variable=deploymentOutput;isOutput=true]$deploymentOutput"
if ($res.ContainsKey('exception')) {
# Happens only if there is an exception
throw $res.exception
}
# [Removal] task(s)
#------------------
- task: AzurePowerShell@5
displayName: 'Remove deployed resources via [${{ parameters.serviceConnection }}]'
condition: and(succeededOrFailed(), eq('${{ parameters.removeDeployment }}', 'True'), not(eq(variables['deploymentName'],'')), not(startsWith(variables['deploymentName'], 'variables[' )))
inputs:
azureSubscription: ${{ parameters.serviceConnection }}
azurePowerShellVersion: ${{ parameters.azurePowerShellVersion }}
preferredAzurePowerShellVersion: ${{ parameters.preferredAzurePowerShellVersion }}
ScriptType: InlineScript
failOnStandardError: false
pwsh: true
inline: |
# Load used function
. (Join-Path '$(ENVSOURCEDIRECTORY)' '$(pipelineFunctionsPath)' 'resourceRemoval' 'Initialize-DeploymentRemoval.ps1')
$functionInput = @{
TemplateFilePath = '$(ENVMODULEPATH)'
ResourceGroupName = '${{ parameters.resourceGroupName }}'
subscriptionId = '${{ parameters.subscriptionId }}'
ManagementGroupId = '${{ parameters.managementGroupId }}'
deploymentName = '$(deploymentName)'
Verbose = $true
}
Write-Verbose 'Invoke task with' -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
Initialize-DeploymentRemoval @functionInput