Skip to content

Enforce Branch Creation Rules #3

Enforce Branch Creation Rules

Enforce Branch Creation Rules #3

name: Enforce Branch Creation Rules
on:
create:
branches:
- "**"
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Check Branch Rules
run: |
branch_name="${{ github.ref_name }}"
if [[ "$branch_name" =~ ^hotfix/ ]]; then
base_branch=$(git merge-base HEAD origin/main)
main_commit=$(git rev-parse origin/main)
if [[ "$base_branch" != "$main_commit" ]]; then
echo "Error: Hotfix branches must be created from 'main'."
exit 1
fi
elif [[ "$branch_name" =~ ^issue/ ]]; then
base_branch=$(git merge-base HEAD origin/main origin/dev)
main_commit=$(git rev-parse origin/main)
dev_commit=$(git rev-parse origin/dev)
if [[ "$base_branch" != "$main_commit" && "$base_branch" != "$dev_commit" ]]; then
echo "Error: Issue branches must be created from 'main' or 'dev'."
exit 1
fi
elif [[ "$branch_name" =~ ^feature/ ]]; then
base_branch=$(git merge-base HEAD origin/dev)
dev_commit=$(git rev-parse origin/dev)
if [[ "$base_branch" != "$dev_commit" ]]; then
echo "Error: Feature branches must be created from 'dev'."
exit 1
fi
fi