GitLab CI的入门搭建

2019-11-17  本文已影响0人  何小有

搭建一个GitLab CI环境分两步

在服务器配置GitLab Runner

GitLab Runner是一个用来执行持续集成脚本的网络服务,它的工作模式是

  1. 轮询GitLab仓库
  2. 一旦发现GitLab仓库中的代码分支有变化,就在服务器的工作空间内pull(拉取)最新代码
  3. 并执行项目目录下.gitlab-ci.yml中的持续集成脚本
  4. 把服务器终端的命令行日志返回给GitLab CI

下面我们在Ubuntu中配置GitLab Runner项目

安装GitLab Runner

Ubuntu系统安装什么都很简单,GitLab Runner也不例外,就下面两行代码

# 命令增加GitLab官方仓库
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
# 安装最新版本的GitLab Runner
$ sudo apt-get install gitlab-runner

注册GitLab Runner

还需要配置一下GitLab仓库的相关信息,这样GitLab Runner才能轮询GitLab仓库与GitLab CI持续集成

# 注册GitLab Runner对应的GitLab信息
$ sudo gitlab-runner register

上面的命令会要我们输入5个信息,分别是

$ Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com)
$ [GitLab URL,在GitLab仓库的`Settings -> CI/CD -> Runners`下查看]
$ Please enter the gitlab-ci token for this runner
$ [注册token,在GitLab仓库的`Settings -> CI/CD -> Runners`下查看]
$ Please enter the gitlab-ci description for this runner
$ [这个Runner的名称]
$ Please enter the gitlab-ci tags for this runner (comma separated):
$ [这个Runner的tag]
$ Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
$ [这个Runner的执行器类型,这里用最简单的shell就好了]

注册如果顺利的话,后面的两步就非常简单了

通过.gitlab-ci.yml编写脚本

在GitLab仓库的目录下新建.gitlab-ci.yml脚本文件,正常情况下GitLab的项目主页顶部会提供快捷方式,可以一键创建.gitlab-ci.yml脚本文件

编辑.gitlab-ci.yml脚本文件,这里为了演示,就使用最简单的方式写了一个输出“GitLab仓库代码在服务器的工作空间目录”的shell脚本

stages:
    - test

test-job:
    stage: test
    script:
        - echo `pwd`
    tags:
        - [上一步配置的Runner的tag名称]

当你编辑完.gitlab-ci.yml脚本文件,点击保存后,在GitLab仓库的Settings -> CI/CD -> Pipelines下,就会发现你刚才保存的代码更改,成功触发了GitLab Runner服务,在服务器上执行了持续集成脚本,并可以直接看到服务器终端的执行输出及结果

服务器终端的执行输出及结果

到这里就已经完成了GitLab CI的搭建

上一篇下一篇

猜你喜欢

热点阅读