Gitlab custom_hooks 全局&单个仓库设置

实验环境:本地docker 搭建的 gitlab  版本:20.04

一、全局hook设置

1.设置全局hook

进入gitlab 容器内 

全局的hook,配置脚本后gitlab系统上所有仓库都受影响,开关在配置文件gitlab.rb中

gitlab.rb默认路径:/etc/gitlab/gitlab.rb

修改gitlab.rb中的字段,找到下面这条:

#gitaly['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks"  //路径可以改为自己配置hook脚本的路径

去掉注释,配置自己的hook脚本存放路径。

例:

设置完成后,执行以下命令重新编译gitlab 让配置生效

#运行时间较长,不要急
gitlab-ctl reconfigure
gitlab-ctl restart

2.添加脚本

进入在gitlab.rb 中 gitaly['custom_hooks_dir'] 配置的hook路径

cd /var/opt/gitlab/gitaly/custom_hooks

服务器端hook简介:

post-receive:在服务端收到文件之后,比如git push XXX 之后,服务端上已经能查到提交信息之后自动触发。

pre-receive:在客户端git push 之后但是服务端还没有收到文件时触发。

update:如果有多个分支推送内容则每一个分支在把内容推送到服务端之前都会触发一次,相比于pre-receive只会触发一次。
 

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

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

       pre-receive.d

       update.d

       post-receive.d

在每个文件夹下可创建任意文件,在对应的 hook 时期,gitlab 就会主动调用

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

目录结构示意:

[root@localhost custom_hooks]# tree
.
├── post-receive.d
│   ├── 01.sh
│   └── 02.sh~
├── pre-receive.d
│   ├── 01.sh
│   ├── 02.py
│   └── 03.rb
└── update.d
    ├── 01.sh
    └── 02.sh

在/var/opt/gitlab/gitaly/custom_hooks下建pre-receive.d目录

在pre-receive.d下写脚本

#!/bin/sh
 
echo "Say hi from gitlab server"
exit 1
 
#脚本含义:当用户提交代码时,打印"Say hi from gitlab server" 并拒绝用户代码提交

3.给脚本赋权

cd /var/opt/gitlab/gitaly
chmod 777 -R custom_hooks
chown -R git:git custom_hooks
cd /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d
chmod +x a.sh

4.实验验证

提交代码

 符合预期:当用户提交代码时,打印"Say hi from gitlab server" 并拒绝用户代码提交

二、单个代码仓库hook设置

1.配置单个代码仓库hook

配置单独仓库,只对单个仓库生效:

注意:按单个仓库配置的时候,必须也把全局设置中的gitlab.rb文件中的设置打开,指定的目录可以什么都不放,gitlab会自动跳过,也不会报错。

单个仓库配置就必须找到单个仓库实际地址,在仓库目录下加上custom_hooks文件夹,然后再添加hook脚本

仓库地址获取方式:

 仓库目录下建custom_hooks目录

例如目录:

/var/opt/gitlab/git-data/repositories/@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git/custom_hooks

2.添加脚本

custom_hooks目录下建这三个目录(这部分与设置全局hook相同,同上)

pre-receive.d

update.d

post-receive.d

在目录:/var/opt/gitlab/git-data/repositories/@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git/custom_hooks/pre-receive.d 下添加脚本

#!/bin/sh
 
echo "Say hi from gitlab server admin/projects/luna/xueqing"
exit 1
# 脚本含义:当用户提交代码时,打印"Say hi from gitlab server admin/projects/luna/xueqing" 并拒绝用户代码提交

3.给脚本赋权 

cd /var/opt/gitlab/git-data/repositories/@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git
chmod 777 -R custom_hooks
chown -R git:git custom_hooks
cd /var/opt/gitlab/git-data/repositories/@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git/custom_hooks/pre-receive.d
chmod +x a.sh

4.实验验证

提交代码

符合预期:当用户提交代码时,打印"Say hi from gitlab server admin/projects/luna/xueqing" 并拒绝用户代码提交

三、注意

当全局hook与单个代码仓库hook同时存在时,单个代码仓库配置的hook优先级高于全局hook (即配置了单独代码仓库hook,不再执行全局hook)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值