GitLab持续集成发布之二:安装GitLab-Runner

1,939 阅读4分钟

前言

Git-Runner介绍

Git-Runner是GitLab整体的持续集成发布的最底层一环,也就是脚本执行的地方,开发者在push代码之后,GitLab-CI在监测到有代码提交之后,会解析项目中的.gitlab-ci.yml文件,然后根据pipeline规则在相应的runner上执行相应的脚本

官方网址:gitlab.com/gitlab-org/…

官网上有Git-Runner的详细介绍,本文的一些内容也是参考了官网。

环境信息

  • Git-Runner: 12.8.0
  • 服务器:CentOS7 4C 16G x86_64 GNU/Linux
  • GitLab:社区版12.7.6

部署

安装Git-Runner

安装Git-Runner官网提供了两种方式,一种是使用dep/rpm包进行安装,另外一种是使用资源包进行安装,我们这里使用第二种方式。

  • 根据系统不同下载不同的执行文件
# Linux x86-64
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Linux x86
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386

# Linux arm
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm

# Linux arm64
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm64

可以通过 uname -a 来查看自己系统的版本信息

  • 设置执行权限
sudo chmod +x /usr/local/bin/gitlab-runner
  • 创建一个GitLab CI 用户
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
  • 安装并将其作为服务运行
# 安装
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# 启动
sudo gitlab-runner start
# 停止
sudo gitlab-runner stop

GitLab-Runner注册

Runner在安装好之后呢,需要向GitLab-CI进行注册,相当于告诉上级,我要为你服务,成为你的员工一样,当然一个Runner是可以对多个CI进行注册的;Runner向GitLab进行注册需要两样东西:①GitLab-CI的URL;②注册token。

根据提供Token的不同,可以注册两种不同类型的Runner:

Shared Runner:注册这种Runner,这个GitLab下的所有项目都可以使用这个Runner执行脚本;

Specific Runner:注册这种Runner,就是指定了具体某个项目才可以使用这个Runner执行脚本;

Shared Runner的Token获取路径:Admin area --> Overview --> Runners 打开后,如下图:

Shared-Runner

Specific Runner的Token获取路径:进入具体项目 --> 左侧栏的Setting --> CI/CD --> Runners 点开

Specific

准备好上面两个东西之后,我们就开始注册…… 其他系统注册方式请查阅官网

  • 开始注册
# 注册
sudo gitlab-runner register
# 会提示让你输入GitLab URL
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://X.X.X.X
# 会提示让你输入token
Please enter the gitlab-ci token for this runner
XXX
# 提示输入Runner的描述,这个稍后可以在GitLab的UI中进行更改
Please enter the gitlab-ci description for this runner
my-runner
# 提示输入与Runner关联的标签,这个也可以稍后在GitLab的UI中进行更改
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag
# 输入Runner执行者,这个根据需求自行选择即可
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell

如果一切正常的话,我们会看到

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

这时候,在Admin Area-->Overview-->Runners 下就可以看到刚刚注册的Runner了

GitLab-Runner 安装完成。。

定义项目构建流程

项目的构建流程是由项目根目录的.gitlab-ci.yml文件控制的,关于.gitlab-ci.yml文件的详细说明可参考官网:docs.gitlab.com/ce/ci/yaml/…

以下是一个Java的基于Maven的一个项目的示例:

#定义 stages(阶段)。任务将按此顺序执行。
stages:
  - build
  - test
  - deploy
#定义bulkd Job
job_build:
  stage: build
  script:
    - mvn clean install -Dmaven.test.skip=true -Pdev
  only:
    - develop
  tags:
    - my-tag

job_test:
  stage: test
  script:
    - mvn test
  tags:
    - my-tag

job_deploy:
  stage: deploy
  script:
    - echo "deploy over..."
  tags:
    - my-tag   

说明:这个yml文件就是有3个job组成,依次为build、test、deploy。在执行这个CI流程时候,会分解成3个job依次执行。这里tags: my-tag就是指定使用哪个runner来执行这个job,我们也可以执行其他已注册可用的runner,script下面可以执行多行。

到这里呢,当开发人员往Git上提交代码的时候,就可以实现自动的build、test、deploy啦

参考

  1. GitLab-Runner安装:docs.gitlab.com/runner/inst…

  2. GitLab-Runner注册:docs.gitlab.com/runner/regi…

  3. 构建流程的yml文件说明:docs.gitlab.com/ce/ci/yaml/…

有任何问题欢迎关注公众号【Hugh的白板】私信我,一起探讨,一起学习