Git Hooks Pre-Commit CI Pipeline
Enforce quality gates with a pre-commit hook running lint and format checks, mirrored by the CI pipeline stages.
Code
#!/usr/bin/env bash
# .git/hooks/pre-commit —— 提交前的本地质量门禁
set -euo pipefail
fail() { echo "Error: $*" >&2; exit 1; }
command -v shellcheck >/dev/null || fail "shellcheck 未安装"
# 只检查暂存区里真实变更的文件
mapfile -t staged < <(git diff --cached --name-only --diff-filter=ACM)
lint_shell() {
local scripts=()
for f in "${staged[@]}"; do
[[ "$f" == *.sh ]] && scripts+=("$f")
done
(( ${#scripts[@]} == 0 )) && return 0
shellcheck "${scripts[@]}" || fail "shellcheck 未通过,请修复后重提"
}
check_secrets() {
# 密钥模式粗筛:防凭据误入版本库
local pattern='(AKIA[0-9A-Z]{16}|sk-live-[a-zA-Z0-9]{20,}|BEGIN RSA PRIVATE KEY)'
for f in "${staged[@]}"; do
[[ -f "$f" ]] || continue
if git diff --cached -- "$f" | grep -E "${pattern}" >/dev/null; then
fail "疑似密钥泄漏: ${f}"
fi
done
}
lint_shell
check_secrets
echo "pre-commit 检查通过:${#staged[@]} 个文件"
# CI 流水线中的镜像阶段(.gitlab-ci.yml 节选):
# stages: [lint, test]
# lint:shellcheck:
# stage: lint
# script:
# - shellcheck scripts/*.sh
# lint:secrets:
# stage: lint
# script:
# - gitleaks detect --source . --staged
What the code does
The shebang line is backed by set -euo pipefail: first failure stops the run, unset variables are errors, and a pipeline fails if any stage fails.
The work is delegated to command, mapfile, shellcheck and fail, chained through 9 pipes so each stage’s output feeds the next.
3 helper functions (fail, lint_shell and check_secrets) keep the repeated logic out of the main flow.
What to watch for
- The pipeline is 9 stages deep — when it misbehaves, split the pipe and test the stages separately.
Statements that decide the outcome
command -v shellcheck >/dev/null || fail "shellcheck 未安装"
for f in "${staged[@]}"; do
[[ "$f" == *.sh ]] && scripts+=("$f")
shellcheck "${scripts[@]}" || fail "shellcheck 未通过,请修复后重提"
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Gitignore Template BuilderCompose clean .gitignore files by combining Node, Python, Go, Rust, Java, OS, IDE, and Docker presets with deduplication and custom rules.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- Does the script stop on the first error?
- Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
- Which external commands does the script rely on?
- `command`, `mapfile`, `shellcheck`, `fail` must be available on the PATH environment variable; everything else is shell built-in syntax.
Report a problem with this exampleOpens a pre-filled issue on GitHub.