gongdear

gongdear的技术博客

欢迎大家参观我的博客
  menu
104 文章
89355 浏览
1 当前访客
ღゝ◡╹)ノ❤️

gitlab-runner 安装使用

gitlab-runner安装使用

gitlab-runner 是一个开源的与 gitlab CI 配合使用的项目,用于运行任务,并将结果返回 gitlab
本文通过docker in docker的方式,即在具有特权模式的 Docker 中使用 Docker,通过 gitlab-runner 使用 docker 的来构建项目,完成打包,测试,发布等任务。这样 gitlab-runner 只是需要 Dockerfile 即可,可方便日后使用其他 CI/CD 工具。同时本地调试也十分方便
文档地址:https://docs.gitlab.com/runner

安装

参考文档 https://docs.gitlab.com/runner/install/docker.html

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 

注册

安装好 gitlab-runner 后,需要进行注册,gitlab 才会出现 runner 列表。注册前需要准备好地址和 token,管理员进入管理中心>概览>Runner可查看 runner 列表、url 和注册令牌
如果想要为某群组或者项目设置 runner,可在对应群组或者项目的设置>CI/CD>Runner获取 url 和 token
首先进入容器交互模式

docker exec -it gitlab-runner /bin/bash

输入命令gitlab-runner register进行注册,按照提示和文档说明一步步填写即可
也可一条命令完成注册

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" 

注册 runner 参考:https://docs.gitlab.com/runner/register/index.html#docker
docker 执行者:https://docs.gitlab.com/runner/executors/docker.html

配置

在配置文件/etc/gitlab-runner/config.toml中添加 privileged 标记,这样可以在容器中使用 docker 的一些功能
参考:https://docs.gitlab.com/runner/executors/docker.html#use-docker-in-docker-with-privileged-mode
其它命令:https://docs.gitlab.com/runner/commands/README.html

[[runners]]
  executor = "docker"
  [runners.docker]
    privileged = true 

完整的例子

[[runners]]
  name = "docker-runner"
  url = "http://kvmdocker/"
  token = "Qzwktt9LewFSxPDNQ2Ch"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.docker]
    tls_verify = false
    image = "alpine:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0

高级配置:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runners-section

环境变量

通过设置环境变量,让 runner 执行的时候读取,避免在源码中记录敏感信息

在群组或者项目的设置中依次点击设置>CI/CD>变量

.gitlab-ci.yml文件中使用方式

ShellUsage
bash/sh$variable
windows batch%variable%
PowerShell$env:variable

参考https://docs.gitlab.com/ce/ci/variables/README.html#via-the-ui

Dockerfile 中使用变量参考:https://docs.docker.com/engine/reference/builder/#arg

添加 .gitlab-ci.yml

项目中添加 CI/CD 配置,在 gitlab 中新建可选择模板,选择 docker 模板
文档参考:https://docs.gitlab.com/ee/ci/yaml/README.html#validate-the-gitlab-ciyml

# This file is a template, and might need editing before it works on your project.
build-master:
  # Official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    # - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker --version
    - echo $NugetAppKey
    - echo $NugetSource
  script:
    - docker build --build-arg AppKey=$NugetAppKey --build-arg Source=$NugetSource -t A1:latest .
  only:
    - master 

问题

1.执行 docker build 的时候报错:time="2019-07-19T01:20:27Z" level=error msg="failed to dial gRPC: cannot connect to the Docker daemon. Is 'docker daemon' running on this host?: dial tcp: lookup docker on 100.100.2.136:53: no such host",这个需要配置 runner,让容器跑在privileged模式,参考 https://docs.gitlab.com/runner/executors/docker.html#use-docker-in-docker-with-privileged-mode
2.runner 执行的时候需要拉取代码,但是拉取的链接由于种种原因在容器内很难访问,需要另外的设置或者代理才行。但是高级设置有个参数可以设置拉取代码的链接:clone_url。文档:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runners-section 示例:

[[runners]]
  name = "ruby-2.1-docker"
  url = "https://CI/"
  token = "TOKEN"
  limit = 0
  executor = "docker"
  builds_dir = ""
  shell = ""
  environment = ["ENV=value", "LC_ALL=en_US.UTF-8"]
  clone_url = "http://gitlab.example.local" 

总结

安装过程也没什么难点,有什么不对的多试几次即可
gitlab 的文档还是挺齐全的,如果能够把相关的文档先大概浏览一遍,将会有事半功倍的效果,省去不少麻烦

宝剑锋从磨砺出,梅花香自苦寒来.