gitlab设置hook
gitlab hook shell脚本 例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# Set the JIRA ID pattern to match against
JIRA_PATTERN='[A-Z]{2,}-[0-9]+'
# Read the commit message from GitLab standard input
while read oldrev newrev refname; do
# Get the commit message for the latest revision
COMMIT_MSG=$(git log --format=%B --max-count=1 "$newrev")
# Check if the commit message contains a JIRA ID
if [[ ! $COMMIT_MSG =~ $JIRA_PATTERN ]]; then
# Reject the commit if there is no JIRA ID
echo "ERROR: Your commit message must include a JIRA ID (e.g. ABC-123)." >&2
exit 1
fi
done
此脚本将读取 GitLab 标准输入中的提交消息,并使用正则表达式来查找匹配 Jira ID 模式的字符串。如果提交消息不包含 Jira ID,则将拒绝提交,并输出错误消息。
要将此脚本用作 GitLab 钩子,请按照以下步骤操作:
- 在您的 Git 存储库中创建一个名为
hooks
的目录。 - 在
hooks
目录中创建一个名为pre-receive
的可执行文件。 - 将上面的脚本内容复制到
pre-receive
文件中,并将文件保存到hooks
目录中。 - 使用
chmod +x hooks/pre-receive
命令使pre-receive
文件可执行。 - 确保您的 GitLab 存储库已配置为使用自定义钩子,并指向
hooks
目录。
完成这些步骤后,您的 GitLab 存储库将在每次提交时运行此脚本,并检查提交消息中是否包含 Jira ID。如果提交消息不包含 Jira ID,则提交将被拒绝,并显示错误消息。
本文由作者按照
CC BY 4.0
进行授权