Py学习  »  Git

I-team博客的gitlab-runner持续集成实践

haifeiWu • 5 年前 • 450 次点击  

做为一个略微看过nodejs语法,但又不懂nodejs的攻城狮,搭建hexo环境很是麻烦,要考虑到翻墙、版本兼容等问题。于是乎,博主每换一个电脑,为了能继续发博客,都需要在新电脑上花一天时间重新搞一下 hexo 环境,楼主感觉还是有简洁的方案来实现我一提交代码就可以自动发布博客,不需要再手动操作一波,这样岂不美哉。so,也就有了今天的经历,代码可以持续集成,博客也可以。楼主的解决方案是使用gitlab与gitlab-runner实现博客部署的持续集成,效果真的不要太好。

持续集成工具 gitlab-runner 介绍

gitlab-ci全称是gitlab continuous integration的意思,也就是持续集成。中心思想是当每一次push到gitlab的时候,都会触发一次脚本执行,然后脚本的内容包括了测试,编译,部署等一系列自定义的内容。而gitlab-runner 是 gitlab 提供的持续集成工具。

简单的说,要让CI工作可总结为以下几点:

  • 在仓库根目录创建一个名为.gitlab-ci.yml 的文件。
  • 为该项目配置一个runner服务,楼主这里使用的是使用gitlab提供代码厂库,在自己的腾讯云服务器上运行gitlab-runner服务。
  • 完成上面的步骤后,每次push代码到Git仓库, runner就会自动开始pipeline。

gitlab-ci的具体部署流程如下图所示(图来自网络,侵权删)
gitlab-runnergitlab-runner

Hexo 博客环境迁移

迁移前版本控制

其实每个nodejs工程根目录下都有一个package.json文件,里面都包含了我们所用的插件信息,只需要我们在安装插件的时候注意加上–save,就会自动把插件信息保存到 package.json 中。

如果目录下没有 package.json 文件也不要紧,在跟目录命令行中运行 npm init 即可生成。

博客环境安装

前面做好版本控制,那接下来的事情就好做了。

  1. 备份你的代码,注意:代码中不需要包含 node_modules 文件夹了
  2. 先在新电脑中装上 nodejs 环境
  3. 由于国内安装 npm 的一些插件需要翻墙,所以这里直接用淘宝镜像:cnpm,安装方法:npm install -g cnpm –registry=registry.npm.taobao.org
  4. 安装hexo客户端:cnpm install hexo-cli -g
  5. 新建博客目录:hexo init
  6. 把你备份的代码放到此目录下,如果有重复的文件直接覆盖就行
  7. 安装 hexo 插件:cnpm install
    就这样,新的博客环境迁移完成了,执行 hexo s 开始你新的博客征程吧!

gitlab-runner环境搭建

gitlab-runner的安装

使用gitlab官网提供的下载地址太慢,所以找到了一个国内的镜像地址:

  1. 新建 gitlab-ci-multi-runner.repo

    touch /etc/yum.repos.d/gitlab-ci-multi-runner.repo
    
  2. 将以下内容写入文件

[gitlab-ci-multi-runner]
name=gitlab-ci-multi-runner
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ci-multi-runner/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
  1. 执行
sudo yum makecache
sudo yum install gitlab-ci-multi-runner
  1. 以上是楼主在centos上的安装过程,其他系统版本的安装请移步gitlab-runner其他系统版本的安装

gitlab-runner注册到gitlab官网

在终端输入gitlab-runner register 会出现以下过程:

[root@localhost ~]# gitlab-runner register
Running in system-mode.                            

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/
Please enter the gitlab-ci token for this runner:
your gitlab-ci token
Please enter the gitlab-ci description for this runner:
[localhost.localdomain]: my-runner
Please enter the gitlab-ci tags for this runner (comma separated):
your tag
Whether to run untagged builds [true/false]:
[false]: true
Registering runner... succeeded                     runner=c5552857
Please enter the executor: parallels, shell, virtualbox, docker+machine, docker-ssh+machine, docker, docker-ssh, ssh, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

在注册过程中有两个比较重要的参数一个是gitlab的URL,另一个就是注册的token,这两个参数可以在gitlab上找到,过程是Settings>CI/CD>Runners settings>Specific Runners,如下图所示
gitlab-runner-settingsgitlab-runner-settings

另外还需要打开
gitlab-runner-settingsgitlab-runner-settings

要是自己注册的gitlab-runner生效还学要禁用Shared Runners

以上过程是楼主在centos上操作的,其他版本请移步gitlab-runner注册到gitlab

创建.gitlab-ci.yml,并放着工程的根目录下

.gitlab-ci.yml具体配置请移步官方文档,下面给出楼主使用的.gitlab-ci.yml具体内容

variables:
  GIT_STRATEGY: none

stages:
  - build_and_deploy

job:
  stage: build_and_deploy
  script:
    - cd /opt/I-team-fly
    - git pull --tags origin dev
    - hexo clean
    - hexo g
    - hexo d
  only: 
    - dev

查看gitlab上的构建结果

gitlab-runner-settingsgitlab-runner-settings

小结

当然这个过程中还是要涉及到几次使用ssh-key来设置免密登录,楼主就不在这里赘述了,请遇到问题的小伙伴自行Google。

参考文章


今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/F5dnpphgRt
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/13361
 
450 次点击