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?

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.