• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..09-Jul-2021-

samples/H09-Jul-2021-10696

tests/H09-Jul-2021-1,2651,107

Create-JobMatrix.ps1H A D09-Jul-20211.3 KiB3822

README.mdH A D09-Jul-202117.2 KiB534448

job-matrix-functions.ps1H A D09-Jul-202119.7 KiB681546

README.md

1# Azure Pipelines Matrix Generator
2
3* [Usage in a pipeline](#usage-in-a-pipeline)
4* [Matrix config file syntax](#matrix-config-file-syntax)
5 * [Fields](#fields)
6    * [matrix](#matrix)
7    * [include](#include)
8    * [exclude](#exclude)
9    * [displayNames](#displaynames)
10    * [$IMPORT](#import)
11* [Matrix Generation behavior](#matrix-generation-behavior)
12    * [all](#all)
13    * [sparse](#sparse)
14    * [include/exclude](#includeexclude)
15    * [displayNames](#displaynames-1)
16    * [Filters](#filters)
17    * [Replace](#replace-values)
18    * [NonSparseParameters](#nonsparseparameters)
19    * [Under the hood](#under-the-hood)
20* [Testing](#testing)
21
22
23This directory contains scripts supporting dynamic, cross-product matrix generation for azure pipeline jobs.
24It aims to replicate the [cross-product matrix functionality in github actions](https://docs.github.com/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#example-running-with-more-than-one-version-of-nodejs),
25but also adds some additional features like sparse matrix generation, cross-product includes and excludes, and programmable matrix filters.
26
27This functionality is made possible by the ability for the azure pipelines yaml to take a [dynamic variable as an input
28for a job matrix definition](https://docs.microsoft.com/azure/devops/pipelines/process/phases?view=azure-devops&tabs=yaml#multi-job-configuration) (see the code sample at the bottom of the linked section).
29
30## Usage in a pipeline
31
32In order to use these scripts in a pipeline, you must provide a config file and call the matrix creation script within a powershell job.
33
34For a single matrix, you can include the `/eng/common/pipelines/templates/jobs/archetype-sdk-tests-generate.yml` template in a pipeline (see /eng/common/scripts/job-matrix/samples/matrix-test.yml for a full working example):
35
36```
37jobs:
38  - template: /eng/common/pipelines/templates/jobs/archetype-sdk-tests-generate.yml
39    parameters:
40      MatrixConfigs:
41        - Name: base_product_matrix
42          Path: eng/scripts/job-matrix/samples/matrix.json
43          Selection: all
44          NonSparseParameters:
45            - framework
46          GenerateVMJobs: true
47        - Name: sparse_product_matrix
48          Path: eng/scripts/job-matrix/samples/matrix.json
49          Selection: sparse
50          GenerateVMJobs: true
51      JobTemplatePath: /eng/common/scripts/job-matrix/samples/matrix-job-sample.yml
52      AdditionalParameters: []
53      CloudConfig:
54        SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources)
55        Location: eastus2
56        Cloud: Public
57      MatrixFilters: []
58      MatrixReplace: []
59```
60
61## Matrix config file syntax
62
63Matrix parameters can either be a list of strings, or a set of grouped strings (represented as a hash). The latter parameter
64type is useful for when 2 or more parameters need to be grouped together, but without generating more than one matrix permutation.
65
66```
67"matrix": {
68  "<parameter1 name>": [ <values...> ],
69  "<parameter2 name>": [ <values...> ],
70  "<parameter set>": {
71    "<parameter set 1 name>": {
72        "<parameter set 1 value 1": "value",
73        "<parameter set 1 value 2": "<value>",
74    },
75    "<parameter set 2 name>": {
76        "<parameter set 2 value 1": "value",
77        "<parameter set 2 value 2": "<value>",
78    }
79  }
80}
81"include": [ <matrix>, <matrix>, ... ],
82"exclude": [ <matrix>, <matrix>, ... ],
83"displayNames": { <parameter value>: <human readable override> }
84```
85
86See `samples/matrix.json` for a full sample.
87
88### Fields
89
90#### matrix
91
92The `matrix` field defines the base cross-product matrix. The generated matrix can be full or sparse.
93
94Example:
95```
96"matrix": {
97  "operatingSystem": [
98    "windows-2019",
99    "ubuntu-18.04",
100    "macOS-10.15"
101  ],
102  "framework": [
103    "net461",
104    "netcoreapp2.1",
105    "net50"
106  ],
107  "additionalTestArguments": [
108    "",
109    "/p:UseProjectReferenceToAzureClients=true",
110  ]
111}
112```
113
114#### include
115
116The `include` field defines any number of matrices to be appended to the base matrix after processing exclusions.
117
118#### exclude
119
120The `include` field defines any number of matrices to be removed from the base matrix. Exclude parameters can be a partial
121set, meaning as long as all exclude parameters match against a matrix entry (even if the matrix entry has additional parameters),
122then it will be excluded from the matrix. For example, the below entry will match the exclusion and be removed:
123
124```
125matrix entry:
126{
127    "a": 1,
128    "b": 2,
129    "c": 3,
130}
131
132"exclude": [
133    {
134        "a": 1,
135        "b": 2
136    }
137]
138```
139
140#### displayNames
141
142Specify any overrides for the azure pipelines definition and UI that determines the matrix job name. If some parameter
143values are too long or unreadable for this purpose (e.g. a command line argument), then you can replace them with a more
144readable value here. For example:
145
146```
147"displayNames": {
148  "/p:UseProjectReferenceToAzureClients=true": "UseProjectRef"
149},
150"matrix": {
151  "additionalTestArguments": [
152    "/p:UseProjectReferenceToAzureClients=true"
153  ]
154}
155```
156
157#### $IMPORT
158
159Matrix configs can also import another matrix config. The effect of this is the imported matrix will be generated,
160and then the importing config will be combined with that matrix (as if each entry of the imported matrix was a parameter).
161To import a matrix, add a parameter with the key `$IMPORT`:
162
163```
164"matrix": {
165  "$IMPORT": "path/to/matrix.json",
166  "JavaVersion": [ "1.8", "1.11" ]
167}
168```
169
170Importing can be useful, for example, in cases where there is a shared base matrix, but there is a need to run it
171once for each instance of a language version. Importing does not support overriding duplicate parameters. To achieve
172this, use the [Replace](#replace-values) argument instead.
173
174The `Selection` and `NonSparseParameters` parameters are respected when generating an imported matrix.
175
176The processing order is as follows:
177
178Given a matrix and import matrix like below:
179```
180{
181    "matrix": {
182        "$IMPORT": "example-matrix.json",
183        "endpointType": [ "storage", "cosmos" ],
184        "JavaVersion": [ "1.8", "1.11" ]
185    },
186    "include": [
187        {
188            "operatingSystem": "windows",
189            "mode": "TestFromSource",
190            "JavaVersion": "1.8"
191        }
192    ]
193}
194
195### example-matrix.json to import
196{
197    "matrix": {
198      "operatingSystem": [ "windows", "linux" ],
199      "client": [ "netty", "okhttp" ]
200    },
201    "include": [
202        {
203          "operatingSystem": "mac",
204          "client": "netty"
205        }
206    ]
207}
208```
209
2101. The base matrix is generated (sparse in this example):
211    ```
212    {
213      "storage_18": {
214        "endpointType": "storage",
215        "JavaVersion": "1.8"
216      },
217      "cosmos_111": {
218        "endpointType": "cosmos",
219        "JavaVersion": "1.11"
220      }
221    }
222    ```
2231. The imported base matrix is generated (sparse in this example):
224    ```
225    {
226      "windows_netty": {
227        "operatingSystem": "windows",
228        "client": "netty"
229      },
230      "linux_okhttp": {
231        "operatingSystem": "linux",
232        "client": "okhttp"
233      }
234    }
235    ```
2361. Includes/excludes from the imported matrix get applied to the imported matrix
237    ```
238    {
239      "windows_netty": {
240        "operatingSystem": "windows",
241        "client": "netty"
242      },
243      "linux_okhttp": {
244        "operatingSystem": "linux",
245        "client": "okhttp"
246      },
247      "mac_netty": {
248        "operatingSystem": "mac",
249        "client": "netty"
250      }
251    }
252    ```
2531. The base matrix is multipled by the imported matrix (in this case, the base matrix has 2 elements, and the imported
254   matrix has 3 elements, so the product is a matrix with 6 elements:
255    ```
256      "storage_18_windows_netty": {
257        "endpointType": "storage",
258        "JavaVersion": "1.8",
259        "operatingSystem": "windows",
260        "client": "netty"
261      },
262      "storage_18_linux_okhttp": {
263        "endpointType": "storage",
264        "JavaVersion": "1.8",
265        "operatingSystem": "linux",
266        "client": "okhttp"
267      },
268      "storage_18_mac_netty": {
269        "endpointType": "storage",
270        "JavaVersion": "1.8",
271        "operatingSystem": "mac",
272        "client": "netty"
273      },
274      "cosmos_111_windows_netty": {
275        "endpointType": "cosmos",
276        "JavaVersion": "1.11",
277        "operatingSystem": "windows",
278        "client": "netty"
279      },
280      "cosmos_111_linux_okhttp": {
281        "endpointType": "cosmos",
282        "JavaVersion": "1.11",
283        "operatingSystem": "linux",
284        "client": "okhttp"
285      },
286      "cosmos_111_mac_netty": {
287        "endpointType": "cosmos",
288        "JavaVersion": "1.11",
289        "operatingSystem": "mac",
290        "client": "netty"
291      }
292    }
293    ```
2941. Includes/excludes from the top-level matrix get applied to the multiplied matrix, so the below element will be added
295   to the above matrix, for an output matrix with 7 elements:
296    ```
297    "windows_TestFromSource_18": {
298      "operatingSystem": "windows",
299      "mode": "TestFromSource",
300      "JavaVersion": "1.8"
301    }
302    ```
303
304## Matrix Generation behavior
305
306#### all
307
308`all` will output the full matrix, i.e. every possible permutation of all parameters given (p1.Length * p2.Length * ...).
309
310#### sparse
311
312`sparse` outputs the minimum number of parameter combinations while ensuring that all parameter values are present in at least one matrix job.
313Effectively this means the total length of a sparse matrix will be equal to the largest matrix dimension, i.e. `max(p1.Length, p2.Length, ...)`.
314
315To build a sparse matrix, a full matrix is generated, and then walked diagonally N times where N is the largest matrix dimension.
316This pattern works for any N-dimensional matrix, via an incrementing index (n, n, n, ...), (n+1, n+1, n+1, ...), etc.
317Index lookups against matrix dimensions are calculated modulus the dimension size, so a two-dimensional matrix of 4x2 might be walked like this:
318
319```
320index: 0, 0:
321o . . .
322. . . .
323
324index: 1, 1:
325. . . .
326. o . .
327
328index: 2, 2 (modded to 2, 0):
329. . o .
330. . . .
331
332index: 3, 3 (modded to 3, 1):
333. . . .
334. . . o
335```
336
337#### include/exclude
338
339Include and exclude support additions and subtractions off the base matrix. Both include and exclude take an array of matrix values.
340Typically these values will be a single entry, but they also support the cross-product matrix definition syntax of the base matrix.
341
342Include and exclude are parsed fully. So if a sparse matrix is called for, a sparse version of the base matrix will be generated, but
343the full matrix of both include and exclude will be processed.
344
345Excludes are processed first, so includes can be used to add back any specific jobs to the matrix.
346
347#### displayNames
348
349In the matrix job output that azure pipelines consumes, the format is a dictionary of dictionaries. For example:
350
351```
352{
353  "net461_macOS1015": {
354    "framework": "net461",
355    "operatingSystem": "macOS-10.15"
356  },
357  "net50_ubuntu1804": {
358    "framework": "net50",
359    "operatingSystem": "ubuntu-18.04"
360  },
361  "netcoreapp21_windows2019": {
362    "framework": "netcoreapp2.1",
363    "operatingSystem": "windows-2019"
364  },
365  "UseProjectRef_net461_windows2019": {
366    "additionalTestArguments": "/p:UseProjectReferenceToAzureClients=true",
367    "framework": "net461",
368    "operatingSystem": "windows-2019"
369  }
370}
371```
372
373The top level keys are used as job names, meaning they get displayed in the azure pipelines UI when running the pipeline.
374
375The logic for generating display names works like this:
376
377- Join parameter values by "_"
378    a. If the parameter value exists as a key in `displayNames` in the matrix config, replace it with that value.
379    b. For each name value, strip all non-alphanumeric characters (excluding "_").
380    c. If the name is greater than 100 characters, truncate it.
381
382#### Filters
383
384Filters can be passed to the matrix as an array of strings, each matching the format of `<key>=<regex>`. When a matrix entry
385does not contain the specified key, it will default to a value of empty string for regex parsing. This can be used to specify
386filters for keys that don't exist or keys that optionally exist and match a regex, as seen in the below example.
387
388Display name filters can also be passed as a single regex string that runs against the [generated display name](#displaynames) of the matrix job.
389The intent of display name filters is to be defined primarily as a top level variable at template queue time in the azure pipelines UI.
390
391For example, the below command will filter for matrix entries with "windows" in the job display name, no matrix variable
392named "ExcludedKey", a framework variable containing either "461" or "5.0", and an optional key "SupportedClouds" that, if exists, must contain "Public":
393
394```
395./Create-JobMatrix.ps1 `
396  -ConfigPath samples/matrix.json `
397  -Selection all `
398  -DisplayNameFilter ".*windows.*" `
399  -Filters @("ExcludedKey=^$", "framework=(461|5\.0)", "SupportedClouds=^$|.*Public.*")
400```
401
402#### Replace values
403
404Replacements for values can be passed to the matrix as an array of strings, each matching the format of `<keyRegex>=<valueRegex>/<replacementValue>`.
405The replace argument will find any permutations where the key fully matches the key regex and the value fully matches the value regex, and replace the value with
406the replacement specified.
407
408NOTE:
409- The replacement value supports regex capture groups, enabling substring transformations, e.g. `Foo=(.*)-replaceMe/$1-replaced`. See the below examples for usage.
410- For each key/value, the first replacement provided that matches will be the only one applied.
411- If `=` or `/` characters need to be part of the regex or replacement, escape them with `\`.
412
413For example, given a matrix config like below:
414
415```
416{
417  "matrix": {
418    "Agent": {
419      "ubuntu-1804": { "OSVmImage": "MMSUbuntu18.04", "Pool": "azsdk-pool-mms-ubuntu-1804-general" }
420    },
421    "JavaTestVersion": [ "1.8", "1.11" ]
422  }
423}
424
425```
426
427The normal matrix output (without replacements), looks like:
428
429```
430$ ./Create-JobMatrix.ps1 -ConfigPath <test> -Selection all
431{
432  "ubuntu1804_18": {
433    "OSVmImage": "MMSUbuntu18.04",
434    "Pool": "azsdk-pool-mms-ubuntu-1804-general",
435    "JavaTestVersion": "1.8"
436  },
437  "ubuntu1804_111": {
438    "OSVmImage": "MMSUbuntu18.04",
439    "Pool": "azsdk-pool-mms-ubuntu-1804-general",
440    "JavaTestVersion": "1.11"
441  }
442}
443```
444
445Passing in multiple replacements, the output will look like below. Note that replacing key/values that appear nested within a grouping
446will not affect that segment of the job name, since the job takes the grouping name (in this case "ubuntu1804").
447
448The below example includes samples of regex grouping references, and wildcard key/value regexes:
449
450```
451$ $replacements = @('.*Version=1.11/2.0', 'Pool=(.*ubuntu.*)-general/$1-custom')
452$ ../Create-JobMatrix.ps1 -ConfigPath ./test.Json -Selection all -Replace $replacements
453{
454  "ubuntu1804_18": {
455    "OSVmImage": "MMSUbuntu18.04",
456    "Pool": "azsdk-pool-mms-ubuntu-1804-custom",
457    "JavaTestVersion": "1.8"
458  },
459  "ubuntu1804_20": {
460    "OSVmImage": "MMSUbuntu18.04",
461    "Pool": "azsdk-pool-mms-ubuntu-1804-custom",
462    "JavaTestVersion": "2.0"
463  }
464}
465```
466
467#### NonSparseParameters
468
469Sometimes it may be necessary to generate a sparse matrix, but keep the full combination of a few parameters. The
470NonSparseParameters argument allows for more fine-grained control of matrix generation. For example:
471
472```
473./Create-JobMatrix.ps1 `
474  -ConfigPath /path/to/matrix.json `
475  -Selection sparse `
476  -NonSparseParameters @("JavaTestVersion")
477```
478
479Given a matrix like below with `JavaTestVersion` marked as a non-sparse parameter:
480
481```
482{
483  "matrix": {
484    "Agent": {
485      "windows-2019": { "OSVmImage": "MMS2019", "Pool": "azsdk-pool-mms-win-2019-general" },
486      "ubuntu-1804": { "OSVmImage": "MMSUbuntu18.04", "Pool": "azsdk-pool-mms-ubuntu-1804-general" },
487      "macOS-10.15": { "OSVmImage": "macOS-10.15", "Pool": "Azure Pipelines" }
488    },
489    "JavaTestVersion": [ "1.8", "1.11" ],
490    "AZURE_TEST_HTTP_CLIENTS": "netty",
491    "ArmTemplateParameters": [ "@{endpointType='storage'}", "@{endpointType='cosmos'}" ]
492  }
493}
494```
495
496A matrix with 6 entries will be generated: A sparse matrix of Agent, AZURE_TEST_HTTP_CLIENTS and ArmTemplateParameters
497(3 total entries) will be multipled by the two `JavaTestVersion` parameters `1.8` and `1.11`.
498
499NOTE: NonSparseParameters are also applied when generating an imported matrix.
500
501#### Under the hood
502
503The script generates an N-dimensional matrix with dimensions equal to the parameter array lengths. For example,
504the below config would generate a 2x2x1x1x1 matrix (five-dimensional):
505
506```
507"matrix": {
508  "framework": [ "net461", "netcoreapp2.1" ],
509  "additionalTestArguments": [ "", "/p:SuperTest=true" ]
510  "pool": [ "ubuntu-18.04" ],
511  "container": [ "ubuntu-18.04" ],
512  "testMode": [ "Record" ]
513}
514```
515
516The matrix is stored as a one-dimensional array, with a row-major indexing scheme (e.g. `(2, 1, 0, 1, 0)`).
517
518## Testing
519
520The matrix functions can be tested using [pester](https://pester.dev/). The test command must be run from within the tests directory.
521
522```
523$ cd tests
524$ Invoke-Pester
525
526Starting discovery in 3 files.
527Discovery finished in 75ms.
528[+] /home/ben/sdk/azure-sdk-tools/eng/common/scripts/job-matrix/tests/job-matrix-functions.filter.tests.ps1 750ms (309ms|428ms)
529[+] /home/ben/sdk/azure-sdk-tools/eng/common/scripts/job-matrix/tests/job-matrix-functions.modification.tests.ps1 867ms (250ms|608ms)
530[+] /home/ben/sdk/azure-sdk-tools/eng/common/scripts/job-matrix/tests/job-matrix-functions.tests.ps1 2.71s (725ms|1.93s)
531Tests completed in 4.33s
532Tests Passed: 141, Failed: 0, Skipped: 4 NotRun: 0
533```
534