Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 3

run step on (if branch/tag is)

Solved! Go to Solution.

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).

2 Replies
Solution
Copilot Lvl 2
Message 2 of 3

Re: run step on (if branch/tag is)

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")
steps:
 - run: echo "I only run if the branch has release in its name!"
Copilot Lvl 3
Message 3 of 3

Re: run step on (if branch/tag is)

@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.