git设置hooks 钩子

 

github是能够设置hooks的,看:在设置webhooks & services,可在Just the push event.是设定向你的服务器发请求,而后再作相应的处理。php

https://help.github.com/articles/creating-webhooksgit

 

看文档:man githooksgithub

NAME
githooks - Hooks used by Gitweb

SYNOPSIS
$GIT_DIR/hooks/*sql

DESCRIPTION
Hooks are little scripts you can place in $GIT_DIR/hooks directory to trigger action at certain points. When git init is run, a handful of example hooks
are copied into the hooks directory of the new repository, but by default they are all disabled. To enable a hook, rename it by removing its .sample
suffix.shell

Note
It is also a requirement for a given hook to be executable. However - in a freshly initialized repository - the .sample files are executable by
default.vim

This document describes the currently defined hooks.bash

 

git hook 自动布署代码

假设你的项目也是跑在此台服务器上,那自动布署代码就很简单了,好比你的在线服务代码在 /var/www/demo 文件夹中。服务器

/var/www/demo也要有写权限工具

你先初始化代码库:

$ git clone /opt/git/gitdemo /var/www/demo 

而后你能够经过 git pull 来更新代码。

固然这样是手动了,我想要的是本地提交更新后,服务器能自动的 git pull代码到最新,因而咱们就要借助 git hook了。

进入到 /opt/git/gitdemo 文件夹中,会发现 .git/hook 文件夹在里面,进入到 hook 中,里面有不少的 sample 脚本,这里咱们只须要用到 post-update。

$ mv post-update.sample post-update $ vim post-update 

能够看到里面其实就是一些shell脚本,你要作的就是把 git pull写进去。当用户提交后,便会调用post-update脚本的。

好比我在服务器的代码库更新后,要求对应的服务代码也要更新(进行pull操做),
则在bare仓库的hooks中的post-receive添加以下内容便可

#!/bin/sh unset $(git rev-parse --local-env-vars) cd WEB_DIR git pull 

这些脚本显然是能够作不少事的,只要你想获得,要了解各脚本什么时候调用,google吧。 

注:服务器中与git用户有关的文件夹及文件,

$ chown -Rh git:git /your/git/dirs

 

另外文章:

最佳工具 git hook 

post-update.sample 更名为post-update

而后加几行简单的代码就能实现你的需求了

例:

gitdir=/****

cd $gitdir

git checkout 对应分支

git pull

end...

相关文章
相关标签/搜索