github是可以設置hooks的,看:在設置webhooks & services
,可在Just the push event.
是設定向你的服務器發請求,然后再做相應的處理。
https://help.github.com/articles/creating-webhooks
看文檔:man githooks
NAME
githooks - Hooks used by Git
SYNOPSIS
$GIT_DIR/hooks/*
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.
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.
This document describes the currently defined hooks.
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...