-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCreate-PrJobMatrix.ps1
More file actions
109 lines (92 loc) · 4.23 KB
/
Create-PrJobMatrix.ps1
File metadata and controls
109 lines (92 loc) · 4.23 KB
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
<#
.SYNOPSIS
Generates a combined PR job matrix from a package properties folder. It is effectively a combination of
Create-JobMatrix and distribute-packages-to-matrix
This script is intended to be used within an Azure DevOps pipeline to generate a job matrix for a PR.
.EXAMPLE
./eng/common/scripts/Create-PRJobMatrix $(Build.ArtifactStagingDirectory)/PackageProperties
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][string] $PackagePropertiesFolder,
[Parameter(Mandatory=$true)][string] $PRMatrixFile,
[Parameter(Mandatory=$true)][string] $PRMatrixSetting,
[Parameter(Mandatory=$False)][string] $DisplayNameFilter,
[Parameter(Mandatory=$False)][array] $Filters,
[Parameter(Mandatory=$False)][array] $Replace,
[Parameter()][switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID)
)
. $PSScriptRoot/job-matrix-functions.ps1
. $PSScriptRoot/../Helpers/Package-Helpers.ps1
if (!(Test-Path $PackagePropertiesFolder)) {
Write-Error "Package Properties folder doesn't exist"
exit 1
}
function GenerateMatrixForConfig {
param (
[Parameter(Mandatory=$true)][string] $ConfigPath,
[Parameter(Mandatory=$True)][string] $Selection,
[Parameter(Mandatory=$false)][string] $DisplayNameFilter,
[Parameter(Mandatory=$false)][array] $Filters,
[Parameter(Mandatory=$false)][array] $Replace
)
$config = GetMatrixConfigFromFile (Get-Content $ConfigPath -Raw)
# Strip empty string filters in order to be able to use azure pipelines yaml join()
$Filters = $Filters | Where-Object { $_ }
[array]$matrix = GenerateMatrix `
-config $config `
-selectFromMatrixType $Selection `
-displayNameFilter $DisplayNameFilter `
-filters $Filters `
-replace $Replace
return ,$matrix
}
# calculate general targeting information and create our batches prior to generating any matrix
# this prototype doesn't handle direct and indirect, it just batches for simplicity of the proto
$packageProperties = Get-ChildItem -Recurse "$PackagePropertiesFolder" *.json `
| % { Get-Content -Path $_.FullName | ConvertFrom-Json }
# in the full proto, we will compress the CIMatrixConfig value into a single string, then use it as a key
# to group the packages
# then we will simply iterate over the groups and generate the matrix for each group
# at the very end, will distribute the packages to the matrix based on the group key (batched over course)
$configs = @(
[PsCustomObject]@{
ConfigPath = $PRMatrixFile
Selection = "sparse"
}
)
$OverallResult = @()
foreach($matrixConfig in $configs) {
Write-Host "Generating config for $($matrixConfig.ConfigPath)"
$generatedMatrix = GenerateMatrixForConfig -ConfigPath $matrixConfig.ConfigPath -Selection $matrixConfig.Selection -DisplayNameFilter $DisplayNameFilter -Filters $Filters -Replace $Replace
# also note here that we should be using a filtered set of package properties
# (remember we figured out which matrix config is associated with each package)
# $batches = Split-ArrayIntoBatches -InputArray $packageProperties -BatchSize $BATCHSIZE
$batches = @()
$everything = ($packageProperties | ForEach-Object { $_.ArtifactName })
$batches += ,$everything
foreach($batch in $batches) {
$ModifiedMatrix = @()
# to understand this iteration, one must understand that the matrix is a list of hashtables, each with a couple keys:
# [
# { "name": "jobname", "parameters": { matrixSetting1: matrixValue1, ...} },
# ]
if($batches.Length -gt 1) {
throw "This script is not prepared to handle more than one batch. We will need to duplicate the input objects."
}
else {
foreach($config in $generatedMatrix) {
$namesForBatch = $batches[0] -join ","
# we just need to iterate across them, grab the parameters hashtable, and add the new key
# if there is more than one batch, we will need to add a suffix including the batch name to the job name
$config["parameters"]["$PRMatrixSetting"] = $namesForBatch
$OverallResult += $config
}
}
}
}
$serialized = SerializePipelineMatrix $OverallResult
Write-Output $serialized.pretty
if ($CI) {
Write-Output "##vso[task.setVariable variable=matrix;isOutput=true]$($serialized.compressed)"
}