gitlab 配置 自定义钩子 pre-receive

gitlab配置custom hook

设置custom_hooks路径

修改 gitlab 中的vi /etc/gitlab/gitlab.rb

注:这里直接去掉注释使用自带的

gitlab_shell['custom_hooks_dir'] = "/opt/gitlab/embedded/service/gitlab-shell/hooks"

启用配置

sudo gitlab-ctl reconfigure


创建hook文件

自定义脚本目录要符合<custom_hooks_dir>/<hook_name.d>/* 的规范。具体就是

  • 在自定的custom_hooks_dir 目录下可创建三个文件夹对应三类 server hook name :

    • pre-receive.d
    • update.d
    • post-receive.d
  • 在每个文件夹下可以创建任意文件,在对应的hook时期,gitlab就会主动调用

  • 文件名以~结尾的文件会被忽略

编写 hook 脚本

hook 脚本就是git 自身的规范,写shell,python、ruby 都可以

留意脚本最后的退出值:0 正常退出,用户可以 push;非 0 异常退出,中断提交(pre-receive 和 update) 。 细节参见: 5.4 Git钩子:自定义你的工作流 · geeeeeeeeek/git-recipes Wiki · GitHub

如果想让用户 push 时看到相应的日志直接在脚本中 echo 即可。

这里举两个例子:

🌰:Say hi.

#!/bin/sh

echo "Say hi from gitlab servertes ok 😄"
exit 0



参考文档: https://java.isture.com/tools/gitlab/custom-hook.html#_1-4-%E7%BC%96%E5%86%99-hook-%E8%84%9A%E6%9C%AC
posted @ 2022-02-14 19:40  李瑞鑫  阅读(2270)  评论(0编辑  收藏  举报