git自动部署代码
服务器创建裸仓库:
# git init --bare test.com.git
# ls test.com.git
# branches config description HEAD hooks index info objects refs
配置自动部署代码的脚本:
#vi hooks/post-receive
#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch to production..."
sudo git --work-tree=/data/gitroot/test.com --git-dir=/data/git/test.com.git checkout -f
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
sudo chown -R www.www /data/gitroot/test.com/
done
# 这里采用sudo 是为了解决git代码目录的权限问题。
# 链接:https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
权限问题:git:git nginx:www
1、git都是以git用户运行,checkout输出代码也是git用户,
2、nginx运行是以www用户运行,
解决办法:git拥有高级权限,且sudo 无需输入密码
注释如下行:
#Defaults requiretty
添加如下行:
git ALL=(ALL) NOPASSWD:ALL
把git用户添加到www用户组
usermod -a -G www git
网友评论