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:
...
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With