在gitlab上使用server_hooks -实例级和项目级

1. 前置条件

2. Git Hook

与许多其他版本控制系统一样,Git 有一种方法可以在发生某些重要操作时,触发自定义脚本,即 Git Hook(Git 钩子)。
在这里插入图片描述
当我们初始化一个项目之后,.git 目录下有一个 hooks 目录,可以看到上图左侧有很多执行任务,比如 pre-commit,代表在运行这些命令之后或之前,会进行一些校验和检测来执行相应任务。

2.1 Git Hook 分为两部分:本地和远程

在这里插入图片描述

2.1.1 本地 Git Hook,由提交和合并等操作触发:
  • 比如代码发生变更,进行 git add,把 message 进行 commit changes;
  • 当 git commit 时,就会执行一个钩子叫 pre-commit(准备提交钩子)。
2.1.2 远程 Git Hook,运行在网络操作上,例如接收推送的提交:
  • 在 commit 之后,要推送到远端,此时有一个叫 pre-push 钩子,把信息推送 git 仓库;
  • 在远程阶段,极狐GitLab 相当于一个远程仓库。如图有很多仓库,分别承担不同功能,比如 pre-receive ,主要在服务器端接收通过本地推上来代码,然后 update 相关代码,post-receive 说明代码接受成功,同时有一个服务器钩子执行。

在这里,我们主要关注本地 hook,比如说 pre-message 和 pre-push,因此我们会借助这些工具来实现规范化代码内容。

3. 操作步骤

3.1 对所有的仓库配置server hooks

3.1.1 全局配置

参考地址:git hooks all repo

gitaly['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks"
gitlab-ctl reconfigure

在这里插入图片描述

3.1.2 编写脚本

创建目录custom_hooks

mkdir -p /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/

编写脚本

该脚本的作用是规范gitlab提交的信息
cat /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/pre-receive.sh

#!/bin/bash
while read oldrev newrev refname; do
    # 从标准输入读取每个引用的旧版本、新版本和引用名称
    commits=$(git rev-list --pretty=oneline $oldrev..$newrev)
    # 遍历每个提交信息
    while read commit; do
        # 提取提交信息的前缀
        prefix=$(echo "$commit" | awk '{print $2}' | awk -F ":" '{print $1}')
        # 检查前缀是否符合要求
        if [[ $prefix != "feat" && $prefix != "fix" && $prefix != "hotfix" ]]; then
            echo "Error: Invalid commit prefix in one or more commits:"
            echo "$commit"
            echo "Only commits with prefixes 'feat', 'fix', or 'hotfix' are allowed."
            exit 1
        fi
    done <<< "$commits"
done

给脚本权限

chown -R git.root /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d
chmod +x git.root /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/pre-receive.sh
3.1.3 测试

测试提交
在这里插入图片描述
回退commit
在这里插入图片描述
再次提交
在这里插入图片描述

3.2 对单个仓库配置server hooks

3.1.1 全局配置

参考地址:git hooks single repo

gitaly['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks"
gitlab-ctl reconfigure

在这里插入图片描述

对于配置单个仓库的server hooks也是需要开启全局的配置,否则会导致脚本在某个repo的相对路径下不会生效

3.1.2 编写脚本

创建目录custom_hooks

# 进入到仓库在gitlab中的存储路径下
cd /var/opt/gitlab/git-data/repositories/
# 根据在gitlab的`管理界面 - 项目`中查看项目的相对路径(是一个hash路径)
cd @hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git
# 创建custom_hooks目录
mkdir -p custom_hooks/pre-receive.d
chown -R git.root custom_hooks

编写脚本

该脚本的作用是禁用对该仓库做任何的push操作

cat pre-receive.sh

#!/bin/bash

while read oldrev newrev refname; do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)

    # 检查是否有提交,如果有则拒绝
    if [ "$oldrev" != "0000000000000000000000000000000000000000" ]; then
        echo "Pushing to $branch is not allowed. All pushes are prohibited."
        exit 1
    fi
done

给脚本权限

# 进入到仓库在gitlab中的存储路径下
cd /var/opt/gitlab/git-data/repositories/
# 根据在gitlab的`管理界面 - 项目`中查看项目的相对路径(是一个hash路径)
cd @hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git
chown -R git.root custom_hooks/
chmod +x git.root custom_hooks/pre-receive.d/pre-receive.sh

在这里插入图片描述

3.1.3 测试

测试提交
在这里插入图片描述

  • 25
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GitLabServer Hooks可以让您在特定事件发生时执行自定义脚本来自动化操作,包括校验commit的用户名和邮箱。下面是具体的操作步骤: 1. 创建一个用于校验的脚本。您可以使用任何可用的脚本语言,例如bash、Python等。以下是一个使用Python的示例脚本: ```python #!/usr/bin/env python import sys import re # Check the commit author name and email commit_line = sys.stdin.readline().strip() commit_fields = commit_line.split("\t") commit_author = commit_fields[2] commit_email = commit_fields[3] # Check the author name against a regex author_regex = "^John Doe$" if not re.match(author_regex, commit_author): sys.stderr.write("Commit author name must match regex: {}\n".format(author_regex)) sys.exit(1) # Check the author email against a regex email_regex = "^johndoe@example.com$" if not re.match(email_regex, commit_email): sys.stderr.write("Commit author email must match regex: {}\n".format(email_regex)) sys.exit(1) sys.exit(0) ``` 此脚本会检查提交的作者名称和电子邮件是否与特定的正则表达式匹配。如果匹配失败,则会打印错误消息并退出。如果匹配成功,则会正常退出。 2. 将脚本上传到GitLab服务器的特定目录。可以将脚本放置在任何目录中,只要GitLab用户有访问权限即可。在本例中,我们将脚本放置在`/usr/local/bin/gitlab-check-commit-author.py`。 3. 在GitLab服务器上设置Server Hook。要设置Server Hook,请按照以下步骤操作: - 打开GitLab服务器上的GitLab仓库。 - 单击仓库页面右上角的齿轮图标,然后单击“设置”。 - 选择“集成”选项卡。 - 在“Web Hooks”部分中单击“添加 Web Hook”按钮。 - 在“URL”字段中输入脚本的URL(例如:`http://localhost:8080/gitlab-check-commit-author.py`)。 - 在“Trigger”部分中选择要触发Hook的事件。在本例中,我们将选择“Push events”。 - 单击“Add Web Hook”按钮保存设置。 现在,当GitLab仓库中发生push事件时,Hook将会调用您的脚本来检查提交的作者名称和电子邮件是否与指定的正则表达式匹配。如果检查失败,则会打印错误消息并拒绝提交。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值