POSTS
Git 與遠端Server 應用
乘上篇文章gitolite 安裝.
當你開心使用git一陣子,又在網路上免費或付費支援git平台的版本控管後是否發覺….
真的好麻煩每次都要同步好幾台SERVER上面的repositories…
有沒有在push上server後自動幫你搞定呢?!
git當初設計實在太人性!!
以下範例:
- 目的:在push 到本機的gitolite server時自動更新一份到bitbucket.org
- 在bitbucket.org 註冊新帳號
- 將gitolite 的使用者git id_rsa.pub 公鑰上傳到bitbucket.org
- bitbucket.org 上開一個測試用的repositories
編輯設定檔:
# cd /home/git/repositories/testing.git
# vi config
加入名為 bitbucket的遠端:
[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote "bitbucket"]
url = git@bitbucket.org:sdpower/mytest.git
fetch = +refs/heads/*:refs/remotes/bitbucket/*
進入hook編輯產生更新時所用到的檔案以post-update:
# cd /home/git/repositories/testing.git
# cd hooks
# cp post-update.sample post-update
# vi post-update
進入hook編輯post-update產生更新時所需要做的動作:
這裡加入了判斷,當網路可以存取才做的動作.
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
RESULT=`wget --spider http://www.google.com.tw/ 2>&1`
FLAG=0
# Traverse the string considering it as an array of words
for x in $RESULT; do
if [ "$x" = '200' ]; then
exec git push bitbucket -f --mirror
fi
done
切回一般使用者到testing做測試:
# git commit -a -m 'test2'
[master 4a07f57] test2
1 files changed, 1 insertions(+), 0 deletions(-)
# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 238 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: remote: bb/acl: sdpower is allowed. accepted payload.
remote: To git@bitbucket.org:sdpower/mytest.git
remote: * [new branch] master -> master
To git@srv1.sd.idv.tw:testing.git
0b01c5a..4a07f57 master -> master
#
成功!!
以下這段就是在push時候所做的動作訊息.
可以前往bitbucket.org確認是否有同步更新.
remote: To git@bitbucket.org:sdpower/mytest.git
remote: * [new branch] master -> master
如有需要可以增加多個遠端,做同步更新.