Skip to content
Latchkey

How to Enforce Branch Naming Conventions With Automation

A branch naming convention (feature/, fix/, hotfix/, release/) lets CI route jobs by prefix and reject off-pattern branches automatically.

Consistent prefixes make branch intent machine-readable: CI can deploy previews for feature/*, fast-track hotfix/*, and build candidates on release/*. Enforce the pattern on pull_request so non-conforming branches fail early.

Steps

  • Agree on a prefix set, for example feature/, fix/, hotfix/, release/, chore/.
  • Validate the source branch name on pull_request.
  • Fail the check when the name does not match the pattern.
  • Reuse the parsed prefix to drive downstream routing and labels.

Workflow

.github/workflows/branch-name.yml
on:
  pull_request:
    types: [opened, edited, synchronize]
jobs:
  branch-name:
    runs-on: ubuntu-latest
    steps:
      - name: Validate branch name
        run: |
          BRANCH="${{ github.head_ref }}"
          if ! echo "$BRANCH" | grep -Eq '^(feature|fix|hotfix|release|chore)/[a-z0-9._-]+$'; then
            echo "Branch '$BRANCH' does not match the naming convention" >&2
            exit 1
          fi

Gotchas

  • Use github.head_ref for the source branch on pull_request; github.ref is the merge ref.
  • Keep the pattern documented, or contributors will keep tripping the check.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →