服务器自动化部署--gitlab

1 篇文章 0 订阅
1 篇文章 0 订阅

情景

假如你在本地的虚拟机或者内网服务器下部署了gitlab,将版本控制本地化或内网化,那如何实现将本地的gitlab仓代码部署到线上的服务器呢?

实现步骤

  1. 本地搭建gitlab版本控制器
  2. 讲项目部署到本地的gitlab仓库上
  3. 开发人员正常再内网下上传,克隆代码
  4. 通过配置gitlab的远程登录服务器,将本地代码推送到远程服务器

实现过程

建立仓库
 1. 在gitlab上建立项目(将本地项目初始化到gitlab上)
 2. 在远程服务器上建立裸仓库
部署ssh-keys
  1. 生成 ssh-keygen -t rsa -C “your_email@example.com”
  2. 在gitlab部署本地的ssh-key
  3. 在远程服务器部署gitlab的ssh-key(对应公钥)
    在这里插入图片描述
部署钩子
  1. 在gitlab的.ssh文件的config文件配置相关信息(注意要将用户切换到git用户)
    在这里插入图片描述
    2.在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

3.在远程仓库下创建hooks文件夹,并在hooks下创建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

4.添加host到本地gitlab服务器的kown_hosts

ssh-keyscan -t rsa ip>> ~/.ssh/known_hosts

5.测试看是否成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值