四畳半神话大系

Git hook 实现自动部署

修改hook

在gitosis仓库中 xxx.git/hook 下修改post-receive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh
#判断是不是远端仓库
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ]; then
echo >&2 "fatal: post-receive: IS_NOT_BARE"
exit 1
fi
unset GIT_DIR
DeployPath="/var/www"
FileName="xxx"
echo "==============================================="
cd $DeployPath
# 判断文件是否存在
if [ ! -d $FileName ]; then
git clone /<thisPath>/<thisproject>.git
fi
cd $FileName
echo "deploying the web"

#git pull origin master
git fetch --all
git reset --hard origin/master

time=`date`
echo "web server pull at webserver at time: $time."
echo "================================================"

其中DeployPath为需要部署的文件路径

1
2
3
4
cd /var/www
git init
git remote add origin xxx.git的绝对路径
git fetch && git checkout master

权限问题

为了解决Permission denied 问题
需要更改文件夹用户所属

1
chown -R git:git www/

为什么指定是git用户呢
因为在gitosis repositories中 xxx.git 的用户也是git

1
2
drwxr-xr-x. 7 git git 139 May 26 22:57 gitosis-admin.git
drwxr-x---. 6 git git 84 May 27 02:45 xxx.git

其他

如果git项目含有submodule
除了源程序git推送外,submodule也需要推送到服务器上
在post-update 中加入

1
2
git submodule init
git submodule update

本文发表于 2018-11-13,最后修改于 2020-04-04。

本站永久域名blog.amoyiki.com,也可搜索「 四畳半神话大系 」找到我。

期待关注我的 ,查看最近的文章和动态。


上一篇 « Flume + zookeeper + kafka收集Nginx日志 下一篇 » 系统限流相关操作

推荐阅读

Big Image