### GitHub Actions Workflow for CI/CD Integration Source: https://context7.com/context7/conventional-branch_github_io/llms.txt A YAML workflow example for GitHub Actions that demonstrates how to trigger different CI/CD behaviors based on branch naming conventions. This includes running tests for feature branches, deploying to staging for release branches, and deploying to production for hotfix branches. ```yaml # This workflow demonstrates a CI/CD integration based on branch naming conventions. # Specific behaviors are triggered based on prefixes like 'feature/', 'release/', and 'hotfix/'. name: Conventional Branch CI/CD on: push: branches: - 'feature/**' - 'bugfix/**' - 'fix/**' - 'hotfix/**' - 'release/**' - 'chore/**' - main - master - develop jobs: build_and_test: runs-on: ubuntu-latest if: github.ref_type == 'branch' && (startsWith(github.ref_name, 'feature/') || startsWith(github.ref_name, 'feat/') || startsWith(github.ref_name, 'bugfix/') || startsWith(github.ref_name, 'fix/') || startsWith(github.ref_name, 'chore/')) steps: - uses: actions/checkout@v3 - name: Set up Node.js uses: actions/setup-node@v3 with: node-version: '18' - name: Install dependencies run: npm install - name: Run tests run: npm test deploy_staging: runs-on: ubuntu-latest needs: build_and_test if: github.ref_type == 'branch' && startsWith(github.ref_name, 'release/') steps: - uses: actions/checkout@v3 - name: Deploy to Staging run: echo "Deploying release branch ${{ github.ref_name }} to staging..." # Add your staging deployment commands here deploy_production: runs-on: ubuntu-latest needs: build_and_test if: github.ref_type == 'branch' && (startsWith(github.ref_name, 'hotfix/') || github.ref_name == 'main' || github.ref_name == 'master') steps: - uses: actions/checkout@v3 - name: Deploy to Production run: echo "Deploying hotfix branch ${{ github.ref_name }} or main to production..." # Add your production deployment commands here ``` -------------------------------- ### Git Branch Naming Examples Source: https://context7.com/context7/conventional-branch_github_io/llms.txt Examples of valid and invalid Git branch names following the Conventional Branch specification. This includes feature, bugfix, hotfix, release, and chore branches, along with common invalid patterns. ```bash # Feature branch examples git checkout -b feature/add-login-page git checkout -b feat/user-authentication git checkout -b feature/issue-123-payment-integration # Bug fix branches git checkout -b bugfix/fix-header-alignment git checkout -b fix/memory-leak-in-parser # Hotfix for urgent production issues git checkout -b hotfix/security-patch-cve-2024 git checkout -b hotfix/critical-database-connection # Release preparation branches git checkout -b release/v1.2.0 git checkout -b release/v2.0.0-beta.1 # Chore branches for non-code tasks git checkout -b chore/update-dependencies git checkout -b chore/improve-documentation # Validation - these are INVALID branch names # git checkout -b feature/New-Login # Uppercase not allowed # git checkout -b fix_header_bug # Underscores not allowed # git checkout -b feature/-login # Leading hyphen not allowed # git checkout -b release/v1.2.0. # Trailing dot not allowed # git checkout -b feature/new--login # Consecutive hyphens not allowed ``` -------------------------------- ### GitHub Actions 自动化分支检查 Source: https://conventional-branch.github.io/zh 此 GitHub Actions 工作流使用 commit-check-action 来自动检查分支名称是否符合约定式分支规范。这有助于在代码托管在 GitHub 上时强制执行命名约定。 ```yaml name: Commit Check on: push: branches: [ main, develop, "feature/*", "bugfix/*", "hotfix/*", "release/*", "chore/*" ] pull_request: branches: [ main, develop, "feature/*", "bugfix/*", "hotfix/*", "release/*", "chore/*" ] jobs: commit-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 with: fetch-depth: 0 - name: Run commit-check uses: therefore/commit-check-action@v0.0.2 with: # Optional: Path to your Conventional Commits configuration file # config_path: .commitlintrc.yml # Optional: You can specify the branches to check # branches: "main,develop" # Optional: You can specify the branch pattern to check branch_pattern: "^(feature|feat|bugfix|fix|hotfix|release|chore)\/.*$" # Optional: You can specify the branch pattern to ignore # ignore_branch_pattern: "^(main|develop)$" # Optional: Enable or disable the check for Pull Requests check_pr: true ``` -------------------------------- ### GitLab CI Pipeline for Branch-based Job Stages Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This GitLab CI configuration defines stages for validation, testing, building, and deployment, with jobs dynamically triggered based on branch naming conventions. It uses regex to validate branch names and applies specific jobs to feature, release, and hotfix branches, ensuring appropriate actions are taken for each. ```yaml # .gitlab-ci.yml stages: - validate - test - build - deploy variables: BRANCH_NAME: $CI_COMMIT_REF_NAME validate_branch: stage: validate script: - | if [[ ! "$BRANCH_NAME" =~ ^(main|master|develop|feature\/|feat\/|bugfix\/|fix\/|hotfix\/|release\/|chore\/) ]]; then echo "❌ Invalid branch name: $BRANCH_NAME" echo "Must use conventional branch prefix (feature/, bugfix/, hotfix/, release/, chore/)" exit 1 fi - echo "✅ Branch name validated: $BRANCH_NAME" test_feature: stage: test only: - /^feature\/.*/ - /^feat\/.*/ - /^bugfix\/.*/ - /^fix\/.*/ script: - npm install - npm run test - npm run lint build_release: stage: build only: - /^release\/.*/ script: - npm run build - npm run test:integration artifacts: paths: - dist/ expire_in: 1 week deploy_staging: stage: deploy only: - /^release\/.*/ environment: name: staging url: https://staging.example.com script: - echo "Deploying to staging environment" - ./deploy.sh staging deploy_production_hotfix: stage: deploy only: - /^hotfix\/.*/ environment: name: production url: https://example.com when: manual script: - echo "Emergency hotfix deployment" - ./deploy.sh production - ./notify-team.sh "Hotfix $BRANCH_NAME deployed" ``` -------------------------------- ### Configure Bitbucket Branching Model Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This script configures the branching model settings in Bitbucket. It uses `curl` to make a PUT request to the Bitbucket API, defining settings for development and production branches, including whether to use the main branch and the name of the main branch. It also specifies the kinds and prefixes for different branch types like feature, bugfix, release, and hotfix. Ensure BITBUCKET_USER, BITBUCKET_APP_PASSWORD, and API_URL are set as environment variables. ```shell curl -X PUT \ -u "$BITBUCKET_USER:$BITBUCKET_APP_PASSWORD" \ -H "Content-Type: application/json" \ "$API_URL/branching-model/settings" \ -d '{ "development": { "use_mainbranch": true }, "production": { "use_mainbranch": true, "name": "main" }, "branch_types": [ { "kind": "feature", "prefix": "feature/" }, { "kind": "bugfix", "prefix": "bugfix/" }, { "kind": "release", "prefix": "release/" }, { "kind": "hotfix", "prefix": "hotfix/" } ] }' ``` -------------------------------- ### Git 分支命名规范示例 Source: https://conventional-branch.github.io/zh 此代码段展示了约定式分支的命名格式,包括类型前缀和描述。遵循此格式可以提高分支的可读性和自动化友好性。 ```git-commit / Examples: feature/add-login-page feat/add-login-page bugfix/fix-header-bug fix/header-bug hotfix/security-patch release/v1.2.0 chore/update-dependencies ``` -------------------------------- ### GitHub API: Bash Script for Branch Protection Rules Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This bash script automates the configuration of branch protection rules on GitHub using its API. It sets required status checks, pull request review requirements, and enforces linear history for the main branch, while also defining rulesets to enforce conventional branch naming patterns. ```bash #!/bin/bash # configure-branch-protection.sh GITHUB_TOKEN="your_github_token" REPO_OWNER="your-org" REPO_NAME="your-repo" API_URL="https://api.github.com/repos/$REPO_OWNER/$REPO_NAME" # Protect main branch curl -X PUT \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ "$API_URL/branches/main/protection" \ -d '{ "required_status_checks": { "strict": true, "contexts": ["validate-branch", "test"] }, "enforce_admins": true, "required_pull_request_reviews": { "required_approving_review_count": 2, "dismiss_stale_reviews": true }, "restrictions": null, "required_linear_history": true, "allow_force_pushes": false, "allow_deletions": false }' # Configure rulesets for branch naming curl -X POST \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ "$API_URL/rulesets" \ -d '{ "name": "Conventional Branch Naming", "target": "branch", "enforcement": "active", "conditions": { "ref_name": { "include": ["~ALL"], "exclude": ["refs/heads/main", "refs/heads/master", "refs/heads/develop"] } }, "rules": [ { "type": "creation", "parameters": { "patterns": [ "^refs/heads/feature/[a-z0-9][a-z0-9.-]*[a-z0-9]$", "^refs/heads/feat/[a-z0-9][a-z0-9.-]*[a-z0-9]$", "^refs/heads/bugfix/[a-z0-9][a-z0-9.-]*[a-z0-9]$", "^refs/heads/fix/[a-z0-9][a-z0-9.-]*[a-z0-9]$", "^refs/heads/hotfix/[a-z0-9][a-z0-9.-]*[a-z0-9]$", "^refs/heads/release/v[0-9]+\\. [0-9]+\\. [0-9]+", "^refs/heads/chore/[a-z0-9][a-z0-9.-]*[a-z0-9]$" ] } } ] }' echo "✅ Branch protection rules configured" echo "✅ Conventional Branch naming enforcement enabled" ``` -------------------------------- ### Configure Bitbucket Branch Restrictions Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This script configures branch restrictions in Bitbucket for various branch patterns like feature, fix, hotfix, etc. It uses `curl` to make POST requests to the Bitbucket API, requiring passing builds to merge and setting the pattern and value for the restriction. Ensure BITBUCKET_USER, BITBUCKET_APP_PASSWORD, and API_URL are set as environment variables. ```shell for pattern in "feature/*" "feat/*" "bugfix/*" "fix/*" "hotfix/*" "release/*" "chore/*"; do curl -X POST \ -u "$BITBUCKET_USER:$BITBUCKET_APP_PASSWORD" \ -H "Content-Type: application/json" \ "$API_URL/branch-restrictions" \ -d "{ \"kind\": \"require_passing_builds_to_merge\", \"branch_match_kind\": \"glob\", \"pattern\": \"$pattern\", \"value\": 2 }" done ``` -------------------------------- ### Bitbucket API: Bash Script for Branch Permissions Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This bash script configures branch permissions for Bitbucket repositories using its API. It sets up push restrictions for the main branch, ensuring that only specified users or groups (like administrators) can push to it, thus enforcing branch control policies. ```bash #!/bin/bash # bitbucket-configure-branches.sh BITBUCKET_USER="your-username" BITBUCKET_APP_PASSWORD="your-app-password" WORKSPACE="your-workspace" REPO_SLUG="your-repo" API_URL="https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_SLUG" # Create branch restriction for main branch curl -X POST \ -u "$BITBUCKET_USER:$BITBUCKET_APP_PASSWORD" \ -H "Content-Type: application/json" \ "$API_URL/branch-restrictions" \ -d '{ "kind": "push", "branch_match_kind": "glob", "pattern": "main", "users": [], "groups": ["administrators"], "value": 1 }' ``` -------------------------------- ### Branch Specification Basic Rules Source: https://conventional-branch.github.io/index Outlines the fundamental rules for constructing conventional branch names, emphasizing the use of lowercase alphanumeric characters, hyphens, and dots, while disallowing consecutive or misplaced hyphens/dots. Clarity, conciseness, and optional ticket number inclusion are also highlighted. ```text 1. Use lowercase alphanumerics, hyphens, and dots. 2. No consecutive, leading, or trailing hyphens or dots. 3. Keep names clear and concise. 4. Include ticket numbers if applicable (e.g., feature/issue-123-new-login). ``` -------------------------------- ### Automated Branch Specification Check (GitHub Action) Source: https://conventional-branch.github.io/index Presents 'commit-check-action', a GitHub Action that integrates with your CI/CD pipeline to automatically check branch specifications. This ensures that all branches merged into your repository comply with the defined naming conventions. ```yaml name: Conventional Branch Check on: [push, pull_request] jobs: check_branch: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run conventional branch check uses: commit-check/commit-check-action@v1.2.0 ``` -------------------------------- ### GitHub Actions Workflow for Branch Validation Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This workflow validates branch names on push and pull requests. It checks if branches adhere to a specified conventional prefix format. It also includes steps for running tests and deploying to staging or production based on branch types. ```yaml name: Conventional Branch CI/CD on: push: branches: - 'feature/**' - 'feat/**' - 'bugfix/**' - 'fix/**' - 'hotfix/**' - 'release/**' - 'chore/**' pull_request: branches: - main - develop jobs: validate-branch: runs-on: ubuntu-latest steps: - name: Validate branch name run: | BRANCH_NAME="${GITHUB_REF#refs/heads/}" if [[ ! "$BRANCH_NAME" =~ ^(feature|feat|bugfix|fix|hotfix|release|chore)/ ]]; then echo "❌ Invalid branch name: $BRANCH_NAME" exit 1 fi echo "✅ Valid branch name: $BRANCH_NAME" test: needs: validate-branch runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/bugfix/') steps: - uses: actions/checkout@v3 - name: Run tests run: | npm install npm test deploy-staging: needs: test runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/heads/release/') steps: - uses: actions/checkout@v3 - name: Deploy to staging run: | echo "Deploying release branch to staging environment" # ./deploy-staging.sh deploy-production-hotfix: needs: validate-branch runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/heads/hotfix/') steps: - uses: actions/checkout@v3 - name: Emergency deploy to production run: | echo "Deploying hotfix to production" # ./deploy-production.sh ``` -------------------------------- ### Automated Branch Specification Check (CLI) Source: https://conventional-branch.github.io/index Introduces 'commit-check', a command-line tool designed to automatically verify if branch names adhere to the Conventional Branch specification. This helps enforce consistency within development teams. ```bash commit-check ``` -------------------------------- ### Client-Side Git Pre-Push Hook for Branch Validation Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This bash script acts as a Git pre-push hook to validate branch names locally before they are pushed to the remote repository. It checks for required prefixes, allowed characters, and avoids consecutive or misplaced hyphens/dots. It skips validation for main branches like 'main', 'master', and 'develop'. ```bash #!/bin/bash # Save as .git/hooks/pre-push and chmod +x current_branch=$(git rev-parse --abbrev-ref HEAD) # Skip validation for main branches if [[ "$current_branch" =~ ^(main|master|develop)$ ]]; then exit 0 fi echo "Validating branch name: $current_branch" # Check prefix if ! [[ "$current_branch" =~ ^(feature|feat|bugfix|fix|hotfix|release|chore)\/ ]]; then cat << EOF ❌ Invalid branch name: "$current_branch" Branch must start with one of: - feature/ or feat/ (new features) - bugfix/ or fix/ (bug fixes) - hotfix/ (urgent production fixes) - release/ (release preparation) - chore/ (maintenance tasks) Example: feature/add-user-login EOF exit 1 fi # Check format if ! [[ "$current_branch" =~ ^[a-z0-9\/\.\-]+$ ]]; then echo "❌ Branch name contains invalid characters (use only a-z, 0-9, -, .)" exit 1 fi if [[ "$current_branch" =~ (--|\.\.) ]]; then echo "❌ Branch name has consecutive hyphens or dots" exit 1 fi if [[ "$current_branch" =~ \/[-\.] ]] || [[ "$current_branch" =~ [-\.]$\\ ]]; then echo "❌ Branch name has leading or trailing hyphens/dots in description" exit 1 fi echo "✅ Branch name is valid" exit 0 ``` -------------------------------- ### commit-check Configuration for Branch and Commit Validation Source: https://context7.com/context7/conventional-branch_github_io/llms.txt This YAML configuration file defines checks for the commit-check tool, enforcing branch naming conventions and Conventional Commits for commit messages. It specifies patterns and custom error messages for various validation rules, including branch prefixes, allowed characters, and commit message structure. ```yaml # .commitcheckrc.yml checks: branch: - check: branch_pattern pattern: ^(main|master|develop|feature\/|feat\/|bugfix\/|fix\/|hotfix\/|release\/|chore\/) error: | Invalid branch name format. Your branch: {branch} Valid formats: • feature/description or feat/description • bugfix/description or fix/description • hotfix/description • release/vX.Y.Z • chore/description Example: feature/add-payment-gateway suggest: | git checkout -b feature/your-feature-name - check: branch_format pattern: ^[a-z0-9\/\.\-]+ error: | Branch name contains invalid characters. Use only lowercase letters (a-z), numbers (0-9), hyphens (-), and dots (.) - check: no_consecutive_separators pattern: ^(?!.*(-{2}|\.{2})) error: "Branch name cannot contain consecutive hyphens or dots" - check: no_leading_trailing pattern: ^[a-z]+\/[a-z0-9].*[a-z0-9]$ error: "Branch description cannot start or end with hyphens or dots" commit: - check: commit_message pattern: ^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+ error: "Commit message must follow Conventional Commits format" ``` -------------------------------- ### Git Pre-receive Hook for Branch Naming Validation Source: https://context7.com/context7/conventional-branch_github_io/llms.txt A server-side Git pre-receive hook script that validates pushed branch names against the Conventional Branch specification. It checks for valid prefixes, allowed characters, and formatting rules. ```bash #!/bin/bash # Save as .git/hooks/pre-receive on Git server while read oldrev newrev refname; do branch_name=$(echo $refname | sed 's/refs\/heads\///') # Allow main/master/develop branches if [[ "$branch_name" =~ ^(main|master|develop)$ ]]; then continue fi # Check for valid prefix if ! [[ "$branch_name" =~ ^(feature|feat|bugfix|fix|hotfix|release|chore)\/ ]]; then echo "ERROR: Branch '$branch_name' does not use a valid prefix." echo "Valid prefixes: feature/, feat/, bugfix/, fix/, hotfix/, release/, chore/" exit 1 fi # Check for lowercase alphanumeric, hyphens, dots only if ! [[ "$branch_name" =~ ^[a-z0-9\/\.\-]+$ ]]; then echo "ERROR: Branch '$branch_name' contains invalid characters." echo "Only lowercase letters, numbers, hyphens, and dots are allowed." exit 1 fi # Check for consecutive hyphens or dots if [[ "$branch_name" =~ (--|\.\.) ]]; then echo "ERROR: Branch '$branch_name' has consecutive hyphens or dots." exit 1 fi # Check for leading/trailing hyphens or dots in description if [[ "$branch_name" =~ \/[-\.] ]] || [[ "$branch_name" =~ [-\.]$ ]]; then echo "ERROR: Branch '$branch_name' has leading or trailing hyphens/dots." exit 1 fi echo "✓ Branch '$branch_name' is valid" done exit 0 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.