Paul Razvan Berg
Paul Razvan Berg

Reputation: 21378

GitHub Actions run on push to all branches

It's easy to run a GitHub Action on any push or pull request:

# Triggers the workflow on push or pull request events
on: [push, pull_request]

But what if I want to restrict runs to pull requests opened against specific base refs, while allowing runs on all branches?

I thought about this:

on:
  push:
    branches:
      - "*"
  pull_request:
    branches:
      - "develop"
      - "staging"

But it didn't work. I added this Action to a feature branch and GitHub didn't pick it up.

Is there anything wrong with my glob? Why doesn't '*' work?

Upvotes: 58

Views: 47576

Answers (2)

Benjamin W.
Benjamin W.

Reputation: 52112

Dropping the restrictions on pushes entirely means triggering on all of them (commits, and also tags):

on:
  push:
  pull_request:
    branches:
      - develop
      - staging

jobs:
  print:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Running!"

Upvotes: 34

Paul Razvan Berg
Paul Razvan Berg

Reputation: 21378

I found the Filter pattern cheat sheet after posting the question:

'*': Matches all branch and tag names that don't contain a slash (/). The * character is a special character in YAML. When you start a pattern > with *, you must use quotes.

'**': Matches all branch and tag names. This is the default behavior when you don't use a branches or tags filter.

It happened that the branch I was testing contained a slash (/), so one asterisk (*) wasn't enough. I switched to two asterisks (**) and it works now.

Upvotes: 63

Related Questions