Using Husky Git Hooks for Pre‑commit and Commit‑msg: Custom Shell Scripts and CI Integration
This article explains how to configure Husky Git hooks, especially pre‑commit and commit‑msg, to run custom shell scripts for code style checks, unit tests, and commit‑message validation, and shows how to reuse these scripts in CI pipelines for consistent code quality.
Husky Project Repository
Husky is a popular tool that lets you easily run scripts in Git hooks, enabling tasks such as code style checks and unit tests before committing code, thereby maintaining repository quality and consistency.
pre‑commit Hook
The pre‑commit hook runs before a commit is finalized, allowing developers to verify that the code snapshot meets project quality standards and style guidelines. If the script exits with a non‑zero status, Git blocks the commit.
Typical use cases include:
Code style checking with tools like ESLint or Prettier.
Static code analysis for potential errors or code smells.
Running unit tests to ensure changes do not break existing functionality.
Automatic code formatting before the commit.
commit‑msg Hook
The commit‑msg hook runs before the commit message is recorded, primarily to validate the message format against project conventions. A non‑zero exit status causes Git to reject the commit.
Common scenarios:
Ensuring the message follows a specific template or includes required prefixes.
Linking the message to issue tracker IDs.
Mandating a change type indicator (e.g., feat, fix, docs).
Checking message length for readability.
Detailed commit‑msg Script
The following script, placed in scripts/check_commit_msg.sh , validates commit messages against a conventional format:
#!/bin/sh
# 获取两个参数:起始SHA和结束SHA
start_sha=$1
end_sha=$2
# 设置颜色变量
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 定义提交信息规范函数
check_commit_message() {
commit_msg="$1"
if ! echo "$commit_msg" | grep -qE "^(feat|fix|docs|style|refactor|test|chore|ci):"; then
echo -e "${RED}Inappropriate commit message:${NC} $1" >&2
echo -e "${RED}Error:${NC} Commit message format is incorrect. It should start with one of '${BLUE}feat|fix|docs|style|refactor|test|chore|ci:${NC}'" >&2
exit 1
fi
}
# workflows传入两个参数,遍历从start_sha到end_sha的所有提交
if [ $# -eq 2 ]; then
for sha in $(git rev-list $start_sha..$end_sha); do
commit_msg=$(git show --format=%B -s $sha)
check_commit_message "$commit_msg"
done
elif [ $# -eq 1 ]; then
check_commit_message "$(cat $1)"
else
echo -e "${RED} error: Failed to get commit message\n"
fi
echo -e "${BLUE}Commit message check passed.${NC}\n"This script checks whether a commit message starts with one of the allowed prefixes (feat, fix, docs, etc.) and can process a range of commits (useful in CI) or a single message file (used by the hook).
Adding the Script to the commit‑msg Hook
To invoke the script from the .husky/commit-msg hook, add the following line:
./scripts/check_commit_msg.sh "$1"When an invalid message is detected, Husky aborts the commit and displays an error, guiding the developer to correct the format.
Summary
Packaging reusable shell scripts in a dedicated directory enables their use both in Husky hooks and CI environments, providing benefits such as automated workflows, enforced code quality, unified development standards, and reduced errors.
Open‑source projects referenced:
Frontend scaffolding create-neat – https://github.com/xun082/create-neat
Online collaborative code editor – https://github.com/xun082/online-edit
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.