Skip to content

Commit fb4ce08

Browse files
committed
Added README
1 parent 90c21f6 commit fb4ce08

File tree

2 files changed

+223
-15
lines changed

2 files changed

+223
-15
lines changed

README.yaml

Lines changed: 222 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66

77
# Name of this project
8-
name: example-github-action-composite
8+
name: github-action-matrix-outputs-write
99

1010
# Tags of this project
1111
tags:
@@ -18,25 +18,34 @@ tags:
1818
license: "APACHE2"
1919

2020
# Canonical GitHub repo
21-
github_repo: cloudposse/example-github-action-composite
21+
github_repo: cloudposse/github-action-matrix-outputs-write
2222

2323
# Badges to display
2424
badges:
2525
- 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"
2828
- name: "Slack Community"
2929
image: "https://slack.cloudposse.com/badge.svg"
3030
url: "https://slack.cloudposse.com"
3131

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"
3336

3437
# 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
3639

3740
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)
4049
4150
references:
4251
- name: "github-actions-workflows"
@@ -48,6 +57,8 @@ references:
4857

4958
# How to use this project
5059
usage: |-
60+
Example how you can use workaround to reference matrix job outputs.
61+
5162
```yaml
5263
name: Pull Request
5364
on:
@@ -56,17 +67,214 @@ usage: |-
5667
types: [opened, synchronize, reopened, closed, labeled, unlabeled]
5768
5869
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:
60100
runs-on: ubuntu-latest
101+
needs: [build]
61102
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
65105
with:
66-
param1: true
106+
matrix-step-name: build
67107
68108
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 }}
70278
```
71279
72280
include:

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: 'Matrix outputs write'
1+
name: 'Matrix outputs - write'
22
description: 'Write outputs for matrix'
33
44
branding:

0 commit comments

Comments
 (0)