112

I want to be able to let an action run on any given branch except master. I am aware that there is a prebuilt filter action, but I want the exact opposite.

More like GitLab's except keyword. Since this is not inside the official docs, has anyone prepared a decent workaround?

Thank you very much.

fweidemann14
  • 1,714
  • 3
  • 13
  • 20
  • Their documentation seems to imply that you might be able to do this using something like: `on: push: branches: - '!master'` https://help.github.com/en/articles/workflow-syntax-for-github-actions#on But I haven't been able to get similar negated patterns to work. – Electric Sheep Aug 30 '19 at 08:32
  • Thank you Ollie, sadly this doesn't seem to trigger the pipeline... – fweidemann14 Aug 31 '19 at 14:35

2 Answers2

190

Update: There is a newer filter described in Samy's answer that provides a more succinct way of achieving this.


The documentation has been updated with more information now:

When you specify a branches or tags filter, the workflow only runs if at least one pattern matches. Any changes to branches or tags that don't match a defined pattern will not trigger a workflow. The order that you define patterns matters:

  • A matching negative pattern after a positive match will exclude the ref again.
  • A matching positive pattern after a negative match will include the ref again.

So in order to exclude master, you need to ensure that a pattern matching everything is included first:

on:
  push:
    branches:    
      - '*'         # matches every branch that doesn't contain a '/'
      - '*/*'       # matches every branch containing a single '/'
      - '**'        # matches every branch
      - '!master'   # excludes master
Electric Sheep
  • 3,867
  • 1
  • 29
  • 38
  • 1
    \*/\* only matches branches containing exactly one / – wrymug Sep 23 '19 at 18:50
  • @ wrymug Thanks, that was what i meant - I will edit the answer to make it clearer. – Electric Sheep Sep 24 '19 at 07:57
  • Thanks for updating. It’s too bad GitHub Actions doesn’t seem to provide a true wildcard pattern. – wrymug Sep 26 '19 at 19:26
  • 4
    @wrymug and others not sure if this is a new feature or not, but `'**'` will match every branch. See [the docs](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet) for more info. – Dylan Nissley Feb 19 '20 at 23:49
145

There is now a branches-ignore option:

on:
  push:
    branches-ignore:
      - master

You cannot branches-ignore and branches in the same workflow.

Paul Wintz
  • 2,542
  • 1
  • 19
  • 33
Samy
  • 1,651
  • 1
  • 11
  • 12
  • branches-ignore is not working for me. `on: push: branches: - main - release pull_request: branches-ignore: - release ` – Vaulstein Jan 17 '22 at 13:54
  • 5
    @Vaulstein `branches-ignore` and `branches` cannot use at the same time on a event. – JSKIM Jan 26 '22 at 00:21