-
Notifications
You must be signed in to change notification settings - Fork 457
/
jobs.publishModule.yml
434 lines (372 loc) · 24.2 KB
/
jobs.publishModule.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#########################################################
## PUBLISH PIPELINE ##
#########################################################
##
## This pipeline template contains the logic to publish module data as
## - A build artifact and/or
## - As a new version to a given storage account and/or
## - As a new version as an UniversalPackage to a given artifact-feed
##
#########################################################
##---------------------------------------------##
## 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 |
## |---------------------------------|--------------------------------------|---------------------------------------------------------------------------------------------------------|-----------------------------------|
## | displayName | 'Publishing' | Name for the pipeline job | 'Publish KeyVault' |
## | 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 |
## | modulePath | '$(modulePath)' | The path to the module to deploy. E.g. [c:/KeyVault] | 'c:/KeyVault' |
## | publishLatest | '$(publishLatest)' | Flag to indicate whether or not to publish a "latest" version to Bicep Registry and Template Specs | true |
## | useApiSpecsAlignedName | '$(useApiSpecsAlignedName)' | Flag to indicate whether or not to publish module using their REST API, or their folder path name | true |
## | templateSpecsRGName | '$(templateSpecsRGName)' | Required to publish to template spec. ResourceGroup of the template spec to publish to | 'mgmt-rg' |
## | templateSpecsRGLocation | '$(templateSpecsRGLocation)' | Required to publish to template spec. Location of the template spec resource group | 'West Europe' |
## | templateSpecsDescription | '$(templateSpecsDescription)' | Required to publish to template spec. Description of the template spec to publish to | 'IaCs module' |
## | vstsFeedToken | '$(vstsFeedToken)' | Required to publish to a DevOps feed. Token with access to the feed to publish to. | '...' |
## | vstsFeedName | '$(vstsFeedName)' | Required to publish to a DevOps feed. Name to the feed to publish to. | 'modules' |
## | vstsFeedProject | '$(vstsFeedProject)' | Required to publish to a DevOps feed. Name of the project hosting the artifacts feed. May be empty. | 'iacs' |
## | bicepRegistryName | '$(bicepRegistryName)' | Required to publish to the private bicep registry. Name of the hosting container registry | 'adpsxxazacrx001' |
## | bicepRegistryRGName | '$(bicepRegistryRGName)' | Required to publish to the private bicep registry. Resource group of the hosting container registry | 'artifacts-rg' |
## | bicepRegistryRgLocation | '$(bicepRegistryRgLocation)' | Required to publish to the private bicep registry. Location of the RG of the hosting container registry | 'West Europe' |
## | vstsOrganizationUri | '$(vstsOrganizationUri)' | Required to publish to a DevOps feed. Name of the organization hosting the artifacts feed. | 'servicescode' |
## | azurePowerShellVersion | '$(azurePowerShellVersion)' | Used for configuring the Azure PowerShell Version, one of the example values. | 'latestVersion' or 'OtherVersion' |
## | preferredAzurePowerShellVersion | '$(preferredAzurePowerShellVersion)' | Used for configuring the Azure PowerShell Version, either an empty string or specific version. | '4.4.0' |
## |======================================================================================================================================================================================================================|
##
##---------------------------------------------##
parameters:
# Pipeline-related parameters
displayName: 'Publishing'
serviceConnection: '$(serviceConnection)'
poolName: '$(poolName)'
vmImage: '$(vmImage)'
defaultJobTimeoutInMinutes: 120
modulesRepository: '$(modulesRepository)'
subscriptionId: '$(ARM_SUBSCRIPTION_ID)'
# Logic-related parameters
## Module-related
modulePath: '$(modulePath)'
# Shared
publishLatest: '$(publishLatest)'
useApiSpecsAlignedName: '$(useApiSpecsAlignedName)'
## TemplateSpec-related
templateSpecsDoPublish: '$(templateSpecsDoPublish)'
templateSpecsRGName: '$(templateSpecsRGName)'
templateSpecsRGLocation: '$(templateSpecsRGLocation)'
templateSpecsDescription: '$(templateSpecsDescription)'
## Artifact-Feed-related
artifactsFeedDoPublish: '$(artifactsFeedDoPublish)'
vstsOrganizationUri: '$(vstsOrganizationUri)'
vstsFeedProject: '$(vstsFeedProject)'
vstsFeedName: '$(vstsFeedName)'
vstsFeedToken: '$(vstsFeedToken)'
## Private-Bicep-Registry-related
bicepRegistryDoPublish: '$(bicepRegistryDoPublish)'
bicepRegistryName: '$(bicepRegistryName)'
bicepRegistryRGName: '$(bicepRegistryRGName)'
bicepRegistryRgLocation: '$(bicepRegistryRgLocation)'
##---------------------------------------------##
## TEMPLATE LOGIC ##
##---------------------------------------------##
jobs:
- job:
displayName: ${{ parameters.displayName }}
timeoutInMinutes: ${{ parameters.defaultJobTimeoutInMinutes }}
pool:
${{ if ne(parameters.vmImage, '') }}:
vmImage: ${{ parameters.vmImage }}
${{ if ne(parameters.poolName, '') }}:
name: ${{ parameters.poolName }}
steps:
# [Checkout Repositories] task(s)
#--------------------------------
- checkout: self
fetchDepth: 0
# [Agent] Prepare environment
#----------------------------
- task: PowerShell@2
displayName: 'Setup agent'
inputs:
targetType: inline
pwsh: true
script: |
# Load used functions
. (Join-Path '$(System.DefaultWorkingDirectory)' 'utilities' 'pipelines' 'sharedScripts' 'Set-EnvironmentOnAgent.ps1')
# Define PS modules to install on the runner
$modules = @(
@{ Name = 'Az.Accounts' },
@{ Name = 'Az.ContainerRegistry' },
@{ Name = 'Az.Resources' }
)
# Set agent up
Set-EnvironmentOnAgent -PSModules $modules
# [Universal Artifact-feed publish] task(s)
#------------------------------------------
- task: PowerShell@2
displayName: 'Publish module to artifacts feed'
condition: and(
eq(variables['artifactsFeedDoPublish'], true),
succeeded()
)
enabled: true
inputs:
targetType: inline
pwsh: true
script: |
# Load used functions
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Get-ModulesToPublish.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Publish-ModuleToUniversalArtifactsFeed.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Get-ModulesMissingFromUniversalArtifactsFeed.ps1')
#Prioritizing the bicep file
$TemplateFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' 'main.bicep'
if (-not (Test-Path $TemplateFilePath)) {
$TemplateFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' 'main.json'
}
################################
## Get modules to publish ##
################################
$functionInput = @{
TemplateFilePath = $TemplateFilePath
PublishLatest = $false # Not supported by Azure DevOps feeds
}
Write-Verbose "Invoke Get-ModulesToPublish with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
$modulesToPublish = @()
# Get the modified child resources
$modulesToPublish += Get-ModulesToPublish @functionInput -Verbose
#############################
## Get missing modules ##
#############################
# Add all modules that don't exist in the target location
$missingInputObject = @{
TemplateFilePath = $TemplateFilePath
VstsOrganizationUri = '${{ parameters.vstsOrganizationUri }}'
VstsFeedProject = '${{ parameters.vstsFeedProject }}'
VstsFeedName = '${{ parameters.vstsFeedName }}'
UseApiSpecsAlignedName = [System.Convert]::ToBoolean('${{ parameters.useApiSpecsAlignedName }}')
}
Write-Verbose "Invoke Get-ModulesMissingFromUniversalArtifactsFeed with" -Verbose
Write-Verbose ($missingInputObject | ConvertTo-Json | Out-String) -Verbose
$missingModules = Get-ModulesMissingFromUniversalArtifactsFeed @missingInputObject -BearerToken $env:TOKEN
foreach($missingModule in $missingModules) {
if($modulesToPublish.TemplateFilePath -notcontains $missingModule.TemplateFilePath) {
$modulesToPublish += $missingModule
}
}
# Filter modules to publish 'prerelease' only if branch is not main/master
$BranchName = '$(Build.SourceBranch)'
if ($BranchName -ne 'refs/heads/main' -and $BranchName -ne 'refs/heads/master') {
Write-Verbose "Filtering modules to only publish a [prerelease] version as the current branch [$BranchName] is not [main/master]." -Verbose
$modulesToPublish = $modulesToPublish | Where-Object -Property version -like '*-prerelease'
}
#################
## Publish ##
#################
foreach ($moduleToPublish in $modulesToPublish) {
$RelPath = (($moduleToPublish.TemplateFilePath).Split('/modules/')[-1]).Split('/main.')[0]
Write-Host "##[group]$(' - [{0}] [{1}]' -f $RelPath, $moduleToPublish.Version)"
$functionInput = @{
TemplateFilePath = $moduleToPublish.TemplateFilePath
VstsOrganizationUri = '${{ parameters.vstsOrganizationUri }}'
VstsFeedProject = '${{ parameters.vstsFeedProject }}'
VstsFeedName = '${{ parameters.vstsFeedName }}'
ModuleVersion = $moduleToPublish.Version
UseApiSpecsAlignedName = [System.Convert]::ToBoolean('${{ parameters.useApiSpecsAlignedName }}')
}
Write-Verbose "Invoke Publish-ModuleToUniversalArtifactsFeed with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
Publish-ModuleToUniversalArtifactsFeed @functionInput -BearerToken $env:TOKEN -Verbose
Write-Host "##[endgroup]"
}
env:
TOKEN: $(vstsFeedToken)
# [template-spec publish] task(s)
#--------------------------------
- task: AzurePowerShell@5
displayName: 'Publish module to template specs'
condition: and(
eq(variables['templateSpecsDoPublish'], true),
succeeded()
)
enabled: true
inputs:
azureSubscription: '${{ parameters.serviceConnection }}'
azurePowerShellVersion: ${{ parameters.azurePowerShellVersion }}
preferredAzurePowerShellVersion: ${{ parameters.preferredAzurePowerShellVersion }}
pwsh: true
ScriptType: InlineScript
inline: |
# Load PS-Profile configuration
if (Test-Path $profile) {
. $profile
} else {
Write-Warning "No profile loaded from location [$profile]"
}
# Set context to chosen subscription
Write-Verbose ('Setting context to subscription [{0}]' -f '${{ parameters.subscriptionId }}') -Verbose
$null = Set-AzContext -Subscription '${{ parameters.subscriptionId }}'
# Load used functions
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Get-ModulesToPublish.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Get-ModulesMissingFromTemplateSpecsRG.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Publish-ModuleToTemplateSpecsRG.ps1')
#Prioritizing the bicep file
$TemplateFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' 'main.bicep'
if (-not (Test-Path $TemplateFilePath)) {
$TemplateFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' 'main.json'
}
################################
## Get modules to publish ##
################################
$functionInput = @{
TemplateFilePath = $TemplateFilePath
PublishLatest = [System.Convert]::ToBoolean('${{ parameters.publishLatest }}')
}
Write-Verbose "Invoke Get-ModulesToPublish with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
$modulesToPublish = @()
# Get the modified child resources
$modulesToPublish += Get-ModulesToPublish @functionInput -Verbose
#############################
## Get missing modules ##
#############################
# Add all modules that don't exist in the target location
$missingInputObject = @{
TemplateFilePath = $TemplateFilePath
TemplateSpecsRGName = '${{ parameters.templateSpecsRgName }}'
PublishLatest = [System.Convert]::ToBoolean('${{ parameters.publishLatest }}')
UseApiSpecsAlignedName = [System.Convert]::ToBoolean('${{ parameters.useApiSpecsAlignedName }}')
}
Write-Verbose "Invoke Get-ModulesMissingFromTemplateSpecsRG with" -Verbose
Write-Verbose ($missingInputObject | ConvertTo-Json | Out-String) -Verbose
$missingModules = Get-ModulesMissingFromTemplateSpecsRG @missingInputObject
foreach($missingModule in $missingModules) {
if($modulesToPublish.TemplateFilePath -notcontains $missingModule.TemplateFilePath) {
$modulesToPublish += $missingModule
}
}
# Filter modules to publish 'prerelease' only if branch is not main/master
$BranchName = '$(Build.SourceBranch)'
if ($BranchName -ne 'refs/heads/main' -and $BranchName -ne 'refs/heads/master') {
Write-Verbose "Filtering modules to only publish a [prerelease] version as the current branch [$BranchName] is not [main/master]." -Verbose
$modulesToPublish = $modulesToPublish | Where-Object -Property version -like '*-prerelease'
}
#################
## Publish ##
#################
foreach ($moduleToPublish in $modulesToPublish) {
$RelPath = (($moduleToPublish.TemplateFilePath).Split('/modules/')[-1]).Split('/main.')[0]
Write-Host "##[group]$(' - [{0}] [{1}]' -f $RelPath, $moduleToPublish.Version)"
$functionInput = @{
TemplateFilePath = $moduleToPublish.TemplateFilePath
TemplateSpecsRgName = '${{ parameters.templateSpecsRgName }}'
TemplateSpecsRgLocation = '${{ parameters.templateSpecsRgLocation }}'
TemplateSpecsDescription = '${{ parameters.templateSpecsDescription }}'
ModuleVersion = $moduleToPublish.Version
UseApiSpecsAlignedName = [System.Convert]::ToBoolean('${{ parameters.useApiSpecsAlignedName }}')
}
Write-Verbose "Invoke Publish-ModuleToTemplateSpecsRG with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
Publish-ModuleToTemplateSpecsRG @functionInput -Verbose
Write-Host "##[endgroup]"
}
# [private bicep registry publish] task(s)
#-------------------------------------------
- task: AzureCLI@2
displayName: 'Publish module to private bicep registry'
condition: and(
eq(variables['bicepRegistryDoPublish'], true),
succeeded()
)
inputs:
addSpnToEnvironment: true
azureSubscription: '${{ parameters.serviceConnection }}'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
# Load PS-Profile configuration
if (Test-Path $profile) {
. $profile
} else {
Write-Warning "No profile loaded from location [$profile]"
}
# Log into Az-PowerShell context
$SecuredPassword = ConvertTo-SecureString -AsPlainText -String $env:servicePrincipalKey
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $env:servicePrincipalId, $SecuredPassword
$null = Connect-AzAccount -ServicePrincipal -TenantId $env:tenantId -Credential $Credential
# Set context to chosen subscription
Write-Verbose ('Setting context to subscription [{0}]' -f '${{ parameters.subscriptionId }}') -Verbose
$null = Set-AzContext -Subscription '${{ parameters.subscriptionId }}'
# Load used functions
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Get-ModulesToPublish.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Get-ModulesMissingFromPrivateBicepRegistry.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' '$(pipelineFunctionsPath)' 'resourcePublish' 'Publish-ModuleToPrivateBicepRegistry.ps1')
#Prioritizing the bicep file
$TemplateFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' 'main.bicep'
if (-not (Test-Path $TemplateFilePath)) {
$TemplateFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' 'main.json'
}
################################
## Get modules to publish ##
################################
$functionInput = @{
TemplateFilePath = $TemplateFilePath
PublishLatest = [System.Convert]::ToBoolean('${{ parameters.publishLatest }}')
}
Write-Verbose "Invoke Get-ModulesToPublish with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
$modulesToPublish = @()
# Get the modified child resources
$modulesToPublish += Get-ModulesToPublish @functionInput -Verbose
#############################
## Get missing modules ##
#############################
# Add all modules that don't exist in the target location
$missingInputObject = @{
TemplateFilePath = $TemplateFilePath
BicepRegistryName = '${{ parameters.bicepRegistryName }}'
BicepRegistryRgName = '${{ parameters.bicepRegistryRgName }}'
PublishLatest = [System.Convert]::ToBoolean('${{ parameters.publishLatest }}')
UseApiSpecsAlignedName = [System.Convert]::ToBoolean('${{ parameters.useApiSpecsAlignedName }}')
}
Write-Verbose "Invoke Get-ModulesMissingFromPrivateBicepRegistry with" -Verbose
Write-Verbose ($missingInputObject | ConvertTo-Json | Out-String) -Verbose
$missingModules = Get-ModulesMissingFromPrivateBicepRegistry @missingInputObject
foreach($missingModule in $missingModules) {
if($modulesToPublish.TemplateFilePath -notcontains $missingModule.TemplateFilePath) {
$modulesToPublish += $missingModule
}
}
# Filter modules to publish 'prerelease' only if branch is not main/master
$BranchName = '$(Build.SourceBranch)'
if ($BranchName -ne 'refs/heads/main' -and $BranchName -ne 'refs/heads/master') {
Write-Verbose "Filtering modules to only publish a [prerelease] version as the current branch [$BranchName] is not [main/master]." -Verbose
$modulesToPublish = $modulesToPublish | Where-Object -Property version -like '*-prerelease'
}
#################
## Publish ##
#################
foreach ($moduleToPublish in $modulesToPublish) {
$RelPath = (($moduleToPublish.TemplateFilePath).Split('/modules/')[-1]).Split('/main.')[0]
Write-Host "##[group]$(' - [{0}] [{1}]' -f $RelPath, $moduleToPublish.Version)"
$functionInput = @{
TemplateFilePath = $moduleToPublish.TemplateFilePath
BicepRegistryName = '${{ parameters.bicepRegistryName }}'
BicepRegistryRgName = '${{ parameters.bicepRegistryRgName }}'
BicepRegistryRgLocation = '${{ parameters.bicepRegistryRgLocation }}'
ModuleVersion = $moduleToPublish.Version
UseApiSpecsAlignedName = [System.Convert]::ToBoolean('${{ parameters.useApiSpecsAlignedName }}')
}
Write-Verbose "Invoke Publish-ModuleToPrivateBicepRegistry with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
Publish-ModuleToPrivateBicepRegistry @functionInput -Verbose
Write-Host "##[endgroup]"
}