run step on (if branch/tag is)

Currently, I do not believe there is a way to run a step (or even a job) if a branch matches a certain pattern. It is only possible to run an entire workflow on a master/tagged branch, but this requires duplicating the workflow just to add another step for releasing/publishing. I could also create an action for the build/test steps, but both creating an action and duplicating the code seem superfulous and more likely to cause errors since someone could forget that both files need to be edited.

A common example of this would be:

  • Checkout

  • Run tests/whatever

  • IF on master/tagged branch THEN release/publish code.

Am i missing something and there is an easy way to do this?

I know i can do:

if: {{ github.ref == 'ref/head/master' }}

but that only works when you know the branch/tag exactly and something like (with pattern):

if: {{ github.ref == 'ref/head/v*' }}

does not work.

Right now the best approach I can think of is to create some type of conditional-step-action that will allow you to input your if condition and then run the step if the branch/tag condition matches, but exit without failing if it does not (so as not to fail the job).

1 Like

Hi @bhowell2,

First, you do not need to use conditional syntax with the “if” statement. See the following excerpt from: "About context and expressions"

When you use expressions in an if conditional, you do not need to use the expression syntax ({{ }}) because GitHub automatically evaluates the if conditional as an expression.

As for pattern matching with the if statement, I suggest using the contains function. Here’s an example:

if: contains(github.ref, "release")
 - run: echo "I only run if the branch has release in its name!"

@bradenmthanks for the response (and noted on the if expression!). Contains does not do pattern matching, but it could definitely work. Using contains would require people to name their release tags something that is unlikely to occur elsewhere (luckily tags are generally for some type of releases anyway). As you put it “release” is a pretty good indicator that the tag is for a release. Many currently use something more of the form “v*” or maybe just a semantic versioned number (e.g. 1.0.1), but probably wouldn’t be a huge deal for people to adjust this by adding “release”, “milestone” or whatever in the tag. This would be much less of a problem anyway if workflows could be reused/referenced which would be preferred to using the IF condition at all.


You can use bash substitutions:


name: Extract Branch Name
run: echo "::set-env name=BRANCH::$(echo ${GITHUB_REF##*/})"

would strip refs/heads and give you “master” 

You should be able to use also RegExp here.

Also see