Using Jobs Across Workflows/Conditional Steps Based on Branch?

I have a series of jobs that I need to run under different circumstances, and I’d like to reuse code as much as possible.

  • setup: this must run before any other job and should run on every branch/pull request, it simply sets up Python, restores a cache, creates a virtualenv, and downloads dependencies.
  • test: this must run after setup but should only on pull requests
  • deploy: this must run only on master after setup and test

I’ve been struggling with the jobs syntax to understand how to run certain steps only on certain branch conditions. I tried to split these out into their own workflows and reference jobs across workflows, but that has not worked and has broken the build. I created .github/workflows/{deploy,setup,test}.yml, but it errors when I try to reference steps across files.

Since the definitions for when to run a workflow are defined at the top level, e.g.:

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
  workflow_dispatch:

How can I then make further decisions on when to run certain actions? As mentioned in the beginning, the setup step is always required, test should run after setup but only on pull requests, and deploy should only run on master.

I’m kind of confused since on is a top-level config value at the workflow level, and I can’t seem to find a way to only run certain steps on certain conditions.

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif
https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsif
https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions