forked from PowerShell/PowerShell-Docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
executable file
·779 lines (668 loc) · 30 KB
/
build.ps1
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#!/usr/bin/pwsh
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Queue Docker image build for a particular image
# ** Expects to have OAuth access in the build**
# ** and the build service has to be granted permission to launch builds**
# The build is expected to have the following parameters:
# - fromTag
# - The tag of the image in the from statement which is being produced
# - imageTag
# - The tag of the produced image
# - PowerShellVersion
# - The version of powershell to put in the image
# - DockerNamespace
# - `public` to build for public consumption.
# - `internal` to build for internal consumption.
[CmdletBinding()]
param(
[Parameter(Mandatory, ParameterSetName="TestByName")]
[Parameter(Mandatory, ParameterSetName="TestAll")]
[switch]
$Test,
[Parameter(ParameterSetName="localBuildByName")]
[Parameter(ParameterSetName="localBuildAll")]
[Parameter(ParameterSetName="TestByName")]
[Parameter(ParameterSetName="TestAll")]
[switch]
$Pull,
[Parameter(Mandatory, ParameterSetName="GenerateMatrixJson")]
[switch]
$GenerateMatrixJson,
[Parameter(ParameterSetName="GenerateMatrixJson")]
[switch]
$FullJson,
[Parameter(Mandatory, ParameterSetName="GenerateManifestLists")]
[switch]
$GenerateManifestLists,
[Parameter(Mandatory, ParameterSetName="GenerateTagsYaml")]
[switch]
$GenerateTagsYaml,
[Parameter(Mandatory, ParameterSetName="localBuildByName")]
[Parameter(Mandatory, ParameterSetName="localBuildAll")]
[switch]
$Build,
[Parameter(ParameterSetName="localBuildByName")]
[Parameter(ParameterSetName="localBuildAll")]
[switch]
$Push,
[Parameter(ParameterSetName="localBuildByName")]
[Parameter(ParameterSetName="localBuildAll")]
[switch]
$SkipTest,
[Parameter(Mandatory, ParameterSetName="GetTagsByName")]
[Parameter(Mandatory, ParameterSetName="GetTagsByChannel")]
[switch]
$GetTags,
[Parameter(Mandatory, ParameterSetName="DupeCheck")]
[switch]
$CheckForDuplicateTags,
[Parameter(Mandatory, ParameterSetName="TestAll")]
[Parameter(Mandatory, ParameterSetName="localBuildAll")]
[Parameter(Mandatory, ParameterSetName="GetTagsByChannel")]
[switch]
$All,
[string]
$ImageName = 'ps.local',
[string]
$Repository = 'powershell',
[string]
$TestLogPostfix,
[switch]
$CI,
[string]
$TagFilter,
[Parameter(ParameterSetName="localBuildByName")]
[Parameter(ParameterSetName="localBuildAll")]
[ValidateScript({([uri]$_).Scheme -eq 'https'})]
[string]
$SasUrl,
[Parameter(ParameterSetName="localBuildByName")]
[Parameter(ParameterSetName="localBuildAll")]
[Parameter(Mandatory, ParameterSetName="TestByName")]
[Parameter(Mandatory, ParameterSetName="TestAll")]
[Parameter(Mandatory, ParameterSetName="GetTagsByName")]
[Parameter(Mandatory, ParameterSetName="GetTagsByChannel")]
[ValidatePattern('(\d+\.){2}\d(-\w+(\.\d+)?)?')]
[string]
$Version,
[Parameter(ParameterSetName="GenerateMatrixJson")]
[Parameter(ParameterSetName="GenerateManifestLists")]
[Parameter(ParameterSetName="GenerateTagsYaml")]
[ValidatePattern('(\d+\.){2}\d(-\w+(\.\d+)?)?')]
[string]
$StableVersion,
[Parameter(ParameterSetName="GenerateMatrixJson")]
[Parameter(ParameterSetName="GenerateManifestLists")]
[Parameter(ParameterSetName="GenerateTagsYaml")]
[ValidatePattern('(\d+\.){2}\d(-\w+(\.\d+)?)?')]
[string]
$LtsVersion,
[Parameter(ParameterSetName="GenerateManifestLists")]
[Parameter(ParameterSetName="GenerateMatrixJson")]
[Parameter(ParameterSetName="GenerateTagsYaml")]
[ValidatePattern('(\d+\.){2}\d(-\w+(\.\d+)?)?')]
[string]
$PreviewVersion,
[Parameter(ParameterSetName="GenerateManifestLists")]
[Parameter(ParameterSetName="GenerateMatrixJson")]
[Parameter(ParameterSetName="GenerateTagsYaml")]
[ValidatePattern('(\d+\.){2}\d(-\w+(\.\d+)?)?')]
[string]
$ServicingVersion,
[Parameter(ParameterSetName="GenerateTagsYaml")]
[ValidateSet('YAML','JSON')]
[string]
$Format = 'YAML',
[switch]
$IncludeKnownIssues,
[switch]
$ForcePesterInstall,
[Parameter(ParameterSetName="GenerateManifestLists")]
[Parameter(ParameterSetName="GenerateMatrixJson")]
[string]
[ValidateSet('All','OnlyAcr','NoAcr')]
$Acr,
[Parameter(ParameterSetName="GenerateManifestLists")]
[Parameter(ParameterSetName="GenerateMatrixJson")]
[string]
[ValidateSet('All','Linux','Windows')]
$OsFilter
)
DynamicParam {
# Add a dynamic parameter '-Name' which specifies the name(s) of the images to build or test
$buildHelperPath = Join-Path -Path $PSScriptRoot -ChildPath 'tools/buildHelper'
Import-Module $buildHelperPath
$imageChannel = 'all'
$dockerFileNames = @()
Get-ImageList -Channel $imageChannel | ForEach-Object { $dockerFileNames += $_ }
# Create the parameter attributs
$Attributes = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
Add-ParameterAttribute -ParameterSetName 'TestByName' -Attributes $Attributes
Add-ParameterAttribute -ParameterSetName 'localBuildByName' -Attributes $Attributes
Add-ParameterAttribute -ParameterSetName 'GetTagsByName' -Attributes $Attributes
Add-ParameterAttribute -ParameterSetName 'GenerateTagsYaml' -Attributes $Attributes -Mandatory $false
if($dockerFileNames.Count -gt 0)
{
$ValidateSetAttr = [System.Management.Automation.ValidateSetAttribute]::new(([string[]]$dockerFileNames))
$Attributes.Add($ValidateSetAttr) > $null
}
# Create the parameter
$Parameter = [System.Management.Automation.RuntimeDefinedParameter]::new("Name", [string[]], $Attributes)
# Return parameters dictionaly
$parameters = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$parameters.Add("Name", $Parameter) > $null
# Create the parameter attributs
$channelAttributes = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
Add-ParameterAttribute -ParameterSetName 'TestAll' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'TestByName' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'localBuildAll' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'localBuildByName' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'GetTagsByName' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'GetTagsByChannel' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'DupeCheck' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'GenerateMatrixJson' -Attributes $channelAttributes -Mandatory $false
Add-ParameterAttribute -ParameterSetName 'GenerateTagsYaml' -Attributes $channelAttributes
Add-ParameterAttribute -ParameterSetName 'GenerateManifestLists' -Attributes $channelAttributes
$channelNames = Get-ChannelNames
$ValidateSetAttr = [System.Management.Automation.ValidateSetAttribute]::new(([string[]]$channelNames))
$channelAttributes.Add($ValidateSetAttr) > $null
# Create the parameter
$Parameter = [System.Management.Automation.RuntimeDefinedParameter]::new("Channel", [string[]], $channelAttributes)
$parameters.Add("Channel", $Parameter) > $null
return $parameters
}
Begin {
$Channel = $PSBoundParameters["Channel"]
if ($PSBoundParameters['Name']) {
$Name = $PSBoundParameters["Name"]
} else {
$Name = $null
}
$ENV:DOCKER_BUILDKIT = 1
if ($PSCmdlet.ParameterSetName -notin 'GenerateMatrixJson', 'GenerateTagsYaml', 'DupeCheck', 'GenerateManifestLists' -and $Channel.Count -gt 1)
{
throw "Multiple Channels are not supported in this parameter set"
}
# We are using the Channel parameter, so assign the variable to that
if ($PSCmdlet.ParameterSetName -like '*All' -or ($PSCmdlet.ParameterSetName -eq 'localBuildByName' -and $Channel.Count -eq 0)) {
$Channels = $channelNames
} elseif ($FullJson) {
$Channels = $channelNames | Where-Object { $_ -notlike 'community*' }
} else {
$Channels = $Channel
}
foreach($dockerFileName in $Name) {
$images = (Get-ImageList -Channel $Channels)
if($dockerFileName -notin $images) {
$exception = [System.Management.Automation.ParameterBindingException]::new("Image $dockerFileName not found in channel $Channels")
throw $exception
}
}
Write-Verbose "Channels: $Channels"
$sasData = $null
if($SasUrl)
{
$sasData = New-SasData -SasUrl $SasUrl
}
}
End {
Set-StrictMode -Version latest
$localImageNames = @()
$testArgList = @()
$tagGroups = @{}
$dupeCheckTable = @{}
$dupeTagIssues = @()
$fullMatrix = @{}
$toBuild = @()
foreach ($actualChannel in $Channels) {
if ($PSBoundParameters['Name'])
{
# We are using the Name parameter, so assign the variable to that
$Name = $PSBoundParameters['Name']
}
else
{
# We are using all, so get the list off all images for the current channel
$Name = Get-ImageList -Channel $actualChannel
}
$versionExtraParams = @{
ServicingVersion = if($Version) {$Version} else {$ServicingVersion}
PreviewVersion = if($Version) {$Version} else {$PreviewVersion}
StableVersion = if($Version) {$Version} else {$StableVersion}
LtsVersion = if($Version) {$Version} else {$LtsVersion}
}
# Get Versions
$versions = Get-Versions -Channel $actualChannel @versionExtraParams
$windowsVersion = $versions.windowsVersion
$linuxVersion = $versions.linuxVersion
# Calculate the paths
$channelPath = Get-ChannelPath -Channel $actualChannel
foreach($dockerFileName in $Name)
{
# Get all image meta data
$allMeta = Get-DockerImageMetaDataWrapper `
-DockerFileName $dockerFileName `
-CI:$CI.IsPresent `
-IncludeKnownIssues:$IncludeKnownIssues.IsPresent `
-ChannelPath $channelPath `
-TagFilter $TagFilter `
-Version $windowsVersion `
-ImageName $ImageName `
-LinuxVersion $linuxVersion `
-BaseRepository $Repository `
-Strict:$CheckForDuplicateTags.IsPresent `
-Channel $actualChannel
$nameForMessage = Split-Path -Leaf -Path $dockerFileName
$message = "$nameForMessage does not exist in every channel. Skipping."
if(!$allMeta)
{
Write-Warning $message
if($CI.IsPresent -and !$GetTags.IsPresent)
{
throw $message
}
}
else
{
$toBuild += $allMeta
if($allMeta.Meta.SubImage)
{
foreach ($tagGroup in $allMeta.ActualTagDataByGroup.Keys)
{
$actualTagData = $allMeta.ActualTagDataByGroup.$tagGroup
$SubImagePath = Join-Path -Path $dockerFileName -ChildPath $allMeta.Meta.SubImage
Write-Verbose -Message "getting subimage - fromtag: $($dockerFileName) - subimage: $($allMeta.Meta.SubImage)"
$subImageAllMeta = Get-DockerImageMetaDataWrapper `
-DockerFileName $SubImagePath `
-CI:$CI.IsPresent `
-IncludeKnownIssues:$IncludeKnownIssues.IsPresent `
-ChannelPath $channelPath `
-TagFilter $TagFilter `
-Version $windowsVersion `
-ImageName $ImageName `
-LinuxVersion $linuxVersion `
-TagData $allMeta.TagData `
-BaseImage $actualTagData.ActualTags[0] `
-BaseRepository $Repository `
-Strict:$CheckForDuplicateTags.IsPresent `
-FromTag $tagGroup.Name `
-Channel $actualChannel
$toBuild += $subImageAllMeta
}
}
}
}
} # end foreach channel
foreach($allMeta in $toBuild)
{
foreach ($tagGroup in $allMeta.ActualTagDataByGroup.Keys)
{
$dockerFileName = $allMeta.Name
$actualTagData = $allMeta.ActualTagDataByGroup.$tagGroup
$actualChannel = $allMeta.Channel
$useAcr = $allMeta.meta.UseAcr
$continueOnError = $allMeta.meta.ContinueOnError
if ($Build.IsPresent -or $Test.IsPresent)
{
$params = @{
Dockerfile = $dockerFileName
psversion = $allMeta.psversion
SasData = $sasData
actualChannel = $actualChannel
actualTagData = $actualTagData
actualVersion = $windowsVersion
AllMeta = $allMeta
CI = $CI.IsPresent
}
if($allMeta.BaseImage)
{
$params.Add('BaseImage',$allMeta.BaseImage)
}
$testParams = Get-TestParams @params
$testArgList += $testParams.TestArgs
$localImageNames += $testParams.ImageName
}
elseif ($GetTags.IsPresent) {
Write-Verbose "from: $($actualTagData.fromTag) actual: $($actualTagData.actualTags -join ', ') psversion: $($allMeta.psversion)" -Verbose
}
elseif ($CheckForDuplicateTags.IsPresent) {
Write-Verbose "$actualChannel - from: $($actualTagData.fromTag) actual: $($actualTagData.actualTags -join ', ') psversion: $($allMeta.psversion)" -Verbose
foreach($tag in $actualTagData.actualTags)
{
if($dupeCheckTable.ContainsKey($tag))
{
$dupeTagIssues += "$tag is duplicate for both '$actualChannel/$dockerFileName' and '$($dupeCheckTable.$tag)'"
}
else
{
$dupeCheckTable.Add($tag,"$actualChannel/$dockerFileName")
}
}
}
elseif ($GenerateTagsYaml.IsPresent -or $GenerateMatrixJson.IsPresent -or $GenerateManifestLists.IsPresent) {
if($Acr -eq 'OnlyAcr' -and !$useAcr)
{
continue
}
if($Acr -eq 'NoAcr' -and $useAcr)
{
continue
}
$tagGroup = $($allMeta.FullRepository)
$os = 'windows'
if ($allMeta.meta.IsLinux) {
$os = 'linux'
}
if ($osFilter -eq 'Linux' -and $os -ne 'linux') {
continue
}
if ($osFilter -eq 'Windows' -and $os -ne 'windows') {
continue
}
$architecture = $allMeta.meta.Architecture
$imagePath = $allMeta.imagePath
$relativeImagePath = $imagePath.Replace($PSScriptRoot,'')
$relativeImagePath = $relativeImagePath -replace '\\', '/'
$dockerfile = "https://github.com/PowerShell/PowerShell-Docker/blob/master$relativeImagePath/docker/Dockerfile"
$osVersion = $allMeta.meta.osVersion
$manifestLists = $allMeta.meta.ManifestLists
if($osVersion -or $GenerateMatrixJson.IsPresent -or $GenerateManifestLists.IsPresent)
{
if ($osVersion) {
$osVersion = $osVersion.replace('${fromTag}', $actualTagData.fromTag)
}
else {
throw "meta.json should have osVersion: $dockerFileName"
}
# skip if we are generate TagsYaml and the image is private.
if (!$GenerateTagsYaml.IsPresent -or !$allMeta.Meta.IsPrivate) {
if (!$tagGroups.ContainsKey($tagGroup)) {
$tags = @()
$tagGroups[$tagGroup] = $tags
}
$tag = [PSCustomObject]@{
Architecture = $architecture
OsVersion = $osVersion
Os = $os
Tags = $actualTagData.TagList
Dockerfile = $dockerfile
Channel = $actualChannel
Name = $dockerFileName
UseAcr = $UseAcr
ContinueOnError = $continueOnError
ManifestLists = $manifestLists
EndOfLife = $allMeta.meta.EndOfLife
DistributionState = $allMeta.meta.GetDistributionState().ToString()
IsLinux = $allMeta.meta.IsLinux
UseInCI = $allMeta.meta.UseInCI
}
$tagGroups[$tagGroup] += $tag
}
else {
Write-Verbose -Message "skipping generating yaml for $dockerFileName" -Verbose
}
}
else {
Write-Verbose "Skipping $($actualTagData.tagList[0]) due to no OS Version in meta.json" -Verbose
}
}
}
}
if($testArgList.Count -gt 0)
{
$logPath = Join-Path -Path $PSScriptRoot -ChildPath "testResults$TestLogPostfix.xml"
$testsPath = Join-Path -Path $PSScriptRoot -ChildPath 'tests'
$testArgPath = Join-Path -Path $testsPath -ChildPath 'testArgs.json'
$testArgList | ConvertTo-Json -Depth 2 | Out-File -FilePath $testArgPath
$extraParams = @{}
if($Test.IsPresent)
{
$tags = @('Behavior')
if($Pull.IsPresent)
{
$tags += 'Pull'
}
$extraParams.Add('Tags',$tags)
}
else {
$tags = @('Build')
if(!$SkipTest.IsPresent)
{
$tags += 'Behavior'
}
if($Pull.IsPresent)
{
$tags += 'Pull'
}
if($Push.IsPresent)
{
$tags += 'Push'
}
$extraParams.Add('Tags', $tags)
}
Invoke-PesterWrapper -Script $testsPath -OutputFile $logPath -ExtraParams $extraParams
}
Write-Verbose "!$GenerateTagsYaml -and !$GenerateMatrixJson -and !$GenerateManifestLists -and '$($PSCmdlet.ParameterSetName)' -notlike '*All'" -Verbose
if (!$GenerateTagsYaml -and !$GenerateMatrixJson -and !$GenerateManifestLists -and $PSCmdlet.ParameterSetName -notlike '*All') {
$dockerImagesToScan = ''
# print local image names
# used only with the -Build
foreach ($fullName in $localImageNames) {
Write-Verbose "image name: $fullName" -Verbose
if ($dockerImagesToScan -ne '') {
$dockerImagesToScan += ',' + $dockerImagesToScan
}
else {
$dockerImagesToScan += $fullName
}
}
if ($dockerImagesToScan) {
Set-BuildVariable -Name 'dockerImagesToScan' -Value $dockerImagesToScan
}
}
if($GenerateTagsYaml.IsPresent)
{
if ($Format -eq 'YAML') {
Write-Output "repos:"
foreach($repo in $tagGroups.Keys | Sort-Object)
{
Write-Output " - repoName: $repo"
Write-Output " tagGroups:"
foreach($tag in $tagGroups.$repo | Sort-Object -Property dockerfile)
{
Write-Output " - tags: [$($tag.Tags -join ', ')]"
Write-Output " osVersion: $($tag.osVersion)"
Write-Output " architecture: $($tag.architecture)"
Write-Output " os: $($tag.os)"
Write-Output " dockerfile: $($tag.dockerfile)"
}
}
}
else {
$repos = @{}
foreach ($repo in $tagGroups.Keys | Sort-Object) {
$tagGroupsJson = @()
foreach ($tag in $tagGroups.$repo | Sort-Object -Property dockerfile) {
$tagGroupsJson += @{
tags = $tag.Tags
osVersion = $tag.OsVersion
architecture = $tag.architecture
os = $tag.os
dockerfile = $tag.dockerfile
}
}
$repos.Add($repo, $tagGroupsJson)
}
$repos | ConvertTo-Json -Depth 10
}
}
if ($GenerateMatrixJson.IsPresent) {
$matrix = @{ }
foreach ($repo in $tagGroups.Keys | Sort-Object) {
$channelGroups = $tagGroups.$repo | Group-Object -Property Channel
foreach($channelGroup in $channelGroups)
{
$channelName = $channelGroup.Name
Write-Verbose "generating $channelName json"
$osGroups = $channelGroup.Group | Group-Object -Property os
foreach ($osGroup in $osGroups) {
$osName = $osGroup.Name
$architectureGroups = $osGroup.Group | Group-Object -Property Architecture
foreach ($architectureGroup in $architectureGroups) {
$architectureName = $architectureGroup.Name
# Filter out subimages. We cannot directly build subimages.
foreach ($tag in $architectureGroup.Group | Where-Object { $_.Name -notlike '*/*' } | Sort-Object -Property dockerfile) {
if (-not $matrix.ContainsKey($channelName)) {
$matrix.Add($channelName, @{ })
}
if (-not $matrix.$channelName.ContainsKey($osName)) {
$matrix.$channelName.Add($osName, @{ })
}
if (-not $matrix.$channelName.$osName.ContainsKey($architectureName)) {
$matrix.$channelName.$osName.Add($architectureName, @{})
}
$jobName = $tag.Name -replace '-', '_'
if (-not $matrix.$channelName[$osName][$architectureName].ContainsKey($jobName) -and -not $tag.ContinueOnError) {
$matrix.$channelName[$osName][$architectureName].Add($jobName, (ConvertTo-SortedDictionary -Hashtable @{
Channel = $tag.Channel
ImageName = $tag.Name
JobName = $jobName
ContinueOnError = $tag.ContinueOnError
EndOfLife = $tag.EndOfLife
DistributionState = $tag.DistributionState
OsVersion = $tag.OsVersion
# azDevOps doesn't support arrays
TagList = $tag.Tags -join ';'
IsLinux = $tag.IsLinux
UseInCI = $tag.UseInCI
Architecture = $tag.Architecture
}))
}
}
}
}
}
}
foreach ($channelName in $matrix.Keys | Sort-Object) {
$fullMatrix[$channelName] = @()
foreach ($osName in $matrix.$channelName.Keys | Sort-Object) {
$osMatrix = $matrix.$channelName.$osName
foreach ($architectureName in $osMatrix.Keys | Sort-Object) {
$architectureMatrix = $osMatrix.$architectureName
$channelMatrix = [System.Collections.ArrayList]::new()
$architectureMatrix.Values | Sort-Object -Property ImageName | ForEach-Object {
$null = $channelMatrix.Add($_)
}
$fullMatrix[$channelName] += $channelMatrix
$matrixJson = $architectureMatrix | ConvertTo-Json -Compress
$variableName = "matrix_${channelName}_${osName}_${architectureName}"
if (!$FullJson) {
Set-BuildVariable -Name $variableName -Value $matrixJson -IsOutput
Write-Verbose -Verbose "*********END of JSON*********************"
}
}
}
}
if($FullJson) {
$matrixJson = $fullMatrix | ConvertTo-Json -Depth 100
Write-Output $matrixJson
}
}
if ($GenerateManifestLists.IsPresent) {
$manifestLists = @()
$tags = @()
foreach ($repo in $tagGroups.Keys | Sort-Object) {
$channelGroups = $tagGroups.$repo | Group-Object -Property Channel
foreach ($channelGroup in $channelGroups) {
$channelName = $channelGroup.Name
switch($channelName) {
'stable' {
$channelTag = 'latest'
$channelTagPrefix = ''
$channelTagPostfix = ''
}
default {
$channelTag = $channelName.ToLower()
$channelTagPrefix = $channelTag + '-'
$channelTagPostfix = '-' + $channelTag
}
}
Write-Verbose "generating $channelName json"
$osGroups = $channelGroup.Group | Group-Object -Property os
foreach ($osGroup in $osGroups) {
$osName = $osGroup.Name
# Filter out subimages. We cannot directly build subimages.
foreach ($tag in $osGroup.Group | Where-Object { $_.Name -notlike '*/*' } | Sort-Object -Property ManifestLists) {
if ($tag.ManifestLists) {
foreach ($manifestList in $tag.ManifestLists) {
$originalManifestList = $manifestList
if ($manifestList -notlike '*${channel*') {
Write-Warning "Issue found with $osName $manifestList $($tag.Tags)"
throw 'ManifestLists entries must contain on of: ${channelTag} ${channelTagPrefix} ${channelTagPostfix}'
}
$manifestList = $manifestList -replace '\${channelTag}', $channelTag
$manifestList = $manifestList -replace '\${channelTagPrefix}', $channelTagPrefix
$manifestList = $manifestList -replace '\${channelTagPostfix}', $channelTagPostfix
if (!$manifestList) {
throw "error formatting $originalManifestList"
}
if ($manifestLists -notcontains $manifestList) {
$manifestLists += $manifestList
}
$tags += [PSCustomObject]@{
Repo = $repo
FormattedManifestList = $manifestList
Tags = $tag.Tags
Channel = $tag.Channel
ContinueOnError = $tag.ContinueOnError
}
}
}
}
}
}
}
$matrix = @{}
foreach ($manifestList in $manifestLists) {
$jobName = $manifestList -replace '-', '_'
$manifestListTags = [PSCustomObject]@{
ManifestList = $manifestList
Channel = ""
Tags = @()
JobName = $jobName
Repo = ""
}
foreach ($tag in $tags | Where-Object { $_.FormattedManifestList -eq $manifestList }) {
if (-not $matrix.ContainsKey($manifestList)) {
$matrix.Add($manifestList, @{ })
}
$manifestListTags.Channel = $tag.Channel
$manifestListTags.Tags += $tag.Tags | Where-Object { $_ -notmatch '\d{8}' } | Select-Object -First 1
$manifestListTags.Repo = $tag.Repo
}
if (-not $matrix.$manifestList.ContainsKey($jobName) -and -not $tag.ContinueOnError) {
$matrix.$manifestList.Add($jobName, $manifestListTags)
}
}
foreach ($manifestList in $matrix.Keys) {
foreach ($jobName in $matrix.$manifestList.Keys) {
$jobMatrix = $matrix.$manifestList.$jobName
$matrixJson = $jobMatrix | ConvertTo-Json -Compress
Write-Output $matrixJson
}
}
exit 0
}
if($CheckForDuplicateTags.IsPresent)
{
if($dupeTagIssues.count -gt 0)
{
throw ($dupeTagIssues -join [System.Environment]::NewLine)
}
else
{
Write-Verbose "No duplicates found." -Verbose
}
}
}