Claus Appel
Claus Appel

Reputation: 1565

How to get a GitHub Action step to run only on branches with a particular name prefix?

I am writing a GitHub Action. I want some of my steps to run only on certain branches.

The whole action is set to run only on master and on branches beginning with features/lr.

on:
  push:
    branches: 
      - master
      - features/lr*

I have a "deploy" step that I want to run on master and on branches beginning with features/lrd. (So for example if my branch is named features/lr-foo, then the deployment step should be skipped.)

I know I can do if conditionals like this:

- name: Deploy application
  if: github.ref == 'refs/heads/master'

Can I also check whether github.ref matches a certain prefix or pattern? What is the syntax for that?

Something like this pseudocode:

- name: Deploy application
  if: github.ref == 'refs/heads/master' || github.ref.matches('refs/heads/lrd*')

Thanks in advance!

Upvotes: 3

Views: 3000

Answers (2)

Claus Appel
Claus Appel

Reputation: 1565

Inspired by the answer from frennky, I ended up doing the following in my step, which is ugly but works:

- name: Deploy application
  if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/features/lrd')

Upvotes: 2

frennky
frennky

Reputation: 13934

The branches, branches-ignore, tags, and tags-ignore keywords accept glob patterns. You can check details in docs - filter pattern.

As for using expressions, docs don't mention matches function, but maybe you could use something like contains, startsWith or endsWith. See here for details.

Upvotes: 2

Related Questions