GitLab Runner


GitLab Runner

GitLab Runner是与GitLab的CI/CD执行环境,是GitLab的一个工具包。GitLab Runner就是自动化部署任务的执行环境。你编写的一条自动化部署的流水线,包含了安装,测试,部署三个任务,这三个任务在哪个环境下执行那,就是在Runner中。没有Runner,GitLab CI/CD就没办法运行。要想做GitLab CI/CD,首先要有一个正常的GitLab Runner。

安装GitLab Runner

GitLab 官方提供了很多种安装Runner的方式,Dokcer,Linux,macOS,Windows,Kubernetes等。
这里我们介绍一种最简单的安装方式,这种方式只需要一条命令,而且卸载删除,可以无任何遗留。非常方便快捷,那就是使用Docker来安装部署。

使用本地卷安装GitLab Runner:

docker run -d --name gitlab-runner --restart always \
     -v /srv/gitlab-runner/config:/etc/gitlab-runner \
     -v /var/run/docker.sock:/var/run/docker.sock \
     gitlab/gitlab-runner:latest

使用本地卷来安装Runner,数据会被保存在本地。

使用Docker 卷安装GitLab Runner:

# docker volume create gitlab-runner-config

docker run -d --name gitlab-runner --restart always \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v gitlab-runner-config:/etc/gitlab-runner \
    gitlab/gitlab-runner:latest

# 重启
docker restart gitlab-runner
# 重启
docker restart gitlab-runner
# 查看Runner 日志
docker logs gitlab-runner

注册GitLab Runner

安装成功了Runner后,还不能用。目前只是安装,还没配置,还没有注册,还没有与GitLab的项目进行绑定。

sudo gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.com/" \
  --registration-token "PROJECT_REGISTRATION_TOKEN" \
  --executor "docker" \
  --docker-image alpine:latest \
  --description "docker-runner" \
  --tag-list "docker,aws" \
  --run-untagged="true" \
  --locked="false" \
  --access-level="not_protected"

对于使用本地卷安装

docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register

使用Docker 卷安装

docker run --rm -it -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest register

执行后会询问,输入一下变量
输入 gitlab的地址 输入gilab-ci的token

Runner的executor 要写 docker

如果你选择了docker 作为执行工具,你会被要求填写一个默认镜像 没有在.gitlab-ci.yml中定义的

除了gitlab地址和token 不能改, 其他都可以在gitlab的gui上能改

配置GitLab Runner

配置Runner主要是配置一个Runner可以并行执行多少任务,docker 卷,cpu,dns,内存,缓存目录,当然你不用一个一个去配置。没有特殊要求,直接使用默认的就可以了。

下面说下一如何配置Runner。上一步我们已经安装了Runner,并将配置文件映射出来了。在目录/srv/gitlab-runner/config/中可以看到一个文件config.toml,这个文件就是Runner 的配置文件。

Runner有几个属性是全局配置,无论你是用什么环境作为执行器都可以配置这几个参数。分别是:

  • concurrent 多少个任务并行执行,
  • log_level 日志等级 debug, info, warn, error, fatal, panic
  • log_format 日志格式化 runner, text, json
  • check_interval 多少秒检查一次新的任务
  • sentry_dsn 允许追踪所以系统错误到sentry
  • listen_address Prometheus 的http监控地址

更多详情配置可以直接查看官方介绍

Docker 部分的配置

为什么会有那么多配置那?因为Runner的安装方式有很多种,安装Runner的环境也有很多种。

runners 部分

Setting Description
name Runner 的名字 描述
url GitLab 地址
token The Runner’s special token (not to be confused with the registration token)
tls-ca-file File containing the certificates to verify the peer when using HTTPS
tls-cert-file File containing the certificate to authenticate with the peer when using HTTPS
tls-key-file File containing the private key to authenticate with the peer when using HTTPS
limit Limit how many jobs can be handled concurrently by this token. 0 (default) simply means don’t limit
executor Select how a project should be built, see next section
shell Name of shell to generate the script. Default value is platform dependent.
builds_dir Absolute path to a directory where builds will be stored in context of selected executor (Locally, Docker, SSH)
cache_dir Absolute path to a directory where build caches will be stored in context of selected executor (locally, Docker, SSH). If the docker executor is used, this directory needs to be included in its volumes parameter.
environment Append or overwrite environment variables
request_concurrency Limit number of concurrent requests for new jobs from GitLab (default 1)
output_limit Set maximum build log size in kilobytes, by default set to 4096 (4MB)
pre_clone_script Commands to be executed on the Runner before cloning the Git repository. this can be used to adjust the Git client configuration first, for example. To insert multiple commands, use a (triple-quoted) multi-line string or “\n” character.
pre_build_script Commands to be executed on the Runner after cloning the Git repository, but before executing the build. To insert multiple commands, use a (triple-quoted) multi-line string or “\n” character.
post_build_script Commands to be executed on the Runner just after executing the build, but before executing after_script. To insert multiple commands, use a (triple-quoted) multi-line string or “\n” character.
clone_url Overwrite the URL for the GitLab instance. Used if the Runner can’t connect to GitLab on the URL GitLab exposes itself.
debug_trace_disabled Disables the CI_DEBUG_TRACE feature. When set to true, then debug log (trace) will remain disabled even if CI_DEBUG_TRACE will be set to true by the user.
referees Extra job monitoring workers that pass their results as job artifacts to GitLab