Validating and linting the git branch name. Create a config file or use the default configuration file. Use it in husky config file to make sure that your branch will not be rejected by some pesky Jenkins branch name conventions. You may use it as part of a CI process or just as an handy npx
command.
$ npm install branch-name-lint
$ npx branch-name-lint
$ npx branch-name-lint --help
Usage
npx branch-name-lint [configfileLocation JSON|JS]
Options
--help - to get this screen
--branch - specify a custom branch name to check instead of the current git branch
Examples
$ branch-name-lint
$ branch-name-lint config-file.json
$ branch-name-lint config-file.js
$ branch-name-lint --branch feature/my-new-feature
Any Valid JSON file with branchNameLinter
attribute.
{
"branchNameLinter": {
"prefixes": [
"feature",
"hotfix",
"release"
],
"suggestions": {
"features": "feature",
"feat": "feature",
"fix": "hotfix",
"releases": "release"
},
"banned": [
"wip"
],
"skip": [
"skip-ci"
],
"disallowed": [
"master",
"develop",
"staging"
],
"separator": "/",
"branchNameEnvVariable": false,
"branch": false,
"msgBranchBanned": "Branches with the name \"%s\" are not allowed.",
"msgBranchDisallowed": "Pushing to \"%s\" is not allowed, use git-flow.",
"msgPrefixNotAllowed": "Branch prefix \"%s\" is not allowed.",
"msgPrefixSuggestion": "Instead of \"%s\" try \"%s\".",
"msgSeparatorRequired": "Branch \"%s\" must contain a separator \"%s\"."
}
}
You can specify a custom branch name to validate instead of using the current git branch in two ways:
-
Using the CLI flag:
$ npx branch-name-lint --branch feature/my-custom-branch
-
Using configuration:
{ "branchNameLinter": { "branch": "feature/my-custom-branch" } }
-
Using an environment variable:
{ "branchNameLinter": { "branchNameEnvVariable": "CI_BRANCH_NAME" } }
Then set the environment variable:
CI_BRANCH_NAME=feature/my-custom-branch npx branch-name-lint
This is useful for CI/CD environments where you might want to validate branch names from environment variables.
You can disable prefix or separator checks by setting their respective configuration values to false
:
{
"branchNameLinter": {
"prefixes": false, // Disables the prefix validation check
"separator": false, // Disables the separator validation check
"regex": "^(revert|master|develop|issue|release|hotfix/|feature/|support/|shift-)"
}
}
When prefixes
is set to false
, any branch prefix will be allowed. When separator
is set to false
, branches without separators will be allowed.
You can also use a JavaScript file for configuration, which allows for more dynamic configuration with variables and imports:
// config-file.js
// Define constants that can be reused
const COMMON_PREFIXES = ['feature', 'bugfix', 'hotfix', 'release'];
const CI_PREFIXES = ['ci', 'build'];
// Combine arrays for configuration
const ALL_PREFIXES = [...COMMON_PREFIXES, ...CI_PREFIXES];
// Export the configuration object
module.exports = {
prefixes: ALL_PREFIXES, // Set to false to disable prefix check
suggestions: {
feat: 'feature'
},
banned: ['wip', 'tmp'],
skip: ['develop', 'master', 'main'],
separator: '/', // Set to false to disable separator check
disallowed: ['master', 'develop', 'main'],
// other options...
};
In order to check the branch name with a regex you can add a a regex as a string under the branchNameLinter in your config JSON. You can also pass any options for the regex (e.g. case insensitive: 'i')
{
"branchNameLinter": {
"regex": "^([A-Z]+-[0-9]+.{5,70})",
"regexOptions": "i",
...
"msgDoesNotMatchRegex": 'Branch "%s" does not match the allowed pattern: "%s"'
}
}
After installation, just add in any husky hook as node modules call.
"husky": {
"hooks": {
"pre-push": "npx branch-name-lint [sample-configuration.json]"
}
},
Or with a JavaScript configuration file:
"husky": {
"hooks": {
"pre-push": "npx branch-name-lint [sample-configuration.js]"
}
},
You can integrate branch-name-lint into your GitHub Actions workflows to enforce branch naming conventions across your team. This is especially useful for maintaining consistent branch naming in collaborative projects.
Create a workflow file at .github/workflows/branch-name-lint.yml
:
name: Branch Name Lint
on:
push:
branches-ignore:
- main
- master
pull_request:
branches:
- main
- master
jobs:
lint-branch-name:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install branch-name-lint --no-save
- name: Extract branch name
shell: bash
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
# For pull requests, use the head branch name
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV
else
# For pushes, extract from GITHUB_REF
echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
fi
- name: Check branch name
run: npx branch-name-lint
env:
BRANCH_NAME: ${{ env.BRANCH_NAME }}
For more advanced use cases, create a custom configuration file:
- First, create a config file at
.github/branch-name-lint.json
:
{
"branchNameLinter": {
"prefixes": [
"feature",
"hotfix",
"release",
"docs",
"chore",
"fix",
"ci",
"test"
],
"suggestions": {
"features": "feature",
"feat": "feature",
"fix": "hotfix",
"releases": "release"
},
"banned": ["wip"],
"skip": ["main", "master", "develop", "staging"],
"disallowed": [],
"separator": "/",
"branchNameEnvVariable": "BRANCH_NAME",
"msgBranchBanned": "Branches with the name \"%s\" are not allowed.",
"msgPrefixNotAllowed": "Branch prefix \"%s\" is not allowed.",
"msgPrefixSuggestion": "Instead of \"%s\" try \"%s\".",
"msgSeparatorRequired": "Branch \"%s\" must contain a separator \"%s\"."
}
}
- Then reference this configuration in your workflow:
name: Branch Name Lint
on:
push:
branches-ignore:
- main
- master
pull_request:
branches:
- main
- master
jobs:
lint-branch-name:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install branch-name-lint --no-save
- name: Extract branch name
shell: bash
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV
else
echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
fi
- name: Check branch name
run: npx branch-name-lint .github/branch-name-lint.json
env:
BRANCH_NAME: ${{ env.BRANCH_NAME }}
When using branch-name-lint in a matrix strategy with multiple operating systems, ensure you use environment variables in a cross-platform way:
jobs:
lint-branch-name:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install branch-name-lint --no-save
- name: Extract branch name
shell: bash
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV
else
echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
fi
- name: Check branch name
run: npx branch-name-lint .github/branch-name-lint.json
env:
BRANCH_NAME: ${{ env.BRANCH_NAME }}
This setup ensures that your branch naming conventions are enforced consistently across all contributions to your repository.
const branchNameLint = require('branch-name-lint');
branchNameLint();
//=> 1 OR 0.
Type: object
Default:
{
prefixes: ['feature', 'hotfix', 'release'],
suggestions: {features: 'feature', feat: 'feature', fix: 'hotfix', releases: 'release'},
banned: ['wip'],
skip: [],
disallowed: ['master', 'develop', 'staging'],
separator: '/',
msgBranchBanned: 'Branches with the name "%s" are not allowed.',
msgBranchDisallowed: 'Pushing to "%s" is not allowed, use git-flow.',
msgPrefixNotAllowed: 'Branch prefix "%s" is not allowed.',
msgPrefixSuggestion: 'Instead of "%s" try "%s".',
msgSeparatorRequired: 'Branch "%s" must contain a separator "%s".'
}
MIT © Ran Bar-Zik