一、官方文档
通常,Git钩子被放置在存储库或项目的hooks
目录中。GitLab从每个项目的hooks
目录创建一个符号链接到gitlab-shell hooks
目录,以便于在gitlab-shell升级之间进行维护。因此,定制钩子的实现方式略有不同。一旦创建了钩子,行为就完全一样了。
按照以下步骤设置自定义挂钩:
- 选择一个需要定制Git钩子的项目。
- 在GitLab服务器上,进入到项目的存储库目录。对于从源安装,路径通常是
/home/git/repositories/<group>/<project>.git
。对于Omnibus安装路径通常/var/opt/gitlab/git-data/repositories/<group>/<project>.git
。 - 在此位置创建一个新的目录
custom_hooks
。 - 在新
custom_hooks
目录中创建一个名称匹配钩子类型的文件。例如push后出发的post-receive
没有扩展名。 - 使钩子文件可执行,并确保它由git拥有。
- 编写代码,使Git hook功能按预期方式运行。钩子可以是任何语言。确保顶部的“shebang”正确反映了语言类型。例如,如果脚本在Ruby中,shebang可能会是
#!/usr/bin/env ruby
。
二 、开始配置
进入gitlab容器内创建钩子文件(mall-组名 new_mall-项目名)
[root@VM_204_108_centos /]# docker exec -it gitlab /bin/bash root@git:/# cd /var/opt/gitlab/git-data/repositories/mall/new_mall.git/ root@git:/var/opt/gitlab/git-data/repositories/mall/new_mall.git# mkdir custom_hooks/ root@git:/var/opt/gitlab/git-data/repositories/mall/new_mall.git# cd custom_hooks/ root@git:/var/opt/gitlab/git-data/repositories/mall/new_mall.git/custom_hooks# vim post-receive #!/bin/bash # 172.19.0.1 这个为宿主的虚拟IP #我在宿主上边/shell-script/new_mall/git-pull.sh 里边写了同步代码的逻辑 #此处需要生成git用户的id_rsa.pub并导入到宿主的root用户认证中去 这是里就不说了。百度很多 linux公私钥认证 ssh root@172.19.0.1 "sh /shell-script/new_mall/git-pull.sh"
转载于:https://my.oschina.net/u/3429388/blog/1541880