Skip to content

Test build and test jobs parallel with one matrix

ChristopherHX edited this page Nov 10, 2022 · 1 revision

You may want to build and test in different jobs for different platforms, but don't want that the test job waits for building unrelated platforms. This also make it easy to pass output variables from build job to the test job of the same configuration, because these jobs are executed as single jobs with a scoped needs context.

.github/workflows/main.yml

on: push
name: build and test
jobs:
  build-and-test:
    strategy:
      matrix:
        os:
        - windows-latest
        - ubuntu-latest
        - macos-latest
    uses: ./.github/workflows/build-and-test.yml
    with:
      build-config: ${{ tojson(matrix) }}

.github/workflows/build-and-test.yml

on:
  workflow_call:
    inputs:
      build-config:
        type: string
jobs:
  build:
    strategy:
      matrix:
        include:
        - ${{ fromjson(inputs.build-config) }}
    runs-on: ${{ matrix.os }}
    steps:
    - run: echo "$MATRIX"
      env:
        MATRIX: ${{ tojson(matrix) }}
  test:
    needs: build
    strategy:
      matrix:
        include:
        - ${{ fromjson(inputs.build-config) }}
    runs-on: ${{ matrix.os }}
    steps:
    - run: echo "$MATRIX"
      env:
        MATRIX: ${{ tojson(matrix) }}

This example imports the global matrix configuration into the build and test jobs, you can access the global matrix values of the parent job in the build and test jobs.

The test job for ubuntu-latest only depends on the build job for ubuntu-latest and are run truly parallel with static needs.