From f85d8f247c84ba490e6e8ce49e83d6bcf1512815 Mon Sep 17 00:00:00 2001 From: Josh Soref <2119212+jsoref@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:40:00 -0400 Subject: [PATCH] Provide example of using an output to define two matrixes (#33502) --- .../using-a-matrix-for-your-jobs.md | 4 ++ .../actions/jobs/matrix-used-twice.md | 67 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 data/reusables/actions/jobs/matrix-used-twice.md diff --git a/content/actions/using-jobs/using-a-matrix-for-your-jobs.md b/content/actions/using-jobs/using-a-matrix-for-your-jobs.md index a60a4ad8d3..5312977ab1 100644 --- a/content/actions/using-jobs/using-a-matrix-for-your-jobs.md +++ b/content/actions/using-jobs/using-a-matrix-for-your-jobs.md @@ -48,6 +48,10 @@ redirect_from: {% data reusables.actions.jobs.matrix-exclude %} +## Example: Using an output to define two matrices + +{% data reusables.actions.jobs.matrix-used-twice %} + ## Handling failures {% data reusables.actions.jobs.section-using-a-build-matrix-for-your-jobs-failfast %} diff --git a/data/reusables/actions/jobs/matrix-used-twice.md b/data/reusables/actions/jobs/matrix-used-twice.md new file mode 100644 index 0000000000..9815147444 --- /dev/null +++ b/data/reusables/actions/jobs/matrix-used-twice.md @@ -0,0 +1,67 @@ +You can use the output from one job to define matrices for multiple jobs. + +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. + +```yaml copy +name: shared matrix +on: + push: + workflow_dispatch: + +jobs: + define-matrix: + runs-on: ubuntu-latest + + outputs: + colors: ${{ steps.colors.outputs.colors }} + + steps: + - name: Define Colors + id: colors + run: | + echo 'colors=["red", "green", "blue"]' >> "$GITHUB_OUTPUT" + + produce-artifacts: + runs-on: ubuntu-latest + needs: define-matrix + strategy: + matrix: +{% raw %} + color: ${{ fromJSON(needs.define-matrix.outputs.colors) }} +{% endraw %} + + steps: + - name: Define Color + env: + color: ${{ matrix.color }} + run: | + echo "$color" > color + - name: Produce Artifact + uses: {% data reusables.actions.action-upload-artifact %} +{% raw %} + with: + name: ${{ matrix.color }} + path: color + + consume-artifacts: + runs-on: ubuntu-latest + needs: + - define-matrix + - produce-artifacts + strategy: + matrix: + color: ${{ fromJSON(needs.define-matrix.outputs.colors) }} + + steps: + - name: Retrieve Artifact +{% endraw %} + uses: {% data reusables.actions.action-download-artifact %} +{% raw %} + with: + name: ${{ matrix.color }} + + - name: Report Color + run: | + cat color +{% endraw %} +```