情景
假如你在本地的虛擬機(jī)或者內(nèi)網(wǎng)服務(wù)器下部署了gitlab,將版本控制本地化或內(nèi)網(wǎng)化,那如何實(shí)現(xiàn)將本地的gitlab倉(cāng)代碼部署到線上的服務(wù)器呢?
實(shí)現(xiàn)步驟
1.本地搭建gitlab版本控制器
2.將項(xiàng)目部署到本地的gitlab倉(cāng)庫(kù)上
3.開(kāi)發(fā)人員正常在內(nèi)網(wǎng)下上傳,克隆代碼
4.通過(guò)配置gitlab的遠(yuǎn)程登錄服務(wù)器,將本地代碼推送到遠(yuǎn)程服務(wù)器
實(shí)現(xiàn)過(guò)程
建立倉(cāng)庫(kù)
- 在gitlab上建立項(xiàng)目,(將本地項(xiàng)目初始化到gitlab上)
- 在遠(yuǎn)程服務(wù)器上建立裸倉(cāng)庫(kù)
部署ssh-keys
- 生成 ssh-keygen -t rsa -C "your_email@example.com"
- 在gitlab部署本地的ssh-key
-
在遠(yuǎn)程服務(wù)器部署gitlab的ssh-key(對(duì)應(yīng)公鑰)
image.png
部署鉤子
-
在gitlab的.ssh文件的config文件配置相關(guān)信息
image.png - 在gitlab的倉(cāng)庫(kù)的下創(chuàng)建custom_hooks文件夾,并在custom_hook下創(chuàng)建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
- 在遠(yuǎn)程倉(cāng)庫(kù)下創(chuàng)建hooks文件夾,并在hooks下創(chuàng)建post-receive,編輯
#!/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
- 添加host到本地gitlab服務(wù)器的kown_hosts
ssh-keyscan -t rsa ip>> ~/.ssh/known_hosts

