情景
假如你在本地的虛擬機或者公司內網服務器部署了gitlab,希望版本控制管理本地化或內網化,那如何實現版本控制管理本地化或內網化的同時,能夠將項目基於版本控制管理部署在線上服務器,這就是以下要討論的問題。
上面方案存在的關卡
-
遠程服務器如何同步或克隆本地gitlab的代碼
-
gitlab如何項目實現自動化同步部署遠程服務器
實現過程
建立倉庫
-
在gitlab建立項目(即在gitlab建立倉庫過程)
-
在遠程服務器建立裸倉庫
-
本地克隆gitlab倉庫
部署ssh-keys
- 生成
ssh-keygen -t rsa -C "your_email@example.com"
-
在gitlab部署本地ssh-key
第一步:
第二步:
- 在遠程服務器部署gitlab的ssh-key
部署鈎子
- 在gitlab的.ssh文件的config文件配置相關信息
host test
hostname 0.0.0.0 #你的主機地址
user root
port 22
identityfile ~/.keys/test #你的私鑰地址
- 在gitlab的倉庫的下創建custom_hooks文件夾,並在custom_hook下創建post-receive,編輯
#!/bin/sh
# example hook script for the "post-receive" event.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".
#. /usr/share/git-core/contrib/hooks/post-receive-email
while read oldrev newrev ref
do
branch=$(git rev-parse --symbolic --abbrev-ref $ref)
if [ "$branch" = "1.0" ]
then
git push -f test:/var/www/html/test.git $branch
fi
done
- 在遠程裸倉庫部署鈎子
#!/bin/sh
#
# An example hook script for the "post-receive" event.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".
#. /usr/share/git-core/contrib/hooks/post-receive-email
while read oldrev newrev ref
do
if [[ $ref =~ .*/1.0$ ]];
then
echo "1.0 ref received. Deploying 1.0 branch to test server..."
git --work-tree=/var/www/html/test--git-dir=$GIT_DIR checkout -f $ref
fi
done
測試
- 在本地新建文件,提交,如何測試服能自動更新,即部署成功
注意:
(1)出現下面
原因:gitlab未把host添加進kown_hosts