配置gitlab服务器的git hooks 钩子

需求是本地开发分支develop推到远程仓库后自动部署测试服务器。

看很多文章,讲的非常复杂。都没讲清楚架构情况。

我们的架构是,多台本地开发机器,一台服务器,服务器上跑着测试程序和gitlab。

这边文章解决的是测试端和git远程仓库在一台机器上的情况,如果是测试程序和gitlab各自一台服务器,那比较麻烦了。

1.找到项目对应的hooks文件夹

/var/opt/gitlab/git-data/repositories/root/xxx.git/hooks/

2.修改post-receive文件

#!/bin/bash

GIT_WORK_TREE=/www/xxx
BRANCH=develop

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

BRANCH 是分支名
GIT_WORK_TREE 是测试程序目录
修改完保存。

3.设置git账户对post-receive文件执行权限。
/www/xxx目录权限也一样。
贪图省事就chmod 777。

4.测试

git push origin develop

能看到develop ref received. Deploying develop branch to testserver…

再看/www/xxx目录下文件都已经更新了。

2018.11.5 更新
这个方法其实是有问题的。
目录/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放在该目录下。

详见:https://docs.gitlab.com/ee/administration/custom_hooks.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值