root是某个gitlab用户
xxx.git是xxx项目的git仓库根目录
在gitlab里
目录 /var/opt/gitlab/git-data/repositories/root/xxx.git/hooks/
是一个连接指向/opt/gitlab/embedded/service/gitlab-shell/hooks/
我们需要到
目录/var/opt/gitlab/git-data/repositories/root/xxx.git/下
新建 custom_hooks目录
把post-receive放在该目录下。
操作如下
#切换目录
cd /var/opt/gitlab/git-data/repositories/root/xxx.git
#创建钩子目录
mkdir custom_hooks
#赋给权限
chown git:git custom_hooks
chmod 755 custom_hooks
cd custom_hooks
#创建 post-receive 钩子
vi post-receive
#保存post-receive内容后
#赋给权限
chown git:git post-receive
chmod 755 post-receive
#切换项目根目录
cd /var/www/html
mkdir erp
chmod 777 -R erp
post-receive的内容
#!/bin/bash
# /var/www/html/erp是你项目的根目录 checkout -f 是自动检出
git --work-tree=/var/www/html/erp checkout -f
# master是需要检出的分支
BRANCH=master
while read oldrev newrev ref
do
if [[ $ref =~ .*/${BRANCH}$ ]];
then
echo "$BRANCH ref received. Deploying $BRANCH branch to testserver..."
git --work-tree=${GIT_WORK_TREE} checkout ${BRANCH} -f
else
echo "Ref $ref successfully received. Doing nothing: only the $BRANCH branch may be deployed on this server."
fi
done
详见:https://docs.gitlab.com/ee/administration/custom_hooks.html
参考:https://blog.csdn.net/daisho/article/details/83272801