1:搭建lnmp环境
2:搭建git仓库服务器
3:编写脚本文件
首先找到仓库hooks的目录,我的目录是 /srv/work.git/hooks, 然后新建一个文件post-receive ,添加可执行权限。
vim post-receive
#!/bin/sh
unset GIT_DIR
NowPath=`pwd`
DeployPath="/home/wwwroot/work/"
cd $DeployPath
#git reset --hard
git pull
git add .
git commit -m '1'
git push test master
cd $NowPath
exit 0
chmod +x post-receive
4. 新建本地仓库关联远程仓库
git clone /srv/work.git
git remote add test git@远程url
5. 本机创建git的ssh
在/home/git/.ssh/ 目录下生成ssh ,然后将公钥添加到远程主机的authorized_keys。
这里需要注意权限的问题,要将远程主机下.ssh的权限改为700,authorized_keys的权限改成600.
6. 在远程主机上创建远程仓库和本地仓库
将本地仓库和远程仓库关联,进入远程仓库的hook目录下编写脚本。
vim post-receive
#!/bin/sh
unset GIT_DIR
NowPath=`pwd`
DeployPath="本地仓库目录"
cd $DeployPath
#git reset --hard
git pull
cd $NowPath
exit 0
chmod +x post-receive
到此就配置好了。
整个过程中遇到的问题就出在5环节,权限问题。和范飞讨论了许久无果,猜测了很多原因,尝试了很多解决办法,最终还是请求邱正刚帮忙解决了这个问题。原因就是权限溢出,给.ssh添加了过多的权限,导致其不能正常工作,设置为700即可ssh连接,没想到linux系统下权限不够不能执行,权限多了也不能执行,还是非常严谨的。