git远程服务器部署
添加用户组与用户
$ groupadd git $ useradd git -g git
创建证书登录:
收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件中,把我们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。如果没有该文件创建它:
$ cd /home/git/$ mkdir .ssh $ chmod 755 .ssh $ touch .ssh/authorized_keys $ chmod 644 .ssh/authorized_keys
初始化Git仓库
cd /www/wwwroot/ mkdir mygit $ chown git:git mygit/ $ cd mygit $ git init --bare test.git
把仓库所属用户改为git:
chown -R git:git runoob.git
复制仓库到网站目录
cd site/ git clone /www/wwwroot/mygit/test.git #设置权限 chown -R git test
关键:添加钩子
cd /www/wwwroot/mygit/test.git/hooks # 生成post-receive文件 touch post-receive # 使用vim编辑 vim post-receive
粘贴如下内容
#!/bin/sh # 打印输出 echo '======上传代码到服务器======' # 打开线上项目文件夹 cd /www/wwwroot/site/test # 这个很重要,如果不取消的话将不能在cd的路径上进行git操作 unset GIT_DIR git pull origin master # 自动编译vue项目,如有需要请去掉前面的#号 # npm run build # 自动更新composer(我暂时没试过) # composer update echo $(date) >> hook.log echo '======代码更新完成======'
保存后给 post-receive 文件加上运行权限
chmod +x post-receive
关键知识点:复制过来的git目录,也就是网站目录,必须要有git权限, 钩子文件,也要有git权限与运行权限
文章参考:https://www.runoob.com/git/git-server.html
https://zhuanlan.zhihu.com/p/100834900