在 Docker 容器中运行 Gitlab Runner

环境:CentOS 7
目的:在 Docker container 中运行 Gitlab Runner,搭建 Gitlab-CI 环境

搭建 docker 运行环境

$ sudo yum install yum-utils device-mapper-persistent-data lvm2
$
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install docker-ce
$
$ sudo systemctl enable docker
$ sudo systemctl start docker
复制代码

安装 Runner

安装步骤:

  1. 安装 Gitlab 官方库
# For RHEL/CentOS/Fedora
 $ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
复制代码
  1. 安装最新版本的 Gitlab Runner
# For RHEL/CentOS/Fedora
$ sudo yum install gitlab-runner
复制代码

注册 Runner

执行注册命令:

$ docker run --rm -t -i -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \
 --non-interactive \
 --executor "docker" \
 --docker-image alpine:3 \
 --url "https://gitlab.com/" \
 --registration-token "PROJECT_REGISTRATION_TOKEN" \
 --description "docker-runner" \
 --tag-list "docker,aws" \
 --run-untagged \
 --locked="false"
复制代码

注册完成后,如下配置文件会被 Runner 自动加载:

 ## 配置文件路径为: /srv/gitlab-runner/config
 [[runners]]
  name = "gitlab-runner"
  url = "https://gitlab.com/"
  token = "PROJECT_REGISTRATION_TOKEN"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "alpine:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
复制代码

启动 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
复制代码

参数说明:

  • -d: 设置容器后台运行
  • --name:容器名称
  • -restart always:每次启动容器就重启 gitlab-runner
  • -v: 共享目录挂载

创建 .gitlab-cli.yml 文件

创建文档示例请点击这里

附:

  • 启动完成后,执行 $ docker ps 查看容器运行情况
  • 查看运行日志
## gitlab-runner 是容器的名字,第一行命令 `--name gitlab-runner`
$ docker logs gitlab-runner
复制代码
  • 停止或者移除容器
$ docker stop gitlab-runner && docker rm gitlab-runner
复制代码

你可能感兴趣的:(在 Docker 容器中运行 Gitlab Runner)