Checkout action depending on pull_request or push


I would like to be able to checkout the branch that is merging into master, instead of master, when the checkout action is ran.
So I looked it up and found this in the docs.

Now I would like to not need to recreate the whole flow when the triggered event is a push in the master branch instead of a pull request…

So is there a way I can add an “IF” statement based on whether it is running on “push” or “pull_request” ?

My current piece of code as follows:

    branches: [ master ]
    branches: [ master, staging ]

    runs-on: ubuntu-latest

    - uses: actions/checkout@v2
        ref: ${{ github.event.pull_request.head.sha }}
    - name: Set up Python 3.6
      uses: actions/setup-python@v2
        python-version: 3.6

Yes, you can do quite fancy magical things, here’s a workflow that I use:

you can see that it decides how to perform a checkout based on the event.

In theory, I could probably use a combined step instead of using multiple conditional steps, but the logic is clear enough this way and it means someone can see at a glance if it is checking out a normal branch, or a merge.