Possible to matrix exclude on different GitHub Actions events?

If I currently test my project on the Python 3 runtimes on both push and pull_request events (and a schedule event)

name: CI/CD

on:
  push:
  pull_request:
  # Run daily at 0:01 UTC
  schedule:
  - cron:  '1 0 * * *'

jobs:
  test:

    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: [3.6, 3.7, 3.8]

is it possible (in an effort to speed up CI/reduce wasted cycles) to use the exclude strategy to only run Python 3.8 on push events, but then on pull_request events run over Python 3.6, 3.7, and 3.8?

@matthewfeickert,

In a workflow, if you want to skip a job according to the events, generally you need to use the if conditional (jobs.<job_id>.if) to do that.

Ideally, the configurations below should work in the workflow.

jobs:
  test_matrix:
    name: Test matrix
    runs-on: ${{ matrix.os }}
    if: ${{ github.event_name == matrix.event-name }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: [3.6, 3.7, 3.8]
        event-name: [pull_request]
        include:
          - os: ubuntu-latest
            python-version: 3.8
            event-name: push
          - os: macos-latest
            python-version: 3.8
            event-name: push
    steps:
      . . .

But just like the env context, there is a restriction that the matrix context also can’t be used in the job’s if conditional.
So, the above configurations actually do not work.

There are two workarounds, maybe you can consider:

  1. Setup multiple matrix for different events.
jobs:
  test_matrix_pr:
    name: Test matrix for pull_request
    if: ${{ github.event_name == pull_request }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: [3.6, 3.7, 3.8]
    steps:
      . . .

  test_matrix_push:
    name: Test matrix for push
    if: ${{ github.event_name == push }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: [3.8]
    steps:
      . . .
  1. Setup a single matrix, and add the if conditional on each step (jobs.<job_id>.steps.if).
jobs:
  test_matrix:
    name: Test matrix
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: [3.6, 3.7, 3.8]
        event-name: [pull_request]
        include:
          - os: ubuntu-latest
            python-version: 3.8
            event-name: push
          - os: macos-latest
            python-version: 3.8
            event-name: push
    steps:
      - name: step 1
        if: ${{ github.event_name == matrix.event-name }}

      - name: step 2
        if: ${{ github.event_name == matrix.event-name }}

      . . . 

      - name: step N
        if: ${{ github.event_name == matrix.event-name }}