Branch Naming Convention Check #118
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://stackoverflow.com/questions/79382491/how-to-proactively-enforce-branch-naming-convention-in-github | |
| name: Branch Naming Convention Check | |
| on: | |
| create: | |
| branches: | |
| - "**" | |
| permissions: | |
| contents: read | |
| jobs: | |
| branch-naming-rules: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check branch name | |
| run: | | |
| # Strip refs/heads/ for branches OR refs/tags/ for tags | |
| if [[ $GITHUB_REF == refs/tags/* ]]; then | |
| BRANCH_NAME=${GITHUB_REF#refs/tags/} | |
| else | |
| BRANCH_NAME=${GITHUB_REF#refs/heads/} | |
| fi | |
| VALID_PATTERNS="^(feat|fix|chore|refactor|docs)\/[a-z0-9]+(-[a-z0-9]+)*$|^v[0-9]+\.[0-9]+\.[0-9]+(-[a-z0-9]+(\.[a-z0-9]+)*(-[a-z0-9]+)*)?$" | |
| if ! [[ $BRANCH_NAME =~ $VALID_PATTERNS ]]; then | |
| { | |
| echo "## ❌ Branch naming check failed" | |
| echo "Branch name '${BRANCH_NAME}' does not follow the required naming convention." | |
| echo "It must follow the naming convention: 'feat|fix|chore|refactor|docs/<name>' for branches or 'v*' for tags." | |
| } >> "$GITHUB_STEP_SUMMARY" | |
| echo "::error::Branch name '${BRANCH_NAME}' does not follow the required naming convention." | |
| exit 1 | |
| fi |