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 | # For Debian/Ubuntu |
Install gitlab-ci-multi-runner
:
1 | # For Debian/Ubuntu |
Register a Specific Runner
1 | sudo gitlab-ci-multi-runner register |
Answer the following questions after this command:
运行上面这段脚本将需要你回答几个问题:
1 | Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci ): |
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 -> Runners)Specific runners
区域。方便验证起见,我们先选择 shell
executor。刷新页面就能看的刚刚注册的 my-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 | # install |
1 | # start the Docker daemon on Mac |
Now we can register a specific docker runner:
现在就能注册一个 docker runner:
1 | sudo gitlab-ci-multi-runner register |
1 | ... |
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 | FROM centos:7 |
Build it on your machine:
在你的机器上构建这个镜像:
1 | docker build -t <docker-user-name>/<docker-image-name> . |
Run it on your machine:
在你的机器上运行这个镜像:
1 | # login bash |
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 |