5
5
#
6
6
7
7
# Name of this project
8
- name : example- github-action-composite
8
+ name : github-action-matrix-outputs-write
9
9
10
10
# Tags of this project
11
11
tags :
@@ -18,25 +18,34 @@ tags:
18
18
license : " APACHE2"
19
19
20
20
# Canonical GitHub repo
21
- github_repo : cloudposse/example- github-action-composite
21
+ github_repo : cloudposse/github-action-matrix-outputs-write
22
22
23
23
# Badges to display
24
24
badges :
25
25
- name : " Latest Release"
26
- image : " https://img.shields.io/github/release/cloudposse/example- github-action-composite .svg"
27
- url : " https://github.com/cloudposse/example- github-action-composite /releases/latest"
26
+ image : " https://img.shields.io/github/release/cloudposse/github-action-matrix-outputs-write .svg"
27
+ url : " https://github.com/cloudposse/github-action-matrix-outputs-write /releases/latest"
28
28
- name : " Slack Community"
29
29
image : " https://slack.cloudposse.com/badge.svg"
30
30
url : " https://slack.cloudposse.com"
31
31
32
- related : []
32
+ related :
33
+ - name : " github-action-matrix-outputs-read"
34
+ description : " Matrix outputs read"
35
+ url : " https://github.com/cloudposse/github-action-matrix-outputs-write"
33
36
34
37
# Short description of this project
35
- description : Template repository of composite GitHub Action
38
+ description : [Workaround implementation](https://github.com/community/community/discussions/17245#discussioncomment-3814009) - Write matrix jobs outputs
36
39
37
40
introduction : |-
38
- This is template repository to create composite GitHub Actions.
39
- Feel free to use it as reference and starting point.
41
+ GitHub actions have an [Jobs need a way to reference all outputs of matrix jobs](https://github.com/community/community/discussions/17245) issue.
42
+ If there is a job that runs multiple times with `strategy.matrix` only the latest iteration's output availiable for
43
+ reference in other jobs.
44
+
45
+ There is a [workaround](https://github.com/community/community/discussions/17245#discussioncomment-3814009) to address the limitation.
46
+ We implement the workaround with two GitHub Actions:
47
+ * [Matrix Outputs Write](https://github.com/cloudposse/github-action-matrix-outputs-write)
48
+ * [Matrix Outputs Read](https://github.com/cloudposse/github-action-matrix-outputs-read)
40
49
41
50
references :
42
51
- name : " github-actions-workflows"
@@ -48,6 +57,8 @@ references:
48
57
49
58
# How to use this project
50
59
usage : |-
60
+ Example how you can use workaround to reference matrix job outputs.
61
+
51
62
```yaml
52
63
name: Pull Request
53
64
on:
@@ -56,17 +67,214 @@ usage: |-
56
67
types: [opened, synchronize, reopened, closed, labeled, unlabeled]
57
68
58
69
jobs:
59
- context:
70
+ build:
71
+ runs-on: ubuntu-latest
72
+ strategy:
73
+ matrix:
74
+ platform: ["i386", "arm64v8"]
75
+ steps:
76
+ - name: Checkout
77
+ uses: actions/checkout@v3
78
+
79
+ - name: Build
80
+ id: build
81
+ uses: cloudposse/[email protected]
82
+ with:
83
+ registry: registry.hub.docker.com
84
+ organization: "${{ github.event.repository.owner.login }}"
85
+ repository: "${{ github.event.repository.name }}"
86
+ build-args: |-
87
+ PLATFORM=${{ matrix.platform }}
88
+
89
+ ## Write for matrix outputs workaround
90
+ - uses: cloudposse/github-action-matrix-outputs-write@main
91
+ id: out
92
+ with:
93
+ matrix-step-name: ${{ github.job }}
94
+ matrix-key: ${{ matrix.platform }}
95
+ outputs: |-
96
+ image: ${{ steps.build.outputs.image }}:${{ steps.build.outputs.tag }}
97
+
98
+ ## Read matrix outputs
99
+ read:
60
100
runs-on: ubuntu-latest
101
+ needs: [build]
61
102
steps:
62
- - name: Example action
63
- uses: cloudposse/example-github-action-composite@main
64
- id: example
103
+ - uses: cloudposse/github-action-matrix-outputs-read@main
104
+ id: read
65
105
with:
66
- param1: true
106
+ matrix-step-name: build
67
107
68
108
outputs:
69
- result: ${{ steps.example.outputs.result1 }}
109
+ result: "${{ steps.read.outputs.result }}"
110
+
111
+ ## This how you can reference matrix output
112
+ assert:
113
+ runs-on: ubuntu-latest
114
+ needs: [read]
115
+ steps:
116
+ - uses: nick-fields/assert-action@v1
117
+ with:
118
+ expected: ${{ registry.hub.docker.com }}/${{ github.event.repository.owner.login }}/${{ github.event.repository.name }}:i386
119
+ ## This how you can reference matrix output
120
+ actual: ${{ fromJson(needs.read.outputs.result).image.i386 }}
121
+
122
+ - uses: nick-fields/assert-action@v1
123
+ with:
124
+ expected: ${{ registry.hub.docker.com }}/${{ github.event.repository.owner.login }}/${{ github.event.repository.name }}:arm64v8
125
+ ## This how you can reference matrix output
126
+ actual: ${{ fromJson(needs.read.outputs.result).image.arm64v8 }}
127
+ ```
128
+
129
+ ### Reusable workflow example
130
+
131
+ Reusable workflow that support matrix outputs
132
+ `./.github/workflow/build-reusabled.yaml`
133
+ ```yaml
134
+ name: Build - Reusable workflow
135
+ on:
136
+ workflow_call:
137
+ inputs:
138
+ registry:
139
+ required: true
140
+ type: string
141
+ organization:
142
+ required: true
143
+ type: string
144
+ repository:
145
+ required: true
146
+ type: string
147
+ platform:
148
+ required: true
149
+ type: string
150
+ matrix-step-name:
151
+ required: false
152
+ type: string
153
+ matrix-key:
154
+ required: false
155
+ type: string
156
+ outputs:
157
+ image:
158
+ description: "Image"
159
+ value: ${{ jobs.write.outputs.image }}
160
+
161
+ jobs:
162
+ build:
163
+ runs-on: ubuntu-latest
164
+ steps:
165
+ - name: Checkout
166
+ uses: actions/checkout@v3
167
+
168
+ - name: Build
169
+ id: build
170
+ uses: cloudposse/[email protected]
171
+ with:
172
+ registry: ${{ inputs.registry }}
173
+ organization: ${{ inputs.organization }}
174
+ repository: ${{ inputs.repository }}
175
+ build-args: |-
176
+ PLATFORM=${{ inputs.platform }}
177
+ outputs:
178
+ image: ${{ needs.build.outputs.image }}:${{ needs.build.outputs.tag }}
179
+
180
+ write:
181
+ runs-on: ubuntu-latest
182
+ needs: [build]
183
+ steps:
184
+ ## Write for matrix outputs workaround
185
+ - uses: cloudposse/github-action-matrix-outputs-write@main
186
+ id: out
187
+ with:
188
+ matrix-step-name: ${{ inputs.matrix-step-name }}
189
+ matrix-key: ${{ inputs.matrix-key }}
190
+ outputs: |-
191
+ image: ${{ needs.build.outputs.image }}
192
+
193
+ outputs:
194
+ image: ${{ fromJson(steps.out.outputs.result).image }}
195
+ ```
196
+
197
+ Then you can use the workflow with matrix
198
+ ```
199
+ name: Pull Request
200
+ on:
201
+ pull_request:
202
+ branches: [ 'main' ]
203
+ types: [opened, synchronize, reopened, closed, labeled, unlabeled]
204
+
205
+ jobs:
206
+ build:
207
+ usage: ./.github/workflow/build-reusabled.yaml
208
+ strategy:
209
+ matrix:
210
+ platform: ["i386", "arm64v8"]
211
+ with:
212
+ registry: registry.hub.docker.com
213
+ organization: "${{ github.event.repository.owner.login }}"
214
+ repository: "${{ github.event.repository.name }}"
215
+ platform: ${{ matrix.platform }}
216
+ matrix-step-name: ${{ github.job }}
217
+ matrix-key: ${{ matrix.platform }}
218
+
219
+ ## Read matrix outputs
220
+ read:
221
+ runs-on: ubuntu-latest
222
+ needs: [build]
223
+ steps:
224
+ - uses: cloudposse/github-action-matrix-outputs-read@main
225
+ id: read
226
+ with:
227
+ matrix-step-name: build
228
+
229
+ outputs:
230
+ result: "${{ steps.read.outputs.result }}"
231
+
232
+ ## This how you can reference matrix output
233
+ assert:
234
+ runs-on: ubuntu-latest
235
+ needs: [read]
236
+ steps:
237
+ - uses: nick-fields/assert-action@v1
238
+ with:
239
+ expected: ${{ registry.hub.docker.com }}/${{ github.event.repository.owner.login }}/${{ github.event.repository.name }}:i386
240
+ ## This how you can reference matrix output
241
+ actual: ${{ fromJson(needs.read.outputs.result).image.i386 }}
242
+
243
+ - uses: nick-fields/assert-action@v1
244
+ with:
245
+ expected: ${{ registry.hub.docker.com }}/${{ github.event.repository.owner.login }}/${{ github.event.repository.name }}:arm64v8
246
+ ## This how you can reference matrix output
247
+ actual: ${{ fromJson(needs.read.outputs.result).image.arm64v8 }}
248
+ ```
249
+
250
+ or as a simple job
251
+
252
+ ```yaml
253
+ name: Pull Request
254
+ on:
255
+ pull_request:
256
+ branches: [ 'main' ]
257
+ types: [opened, synchronize, reopened, closed, labeled, unlabeled]
258
+
259
+ jobs:
260
+ build:
261
+ usage: ./.github/workflow/build-reusabled.yaml
262
+ with:
263
+ registry: registry.hub.docker.com
264
+ organization: "${{ github.event.repository.owner.login }}"
265
+ repository: "${{ github.event.repository.name }}"
266
+ platform: "i386"
267
+
268
+ ## This how you can reference single job output
269
+ assert:
270
+ runs-on: ubuntu-latest
271
+ needs: [build]
272
+ steps:
273
+ - uses: nick-fields/assert-action@v1
274
+ with:
275
+ expected: ${{ registry.hub.docker.com }}/${{ github.event.repository.owner.login }}/${{ github.event.repository.name }}:i386
276
+ ## This how you can reference matrix output
277
+ actual: ${{ needs.build.outputs.image }}
70
278
```
71
279
72
280
include :
0 commit comments