You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/actions/get-started/understanding-github-actions.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,8 @@ A **job** is a set of **steps** in a workflow that is executed on the same **run
70
70
71
71
You can configure a job's dependencies with other jobs; by default, jobs have no dependencies and run in parallel. When a job takes a dependency on another job, it waits for the dependent job to complete before running.
72
72
73
+
You can also use a **matrix** to run the same job multiple times, each with a different combination of variables—like operating systems or language versions.
74
+
73
75
For example, you might configure multiple build jobs for different architectures without any job dependencies and a packaging job that depends on those builds. The build jobs run in parallel, and once they complete successfully, the packaging job runs.
74
76
75
77
For more information, see [AUTOTITLE](/actions/using-jobs).
Copy file name to clipboardExpand all lines: content/actions/how-tos/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow.md
{% data reusables.actions.enterprise-github-hosted-runners %}
18
-
19
17
## About matrix strategies
20
18
21
19
{% data reusables.actions.jobs.about-matrix-strategy %}
22
20
23
-
## Using a matrix strategy
24
-
25
-
{% data reusables.actions.jobs.using-matrix-strategy %}
26
-
27
-
### Example: Using a single-dimension matrix
28
-
29
-
{% data reusables.actions.jobs.single-dimension-matrix %}
30
-
31
-
### Example: Using a multi-dimension matrix
32
-
33
-
{% data reusables.actions.jobs.multi-dimension-matrix %}
34
-
35
-
### Example: Using contexts to create matrices
36
-
37
-
{% data reusables.actions.jobs.matrix-from-context %}
21
+
## Adding a matrix strategy to your workflow job
22
+
23
+
Use `jobs.<job_id>.strategy.matrix` to define a matrix of different job configurations. Within your matrix, define one or more variables followed by an array of values. For example, the following matrix has a variable called `version` with the value `[10, 12, 14]` and a variable called `os` with the value `[ubuntu-latest, windows-latest]`:
24
+
25
+
```yaml
26
+
jobs:
27
+
example_matrix:
28
+
strategy:
29
+
matrix:
30
+
version: [10, 12, 14]
31
+
os: [ubuntu-latest, windows-latest]
32
+
```
33
+
34
+
A job will run for each possible combination of the variables. In this example, the workflow will run six jobs, one for each combination of the `os` and `version` variables.
35
+
36
+
The above matrix will create the jobs in the following order.
37
+
38
+
* `{version: 10, os: ubuntu-latest}`
39
+
* `{version: 10, os: windows-latest}`
40
+
* `{version: 12, os: ubuntu-latest}`
41
+
* `{version: 12, os: windows-latest}`
42
+
* `{version: 14, os: ubuntu-latest}`
43
+
* `{version: 14, os: windows-latest}`
44
+
45
+
For reference information and examples, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix).
46
+
47
+
## Using contexts to create matrices
48
+
49
+
To create matrices with information about workflow runs, variables, runner environments, jobs, and steps, access contexts using the {% raw %}`${{ <context> }}`{% endraw %} expression syntax. For more information about contexts, see [AUTOTITLE](/actions/learn-github-actions/contexts).
50
+
51
+
For example, the following workflow triggers on the `repository_dispatch` event and uses information from the event payload to build the matrix. When a repository dispatch event is created with a payload like the one below, the matrix `version` variable will have a value of `[12, 14, 16]`. For more information about the `repository_dispatch` trigger, see [AUTOTITLE](/actions/using-workflows/events-that-trigger-workflows#repository_dispatch).
52
+
53
+
```json
54
+
{
55
+
"event_type": "test",
56
+
"client_payload": {
57
+
"versions": [12, 14, 16]
58
+
}
59
+
}
60
+
```
61
+
62
+
```yaml
63
+
on:
64
+
repository_dispatch:
65
+
types:
66
+
- test
67
+
68
+
jobs:
69
+
example_matrix:
70
+
runs-on: ubuntu-latest
71
+
strategy:
72
+
matrix:
73
+
version: {% raw %}${{ github.event.client_payload.versions }}{% endraw %}
74
+
steps:
75
+
- uses: {% data reusables.actions.action-setup-node %}
76
+
with:
77
+
node-version: {% raw %}${{ matrix.version }}{% endraw %}
78
+
```
38
79
39
80
## Expanding or adding matrix configurations
40
81
41
-
{% data reusables.actions.jobs.matrix-include %}
82
+
To expand existing matrix configurations or to add new configurations, use `jobs.<job_id>.strategy.matrix.include`. The value of `include` is a list of objects.
83
+
84
+
For example, consider the following matrix.
85
+
86
+
```yaml
87
+
strategy:
88
+
matrix:
89
+
fruit: [apple, pear]
90
+
animal: [cat, dog]
91
+
include:
92
+
- color: green
93
+
- color: pink
94
+
animal: cat
95
+
- fruit: apple
96
+
shape: circle
97
+
- fruit: banana
98
+
- fruit: banana
99
+
animal: cat
100
+
```
101
+
102
+
This will result in six jobs with the following matrix combinations.
Each `include` entry was applied in the following ways.
112
+
113
+
* `{color: green}` is added to all of the original matrix combinations because it can be added without overwriting any part of the original combinations.
114
+
* `{color: pink, animal: cat}` adds `color:pink` only to the original matrix combinations that include `animal: cat`. This overwrites the `color: green` that was added by the previous `include` entry.
115
+
* `{fruit: apple, shape: circle}` adds `shape: circle` only to the original matrix combinations that include `fruit: apple`.
116
+
* `{fruit: banana}` cannot be added to any original matrix combination without overwriting a value, so it is added as an additional matrix combination.
117
+
* `{fruit: banana, animal: cat}` cannot be added to any original matrix combination without overwriting a value, so it is added as an additional matrix combination. It does not add to the `{fruit: banana}` matrix combination because that combination was not one of the original matrix combinations.
118
+
119
+
For reference and example configurations, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrixinclude).
42
120
43
-
### Example: Expanding configurations
121
+
## Excluding matrix configurations
44
122
45
-
{% data reusables.actions.jobs.matrix-expand-with-include %}
123
+
To remove specific configurations defined in the matrix, use `jobs.<job_id>.strategy.matrix.exclude`.
124
+
125
+
For example, the following workflow will run nine jobs: one job for each of the 12 configurations, minus the one excluded job that matches `{os: macos-latest, version: 12, environment: production}`, and the two excluded jobs that match `{os: windows-latest, version: 16}`.
126
+
127
+
```yaml
128
+
strategy:
129
+
matrix:
130
+
os: [macos-latest, windows-latest]
131
+
version: [12, 14, 16]
132
+
environment: [staging, production]
133
+
exclude:
134
+
- os: macos-latest
135
+
version: 12
136
+
environment: production
137
+
- os: windows-latest
138
+
version: 16
139
+
runs-on: {% raw %}${{ matrix.os }}{% endraw %}
140
+
```
141
+
142
+
For reference information, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrixexclude)
143
+
144
+
## Using an output to define two matrices
145
+
146
+
You can use the output from one job to define matrices for multiple jobs.
147
+
148
+
For example, the following workflow demonstrates how to define a matrix of values in one job, use that matrix in a second jobs to produce artifacts, and then consume those artifacts in a third job. Each artifact is associated with a value from the matrix.
149
+
150
+
```yaml copy
151
+
name: shared matrix
152
+
on:
153
+
push:
154
+
workflow_dispatch:
155
+
156
+
jobs:
157
+
define-matrix:
158
+
runs-on: ubuntu-latest
159
+
160
+
outputs:
161
+
colors: {% raw %}${{ steps.colors.outputs.colors }}{% endraw %}
color: {% raw %}${{ fromJSON(needs.define-matrix.outputs.colors) }}{% endraw %}
175
+
176
+
steps:
177
+
- name: Define Color
178
+
env:
179
+
color: {% raw %}${{ matrix.color }}{% endraw %}
180
+
run: |
181
+
echo "$color" > color
182
+
- name: Produce Artifact
183
+
uses: {% data reusables.actions.action-upload-artifact %}
184
+
with:
185
+
name: {% raw %}${{ matrix.color }}{% endraw %}
186
+
path: color
187
+
188
+
consume-artifacts:
189
+
runs-on: ubuntu-latest
190
+
needs:
191
+
- define-matrix
192
+
- produce-artifacts
193
+
strategy:
194
+
matrix:
195
+
color: {% raw %}${{ fromJSON(needs.define-matrix.outputs.colors) }}{% endraw %}
196
+
197
+
steps:
198
+
- name: Retrieve Artifact
199
+
uses: {% data reusables.actions.action-download-artifact %}
200
+
with:
201
+
name: {% raw %}${{ matrix.color }}{% endraw %}
202
+
203
+
- name: Report Color
204
+
run: |
205
+
cat color
206
+
```
46
207
47
-
### Example: Adding configurations
208
+
## Handling failures
48
209
49
-
{% data reusables.actions.jobs.matrix-add-with-include %}
210
+
To control how job failures are handled, use `jobs.<job_id>.strategy.fail-fast` and `jobs.<job_id>.continue-on-error`.
50
211
51
-
## Excluding matrix configurations
212
+
You can use `jobs.<job_id>.strategy.fail-fast` and `jobs.<job_id>.continue-on-error` together. For example, the following workflow will start four jobs. For each job, `continue-on-error` is determined by the value of `matrix.experimental`. If any of the jobs with `continue-on-error: false` fail, all jobs that are in progress or queued will be cancelled. If the job with `continue-on-error: true` fails, the other jobs will not be affected.
52
213
53
-
{% data reusables.actions.jobs.matrix-exclude %}
214
+
```yaml
215
+
jobs:
216
+
test:
217
+
runs-on: ubuntu-latest
218
+
continue-on-error: {% raw %}${{ matrix.experimental }}{% endraw %}
219
+
strategy:
220
+
fail-fast: true
221
+
matrix:
222
+
version: [6, 7, 8]
223
+
experimental: [false]
224
+
include:
225
+
- version: 9
226
+
experimental: true
227
+
```
54
228
55
-
## Example: Using an output to define two matrices
229
+
For reference information see [`jobs.<job_id>.strategy.fail-fast`](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategyfail-fast) and [`jobs.<job_id>.continue-on-error`](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idcontinue-on-error).
56
230
57
-
{% data reusables.actions.jobs.matrix-used-twice %}
231
+
## Defining the maximum number of concurrent jobs
58
232
59
-
## Handling failures
233
+
To set the maximum number of jobs that can run simultaneously when using a `matrix` job strategy, use `jobs.<job_id>.strategy.max-parallel`.
60
234
61
-
{% data reusables.actions.jobs.section-using-a-build-matrix-for-your-jobs-failfast %}
235
+
For example, the following workflow will run a maximum of two jobs at a time, even if there are runners available to run all six jobs at once.
62
236
63
-
## Defining the maximum number of concurrent jobs
237
+
```yaml
238
+
jobs:
239
+
example_matrix:
240
+
strategy:
241
+
max-parallel: 2
242
+
matrix:
243
+
version: [10, 12, 14]
244
+
os: [ubuntu-latest, windows-latest]
245
+
```
64
246
65
-
{% data reusables.actions.jobs.section-using-a-build-matrix-for-your-jobs-max-parallel %}
247
+
For reference information, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymax-parallel).
Copy file name to clipboardExpand all lines: content/actions/how-tos/writing-workflows/choosing-what-your-workflow-does/using-jobs-in-a-workflow.md
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -28,3 +28,9 @@ redirect_from:
28
28
## Defining prerequisite jobs
29
29
30
30
{% data reusables.actions.jobs.section-using-jobs-in-a-workflow-needs %}
31
+
32
+
## Using a matrix to run jobs with different variables
33
+
34
+
To automatically run a job with different combinations of variables, such as operating systems or language versions, define a `matrix` strategy in your workflow.
35
+
36
+
For more information, see [AUTOTITLE](/actions/how-tos/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow).
0 commit comments