Can I share strategy matrices between multiple jobs?

I’ve just discovered strategy matrices which I’m using to test and build on multiple versions of python:

name: CI

on: [push]

# Largely based upon https://docs.github.com/en/actions/guides/building-and-testing-python
jobs:

  super-lint:
    name: Lint Code Base
    runs-on: [ ubuntu-latest ]
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Run Super-Linter
        uses: github/super-linter@v4
        env:
          VALIDATE_PYTHON_ISORT: false

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.8, 3.9]
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          make init
      - name: Test with pytest
        run: |
          make test

  build:
    runs-on: ubuntu-latest
    needs: [
      test
    ]
    strategy:
      matrix:
        python-version: [3.8, 3.9]
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          make init
      - name: Build
        run: |
          make build

As you can see I’ve got python-version: [3.8, 3.9] defined in more than one place. Is there a way to share a matrix with multiple jobs?

1 Like

Hey @jamiet-msm! No, there is no good way to share a matrix with multiple jobs. Matrixes are defined in a job, to allow that job to spawn multiple versions of itself, as you have shown above. There is no matrix keyword defined at a higher level, to allow you to set it for multiple jobs.

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix

Righto, thx @mickeygousset . Was hoping I could do it with an environment variables but I believe I’m right in saying that env vars can’t contain a list.

:+1: Correct. Environment variables won’t help you here, I don’t think.

Not supported at the moment, what a work around could do is to set a env for those python versions and you can use the same, declaring in one place.

Yes.

You can generate an output and then have both matrixes use the output (probably via fromJSON(...).

It’s a little late, but I’d like to set the record straight here. You absolutely can define reusable matrices! An approach I use is:

  1. You have an initial job that creates your matrix (or multiple matrices) as outputs
  2. Other jobs then need that job, e.g.
build:
  needs: [ createMatrices ]
  strategy:
    matrix: ${{ fromJSON(needs.createMatrices.outputs.pythonVersion) }}
1 Like