GitHub Actions: how to target all branches EXCEPT master?

GitGithubGithub Actions

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

There is now a branches-ignore option:

on:
  push:
    branches-ignore:
      - master

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestbranchestags

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionfweidemann14View Question on Stackoverflow
Solution 1 - GitElectric SheepView Answer on Stackoverflow
Solution 2 - GitSamyView Answer on Stackoverflow