Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to restrict what branch an action can be run on?

When running a github action manually, I can choose which branch to run it against. This seems like a bad idea for some actions. Especially actions along the lines of DeployToProduction - where I only ever want that to be run against the main branch.

Is there any way to restrict it to only run against main - while still making the action manually-triggered?

An example of a manually-triggered action that I have might look something like this:

---
name: DeployToStaging
on:
  workflow_dispatch:
jobs: 
  ...
like image 900
davidpricedev Avatar asked Nov 17 '25 08:11

davidpricedev


2 Answers

I had the same issue and I solved it with a condition:

if: github.ref == 'refs/heads/master'

steps:
...

in this way the user can see that the action is skipped when selecting a different branch.

like image 119
JaredEzz Avatar answered Nov 19 '25 10:11

JaredEzz


Two alternative ways to check for this with an if condition without having to hard-code branch names include:

# Only run for the default branch
if: github.ref_name == github.event.repository.default_branch

# Older syntax for GitHub Enterprise Server where ref_name isn't available
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch)

and:

# Only run for protected branches
if: github.ref_protected == true
like image 36
Martin Costello Avatar answered Nov 19 '25 09:11

Martin Costello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!