Gitlab docker: gitlab/gitlab-ee:15.4.2-ee.0
Gitlab Docs - Git server hooks
Gitlab pre-receive webook 的添加与使用
/etc/gitlab/gitlab.rb
文件中,我们需要打开它进行编辑;vi /etc/gitlab/gitlab.rb
vi
,则需要安装vim;apt-get update
apt-get install vim
gitlab.rb
文件的编辑页面后,输入/custom_hooks_dir
,点击回车,会直接跳到配置行,默认如下所示:#gitaly['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks"
我们需要把前面的#
符号去掉,表示应用这条规则;
gitaly['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks"
esc
按钮,输入:wq
,回车,保存完成;/var/opt/gitlab/gitaly/custom_hooks
文件夹cd /var/opt/gitlab/gitaly/custom_hooks/
pre-receive.d
mkdir pre-receive.d
pre-receive-commit-message-check
vi /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/pre-receive-commit-message-check
#!/bin/bash
zeroCommit='0000000000000000000000000000000000000000'
mergeRegex='^(Merge)(.{1,})'
contentRegex='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.{1,}\))?:\s?(.{1,})'
while read -r oldrev newrev refname; do
echo "开始检查提交信息..."
echo "您提交的分支为:$refname"
# Branch or tag got deleted, ignore the push
[ "$newrev" = "$zeroCommit" ] && continue
# Calculate range for new branch/updated branch
[ "$oldrev" = "$zeroCommit" ] && range="$newrev" || range="$oldrev..$newrev"
for commit in $(git rev-list "$range" --not --all); do
user=$(git log --pretty=format:"%an" $commit -1)
commitDate=$(git log --pretty=format:"%cd" $commit -1)
msg=$(git log --pretty=format:"%s" $commit -1)
flagMerge=$(echo $msg | grep -E "$mergeRegex")
flagPush=$(echo $msg | grep -E "$contentRegex")
if [ -z "$flagPush" ] && [ -z "$flagMerge" ]; then
echo "[ERROR]"
echo "[ERROR]由于这份提交日志不规范,本次提交被拒绝"
echo "[ERROR]$commit in ${refname#refs/heads/}"
echo "[ERROR]提交者:$user"
echo "[ERROR]提交日期:$commitDate"
echo "[ERROR]提交日志:$msg"
echo "[ERROR]提交信息检查不通过!!!"
echo "[ERROR]请按照规范修改提交日志后重新尝试提交。"pre
echo "[ERROR]"
exit 1
fi
done
echo "提交信息检查通过。"
done
chmod 777 pre-receive-commit-message-check