I'm looking for methods to protect main branch by preventing branches except dev branch from pull request merge to main branch. But I could not find a way to it. Is there any way for it? or is it possible using Github Action?
I took Rob Bos's advice and created a workflow to do this, here is what I have in my enforcer.yaml file
name: 'Check Branch'
on:
pull_request:
jobs:
check_branch:
runs-on: ubuntu-latest
steps:
- name: Check branch
if: github.base_ref == 'main' && github.head_ref != 'dev'
run: |
echo "ERROR: You can only merge to main from dev."
exit 1
Then I have the workflow check_branch as a required status check in the branch protection rule for my main branch. Attempting a pull request into main from any branch other than dev fails.
That's not possible out of the box. You can write a workflow that runs on a PR and fails if the source branch is not dev. Then add this workflow as a required check in the branch protection rule for the main branch.
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