github-actions

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

Solution

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