钩子介绍
自定义钩子分为:项目钩子和全局钩子
自定义全局钩子:
全局钩子目录结构:
(注意:excludes目录结构是我们自定义的目录,规则逻辑在update.d/update.py脚本里实现的,非gitlab官方提供功能)
/opt/gitlab/embedded/service/gitlab-shell/custom_hooks
├── excludes
│ └── excludes.txt
└── update.d
└── update.py
钩子目录:
自定全局钩子目录: /opt/gitlab/embedded/service/gitlab-shell/custom_hooks ,其中 update.d/update.py 是我们自定义的钩子脚本, 脚本使用python语言。
如何扩展和修改全局钩子指定目录?
1.修改 /etc/gitlab/gitlab.rb 配置文件中的配置项:gitlab_shell['custom_hooks_dir'] = "/opt/gitlab/embedded/service/gitlab-shell/custom_hooks"
2. 执行 sudo gitlab-ctl reconfigure 命令来使配置生效。
Tips:不要尝试直接修改 /config.yml 的文件内容,文件中有说明:
This file is managed by gitlab-ctl. Manual changes will be erased! To change the contents below, edit /etc/gitlab/gitlab.rb and runsudo gitlab-ctl reconfigure.
3.在自定的 custom_hooks_dir 目录(即我们的custom_hooks目录)下可创建三个文件夹对应三类 server hook name :
pre-receive.d
update.d
post-receive.d
在每个文件夹下可创建任意文件,在对应的 hook 时期,gitlab 就会主动调用
文件名以 ~ 结尾的文件会被忽略
如果想看这部分的实现细节可以看 /lib/gitlab_custom_hook.rb 文件
如何对单个项目排除全局钩子?
在 /opt/gitlab/embedded/service/gitlab-shell/custom_hooks/excludes/excludes.txt 文件中新加一行你要排除的git的地址即可:例如:
自定义项目钩子
项目钩子的目录为固定目录:
.git/custom_hooks/ 例如我们的项目自定义目录为: /var/opt/gitlab/git-data/repositories/frontend/testWeb.git/custom_hooks/update ,update是我们自定义的钩子脚本。
钩子的执行顺序
钩子将按照以下的顺序执行
.git/hooks/ - symlink to gitlab-shell/hooks global dir
.git/hooks/ - executed by git itself, this is gitlab-shell/hooks/
.git/custom_hooks/ - per project hook (this is already existing behavior)
.git/custom_hooks/.d/* - per project hooks
.git/hooks/.d/* OR //* - global hooks: all executable files (minus editor backup files)
钩子校验规则
1、提交合并代码到test分支前,需要先合并代码到dev验证通过后方能push到test
2、提交合并代码到master分支前,需要先合并代码到dev、test验证通过后方能push到master
3、不同分支不能