GitLab CI Runner with Docker

Install CI Runner:

Install a CI Runner on a machine is quite easy, here is the linux-repository:
在一台普通机器上安装 CI Runner 应该是一件很简单的事情,下面是 Linux 的安装方法

Add GitLab’s official repository via apt-get or yum:

1
2
3
4
5
# For Debian/Ubuntu
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash

# For CentOS
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash

Install gitlab-ci-multi-runner:

1
2
3
4
5
# For Debian/Ubuntu
sudo apt-get install gitlab-ci-multi-runner

# For CentOS
sudo yum install gitlab-ci-multi-runner

Register a Specific Runner

1
sudo gitlab-ci-multi-runner register

Answer the following questions after this command:
运行上面这段脚本将需要你回答几个问题:

1
2
3
4
5
6
7
8
9
10
11
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci ):
https://gitlab.com/ci
Please enter the gitlab-ci token for this runner:
xxx
Please enter the gitlab-ci description for this runner:
my-runner
Please enter the gitlab-ci tags for this runner (comma separated):
ci,runner
INFO[0034] fcf5c619 Registering runner... succeeded
Please enter the executor: docker-ssh+machine, docker, docker-ssh, parallels, shell, ssh, virtualbox, docker+machine:
shell

You can find the answer of the first 2 questions in the Specific runners section, Runners Page of your gitlab repo(Settings -> Runners). We choose shell executor for the first time. Refresh the Runners Page you will see my-runner in the Specific runners list.
头两个问题的答案就在项目的 Runners 页面Settings -> RunnersSpecific runners 区域。方便验证起见,我们先选择 shell executor。刷新页面就能看的刚刚注册的 my-runner 出现在列表中。

image from https://gitlab.com/help/ci/quick_start/README.md#configuring-a-runner

Running the Specific Runner

Click the Disable Shared runners button on the Runners Page so we can just use our runner register just now on each build. After commiting .gitlab-ci.yml (the file that is used by GitLab Runner to manage your project’s build), we will see jobs running in the Builds Page (or Pipelines Page) on each push.
为了每次都运行我们刚刚注册的 runner,需要点击 Runners 页Disable Shared runners 按钮,放弃 Gitlab 提供的共享 runner。提交了 .gitlab-ci.yml 构建文件 文件的项目,每次 push 都可以在 Builds 页面 (or Pipelines 页面) 看到进度。

Docker Executor

If you want to use Docker runner, install and start it before using the multi runner:
安装 Docker,用作 runner。

1
2
3
4
5
6
7
8
9
10
# install
curl -sSL https://get.docker.com/ | sh
# UPDATE: 2017-07-25: install docker for CentOS6:
# rpm -iUvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# yum update -y
# yum -y install docker-io
# UPDATE: END

# start the Docker daemon on CentOS
sudo service docker start
1
2
# start the Docker daemon on Mac
boot2docker start

Now we can register a specific docker runner:
现在就能注册一个 docker runner:

1
sudo gitlab-ci-multi-runner register
1
2
3
4
5
...
Please enter the executor: docker-ssh+machine, docker, docker-ssh, parallels, shell, ssh, virtualbox, docker+machine:
docker
Please enter the default Docker image (eg. ruby:2.1):
centos:7

Here we use a simple centos7 docker image. Now we have a pure environment at each build.
这里我们选了一个 centos7 的 Docker 镜像,这样我们每次构建都有一个“纯净”的环境。

Create Docker Image

Centos7 might too simple to do complex build with special requirements. We can build our own image by creating a Dockerfile, Base FROM centos and installed with java7 using RUN command.
当我们需要复杂的构建环境时,Centos7 明显是不够用的。所以我们可以自己做一个镜像,只需创建一个 Dockerfile 文件,声明基于(FROM)centos,且通过 RUN 命令安装 java7。

1
2
3
4
5
6
FROM centos:7
MAINTAINER Hans Chan <icsbun@gmail.com>

# yum install dependences
RUN yum install -y java-1.7.0-openjdk \
java-1.7.0-openjdk-devel

Build it on your machine:
在你的机器上构建这个镜像:

1
docker build -t <docker-user-name>/<docker-image-name> .

Run it on your machine:
在你的机器上运行这个镜像:

1
2
3
4
# login bash
docker run -i -t <docker-user-name>/<docker-image-name> /bin/bash
# check the java version
java -version

Push it to Docker Hub if you have an account:
如果你有一个 Docker Hub 账户,你就能把你的镜像推送到公网上了:

1
docker push <docker-user-name>/<docker-image-name>

Here is a full example base on Centos6.6, which contains java7 and git CLI.
这里我写了一个相对完整的示例,包含了 java7 和 git 命令行等。

Config Specific Runner

GitLab Runner configuration uses the TOML format.
The file to be edited can be found in: /etc/gitlab-runner/config.toml (on *nix systems as root). Change the image parameter in [runners.docker] section to <docker-user-name>/<docker-image-name> created by yourself, restart runner an have fun!
GitLab Runner 配置 使用 TOML 格式的文件,放在 /etc/gitlab-runner/config.toml (root 权限运行的 *nix 系统)。把 [runners.docker] 段落中的 image 参数改成刚刚创建的 <docker-user-name>/<docker-image-name> 镜像,重启 runner 即可:

1
sudo gitlab-ci-multi-runner restart