Git hook实现代码自动部署
原理与流程
- git用户执行
git push
操作 - 远程仓库发现有用户执行了push操作,就会执行一个脚本
post-receive
(钩子) - 在
post-receive
脚本中,可以执行自定义的工作。比如自动git pull
,然后运行部署脚本。
Git仓库执行
若代码仓库直接使用git --init
创建,可使用此方法。
在仓库的/hoooks/
目录下,新建文件post-receive
,并赋予执行权限chmod +x post-receive
,然后写入如下内容:
#!/bin/sh
unset GIT_DIR # 取消hooks目录的默认环境
# 以下为自添加脚本内容
cd /path/to/yourproject
git pull
sh deploy.sh
GitLab执行
若代码由GitLab托管到Linux服务器上,则此钩子需在GitLab服务器的文件系统上配置,需要GitLab服务器的管理员权限。
配置流程如下:
1. 添加钩子
导航到需要定制钩子的项目仓库的托管路径(本例为GitLab默认安装路径):
/var/opt/gitlab/git-data/repositories/<group>/<project>.git/
在此位置创建新目录
custom_hooks
- 在新的custom_hooks目录中,创建一个名称与钩子类型匹配的文件,如
post-receive
,不带扩展名。 - 将该hook文件设为可执行文件,如:
chmod g+x post-receive
。 - 编写钩子函数代码,可以是任何语言,只需在钩子文件头部声明即可。如:
#!/bin/sh
#!/usr/bin/env ruby
#!/usr/bin/env python
等。
2. 给Git用户添加sudu权限
由于GitLab使用Git用户来执行钩子文件,所以还需要给Git用户添加执行权限。vi /etc/sudoers
找到root ALL=(ALL) ALL
,新增一行git ALL=<ALL> NOPASSWD:ALL
。
为防止sudo
其他程序命令出现command not found
的问题,你最好新增或修改 secure_path
,添加你需要用到的环境。例如,以下配置新增了nodejs环境,这样其他用户便可以使用sudo node
命令了。
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/data/nodejs/node-v8.9.3/bin
3. Git Pull免密
找到Git用户的根目录,切换为Git用户环境就能看到了su - git
。如果是GitLab自动创建的,它的home目录为/var/opt/gitlab/
,而不是通常认为的 /home/git
。
将root用户的公钥添加到~/.ssh/authorized_keys
文件中。简单操作,就是把id_rsa.pub
复制.ssh
文件夹下,然后cat id_rsa.pub >> authorized_keys
就可以了。
4.脚本命令
钩子脚本示例如下:
#!/bin/sh
unset GIT_DIR
destPath="/data/hexo/hexo-blog" # 仓库目录
depLogPath="$destPath/deploy-log.txt" # 自动部署日志
echo "*****************"
source /etc/profile # 使默认环境变量生效,否则部分命令可能无法使用
echo "who am i:"`whoami`
date >> $depLogPath
echo "post-receive" >> $depLogPath
echo "originPath:"`pwd` >> $depLogPath
cd $destPath
echo "curPath:"`pwd` >> $depLogPath
# 命令前需要添加sudo, 否则可能会没有权限操作
sudo git pull
sudo hexo g -d
echo >> $depLogPath
echo "*****************"
exit 0
需要注意的是,由于Git用户未配置默认的环境变量,所以每次执行都需要使用 source /etc/profile
命令来使环境变量生效,否则,部分命令就会出现command not found
。
需要提醒的是,sudo
命令使用的又是的另外的环境变量,如果你使用sudo来执行命令,你需要确认有没有在上面第2步里面配置好你的secure_path
。
另外,如果你没有在命令前添加sudo
,并且命令含有对文件的写操作,最好确认你的账户对该文件具有写权限,否则操作会失败。一种方法是对需要操作的文件添加读写权限chmod o+rw [filename]
, 如果需要创建文件,则还需要有执行权限。当然,最方便的办法还是在需要用到的命令前加上sudo
指令。
5.效果
如果以上操作都正确配置,你就可以在git push
完成的日志中看到钩子脚本中命令的输出结果。并检查每一步的操作是否成功完成了。
remote: *****************
remote: who am i:git
remote: From:/hexo-blog
remote: 866bd8a..ade66a5 master -> origin/master
remote: Updating 866bd8a..ade66a5
remote: Fast-forward
remote: 3 files changed, 10 insertions(+), 21 deletions(-)
remote: INFO Start processing
remote: INFO Files loaded in 330 ms
remote: INFO Generated: search.xml
remote: INFO Generated: archives/index.html
remote: INFO Generated: archives/2018/index.html
remote: INFO Generated: archives/2018/04/index.html
remote: INFO 9 files generated in 264 ms
remote: *****************
GitLab WebHook
除以上方式外,还可通过GitLab WebHook进行远程部署。部署方法较简单,只是需要另外的web服务器环境,此处暂时不介绍了。
本文参考:
https://docs.gitlab.com/ce/administration/custom_hooks.html
https://stackoverflow.com/questions/24154384/how-can-i-add-hooks-to-gitlab
https://www.cnblogs.com/zhangshiwen/p/7747864.html
http://jiluz.com/2017/03/07/git-hooks/
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 using1174@foxmail.com
文章标题: Git hook实现代码自动部署
文章字数: 1,155
本文作者: Jun
发布时间: 2018-04-11, 19:45:00
最后更新: 2018-04-21, 02:19:26
原始链接: http://yoursite.com/2018/04/11/Git-hook实现代码自动部署/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。