github actions exclude pull requests from a branch

Viewed 2307

Say I have a workflow that runs on every PR to master which starts with:

on:
  pull_request:
    branches:
      - master

I'd like to skip all jobs if the PR is coming from the depbot branch. Something like:

on:
  pull_request:
    branches:
      - master
    head_ref-ignore:
      - depbot

I think you can skip all the steps (one at a time) using

 if: startsWith(github.head_ref, 'depbot') == false

But it is not what I want, as it would still initiate the job. How can I achieve that at the launch level?

2 Answers

But it is not what I want, as it would still initiate the job.

That means you would need a "gatekeeper" job which would be initiated (and check github.head_ref), and, through job dependency, would call the second one only if the right condition is fulfilled.

But the point is: you need at least one job to start, in order to check a condition.

According to the documentation, there are at least 2 ways of doing it at the workflow level:

on:
  pull_request:
    branches:    
      - 'master'    # matches refs/heads/master
      - '!depbot'   # excludes refs/heads/depbot

Or

on:
  pull_request:
    branches-ignore:    
      - 'depbot'   # ignore refs/heads/depbot

Not that you cannot use both the branches and branches-ignore filters for the same event in a workflow. Use the branches filter when you need to filter branches for positive matches and exclude branches. Use the branches-ignore filter when you only need to exclude branch names.

Related