持续集成 の 使用 git hook 部署代码

持续集成的一个环节就是提交代码后,能即刻部署到测试环境下,以运行各类测试用例。

本文使用 git hook 即可做到代码的简易部署。我们以一个网站为例。

解决方案的思路

你的网站代码在你本地 git 库里,你把代码 push 到远程 git 库里(你的 Web 服务器),通过 post-receive hook 部署到 document root 网站目录下。

示例

原创文章请参见 Using Git to manage a web site,作者 menon-sen 2008年的一篇文章,致敬!
本文有改动,以用户 /home/michael 为例。

The local repository:建库
$ cd
$ mkdir wph/web
$ cd wph/web
$ git init
Initialized empty Git repository in /home/michael/wph/web.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "The humble beginnings of my web site."
The remote repository:建库
$ cd
$ mkdir -p wph/web.git
$ cd wph/web.git
$ git init --bare
Initialized empty Git repository in /home/michael/wph/web.git/
The remote repository:设置 hook
$ cd ~/wph/web.git

# 编辑 hook 脚本
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/home/app/wph/web git checkout -q -f

$ chmod +x hooks/post-receive
  • 请务必加上执行权限,否则不会执行;

hook 设置好后,执行以下任一命令查看效果:
hooks/post-receive. hooks/post-receivesource hooks/post-receive

  • 请确保 GIT_WORK_TREE 所指向的目录存在,否则会报告错误 “fatal: This operation must be run in a work tree”。
网站目录权限
  • 网站目录
    post-receive 脚本中设置的 GIT_WORK_TREE 变量,就是网站目录:
    $ mkdir -p /home/app/wph/web/
  • 目录权限
    假设使用 nginx HTTP Server,网站目录一般都设置为 nginx:nginx。可以把 michael 用户加到 nginx 组内(修改 /etc/group),网站目录 chmod 设置为同组可写。
chown -R nginx:nginx /home/app/wph/web/
chmod -R g+w /home/app/wph/web/

或者
chown -R michael:nginx /home/app/wph/web/,michael 作为 owner,用户 nginx 属于 nginx 组,更方便理解。或者直接设置为 michael:michael 也可以。

The local repository:和 remote 关联
$ cd ~/wph/web
$ git remote add xweb ssh://192.168.99.236/home/michael/wph/web.git
$ git push xweb +master:refs/heads/master

至此,网站目录下就有了 master 分支的完整的文件副本,但不包含 .git metadata(这个很好,类似于 svn export 了)。以后,你只要 git push xweb,在推送到远程库的同时,你的网站目录就得到了更新。

注:

  1. git remote: Manage set of tracked repositories
  2. 假如你本地库是从另外一个远程库复制来的,签出在不同的分支下(不在 master 下),则 git push xweb +master:refs/heads/master 时,会报错,调整一下命令参数即可,比如:git push xweb +v0.3:refs/heads/v0.3
  3. "Error: EPERM: operation not permitted, chmod ..." 错误
    这个错误有时会发生在 gulp 下生成的新目录文件身上。网站目录设置为 michael:nginx 即可;
  4. pull/push from multiple remote locations
git remote set-url origin --add --push <a remote>
git remote set-url origin --add --push <another remote>

这样你就可以同时推送到多个远程库了,而不必一个一个推送。

git push <repository> [<refspec>…​]

  • <refspec>…​

Specify what destination ref to update with what source object. The format of a <refspec> parameter is an optional plus +, followed by the source object <src>, followed by a colon :, followed by the destination ref <dst>.

The <src> is often the name of the branch you would want to push, but it can be any arbitrary "SHA-1 expression", such as master~4 or HEAD.

The <dst> tells which ref on the remote side is updated with this push. The object referenced by <src> is used to update the <dst> reference on the remote side. By having the optional leading +, you can tell Git to update the <dst> ref even if it is not allowed by default.

To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch)

  • 示例
git remote add prod-web ssh://192.168.99.236/home/git/wph/web.git
git push prod-web +v0.3:refs/heads/v0.3
- `+v0.3:refs/heads/v0.3`:+<src>:<dst>;省略 `:<dst>`,则表示推送到远程同名 ref;
- `+`表示强制推送,allow non-fast-forward updates;
- 为了简单起见,通常 +v0.3:master 即可,即将本地 v0.3 推送到远程 master(这样远程签出 master 到站点目录,而不必每次都修改签出目录);

post-receive 示例(发布指定分支)

#!/bin/bash

target_branch="production"
working_tree="PATH_TO_DEPLOY"

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ -n "$branch" ] && [ "$target_branch" == "$branch" ]; then
    
       GIT_WORK_TREE=$working_tree git checkout $target_branch -f
       NOW=$(date +"%Y%m%d%H%M%S")
       git tag release_$NOW $target_branch
    
       echo "   /==============================="
       echo "   | DEPLOYMENT COMPLETED"
       echo "   | Target branch: $target_branch"
       echo "   | Target folder: $working_tree"
       echo "   | Tag name     : release_$NOW"
       echo "   \=============================="
    fi
done

备注


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 安装 Github 查看是否安装git: $ git config --global user.name "...
    Albert_Sun阅读 13,728评论 9 163
  • 本文作者陈云峰,转载请注明。 这篇文章记录个人常用的一些命令,和记不住的一些命令,转载了并不断更新。 Git官网 ...
    陳云峰阅读 2,846评论 0 24
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 领导是组织的思想,管理是组织的行为;领导是做正确的决策,管理是顺着决策正确地做;领导是把梯子搭在成功的墙上,管理是...
    新呐喊阅读 701评论 2 7
  • 2017.3.12 一,亲子话题,如何均衡二胎 二,婚姻家庭,如何让婚姻更幸福 三,美肤类,气质优雅女神课程 一,...
    80后女诸葛阅读 194评论 1 0