I have a GitHub Actions workflow file, which allows a manual trigger using the workflow_dispatch
event. GitHub offers a drop-down list to choose on which branch to run the workflow.
I was wondering if there is any way to limit that option to a specific branch (or specific branches).
Example of a workflow file with a workflow_dispatch
:
name: A test workflow title
on:
push:
workflow_dispatch:
# branches: # *** This seems to not be supported! ***
# - main
jobs:
print-hello-world:
runs-on: ubuntu-20.04
steps:
- name: Print hello world
run: echo "Hello world!"
2
Answers
Using
if
you can add a step to fail a job if the workflow was triggered byworkflow_dispatch
on specific branches.It's also possible to skip the job by placing the negative condition in the job-level:
A simple way to do this is by using Environment Protection Rules.
production
by going to Settings > Environmentsproduction
, you will find the topmost section ofDeployment Branches
on clicking onproduction
in the list of environments.All Branches
. You can select eitherProtected branches
orSelected branches
with a matching pattern.Add this environment to your workflow file under jobs like:
References:
https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#environment-protection-rules
https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#creating-an-environment