打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用 Git Hooks 实现自动项目部署
 

使用 Git Hooks 实现自动项目部署

In Technology Tag: git

最近在某服务器上面搭建 git 开发和部署环境,git 开发环境很简单,按照 ProGit 一书的相关知识就可以轻松搞定,实现了类似 Github 的使用 SSH + 私有 Clone 的方式。

关于部署,实际上是自动部署,起初的想法是使用 bash shell 制定一个定时任务去不断 git pull 产品代码,后来记得 Git 带有 Hooks,索性在 ProGit 一书翻了翻:

Git 本身可以调用自定义的挂钩脚本,其中有两组:客户端和服务器端。客户端挂钩用于客户端的操作,如提交和合并。服务器端挂钩用于 Git 服务器端的操作,如接收被推送的提交。详情请查看 ProGit 相关章节

如果这样就简单了,利用服务器端调用想要的挂钩(Hook),即可实现自动部署的方案,为了保证不被肆意部署,特加了一个对需要部署 commit 的判断,利用读取 commit subject 并匹配想要的字符串才去部署,这样我认为是一个比较安装的部署方案。

Git 的挂钩(Hook)主要包含:

  • applypatch-msg
  • post-update
  • pre-rebase
  • commit-msg
  • pre-applypatch
  • update
  • post-commit
  • pre-commit
  • post-receive
  • prepare-commit-msg

这里我们只需要使用 post-receive 这个 Hook:在接收 post(push)
请求之后执行。其他大部分我也没有大多研究,不过看名字不算难理解,我觉得其中大部分包含 commit 的属于客户端。

好了,部署开始:

1. 在服务器 git 仓库(注意是 bare 仓库,不是代码仓库)的 Hooks,编辑
post-receive(如果没有自行创建),代码请看:

http://gist.github.com/566767):

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#!/bin/sh
#
# git autodeploy script when it matches the string "[deploy]"
#
# @author icyleaf <icyleaf.cn@gmail.com>
# @link http://icyleaf.com
# @version 0.1
#
# Usage:
# 1. put this into the post-receive hook file itself below
# 2. `chmod +x post-recive`
# 3. Done!
 
# Check the remote git repository whether it is bare
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ]; then
echo >&2 "fatal: post-receive: IS_NOT_BARE"
exit 1
fi
 
# Get the latest commit subject
SUBJECT=$(git log -1 --pretty=format:"%s")
 
# Deploy the HEAD sources to publish
IS_PULL=$(echo "$SUBJECT" | grep "\[deploy\]")
if [ -z "$IS_PULL" ]; then
echo >&2 "tips: post-receive: IS_NOT_PULL"
exit 1
fi
 
# Check the deploy dir whether it exists
DEPLOY_DIR=/home/icyleaf/php/icyleaf/
if [ ! -d $DEPLOY_DIR ] ; then
echo >&2 "fatal: post-receive: DEPLOY_DIR_NOT_EXIST: \"$DEPLOY_DIR\""
exit 1
fi
 
# Check the deploy dir whether it is git repository
#
#IS_GIT=$(git rev-parse --git-dir 2>/dev/null)
#if [ -z "$IS_GIT" ]; then
# echo >&2 "fatal: post-receive: IS_NOT_GIT"
# exit 1
#fi
 
# Goto the deploy dir and pull the latest sources
cd $DEPLOY_DIR
#env -i git reset --hard
env -i git pull
view raw post-receive.sh hosted with ? by GitHub

这里会先判断脚本所在目录是否是 bare git 仓库,然后获取最新 commit 的 subject,并匹配是否包含 [deploy] 字样,如果包含,则继续检查产品代码仓库路径是否存在,如果存在则执行 git pull 操作。

2. 对刚才编辑的 post-receive 执行下面命令以保证脚本可执行:

$ chmod +x post-receive

3. 完成!


对于自定义脚本,其实不仅限于 bash shell,你可以使用你熟悉的语言,然后把你的脚本路径在 hooks 脚本中加载即可。

脚本还会继续更新,下面需要增加关于测试部分的相关判断和部署。 bash shell 还需要进一步学习,上面脚本是我第一次写,如有不妥之处,请指教,感谢!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
将项目从 GitHub 部署到服务器 – 码农网
jenkins结合ansible用shell实现自动化部署和回滚
在Git上如何强推代码规范
Jenkins Ansible Gitlab自动化部署三剑客 | 一路向北的博客
git全局钩子 | Yet Another Summer Rain
DevOps通用及版本控制面试题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服