DevOps GitLab CICD 实践2——Runner 部署

释放双眼,带上耳机,听听看~!

前置步骤:DevOps GitLab CICD 实践1——GitLab 部署

官方文档

文档地址

虽然有官方指引,但个人感觉指引的不够清晰,导致初次配置可能频繁失败

Run GitLab Runner in a container

This is how you can run GitLab Runner inside a Docker container.

General GitLab Runner Docker image usage

GitLab Runner Docker images (based on Ubuntu or Alpine Linux) are designed as wrappers around the standard gitlab-runner command, like if GitLab Runner was installed directly on the host.

The general rule is that every GitLab Runner command that normally would be executed as:


1
2
3
1gitlab-runner [Runner command and options...]
2复制代码
3

can be executed with:


1
2
3
1docker run [chosen docker options...] gitlab/gitlab-runner [Runner command and options...]
2复制代码
3

For example, getting the top-level help information for GitLab Runner command could be executed as:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1docker run --rm -t -i gitlab/gitlab-runner --help
2
3NAME:
4   gitlab-runner - a GitLab Runner
5
6USAGE:
7   gitlab-runner [global options] command [command options] [arguments...]
8
9VERSION:
10   10.7.0 (7c273476)
11
12(...)
13复制代码
14

In short, the gitlab-runner part of the command is replaced with docker run [docker options] gitlab/gitlab-runner, while the rest of Runner’s command stays as it is described in the register documentation. The only difference is that the gitlab-runner command is executed inside of a Docker container.

Docker image installation and configuration

Install Docker first:


1
2
3
1 curl -sSL https://get.docker.com/ | sh
2复制代码
3

You need to mount a config volume into the gitlab-runner container to be used for configs and other resources:


1
2
3
4
5
6
1 docker run -d --name gitlab-runner --restart always \
2   -v /srv/gitlab-runner/config:/etc/gitlab-runner \
3   -v /var/run/docker.sock:/var/run/docker.sock \
4   gitlab/gitlab-runner:latest
5复制代码
6

Tip: On macOS, use /Users/Shared instead of /srv.

Or, you can use a config container to mount your custom data volume:


1
2
3
4
5
6
1 docker run -d --name gitlab-runner-config \
2     -v /etc/gitlab-runner \
3     busybox:latest \
4     /bin/true
5复制代码
6

And then, run the Runner:


1
2
3
4
5
6
1 docker run -d --name gitlab-runner --restart always \
2     -v /var/run/docker.sock:/var/run/docker.sock \
3     --volumes-from gitlab-runner-config \
4     gitlab/gitlab-runner:latest
5复制代码
6

Register the runner you just launched by following the instructions in the Docker section of Registering Runners. The runner won’t pick up any jobs until it’s registered.

安装步骤

获取Gitlab Runner秘钥

可以通过Gitlab管理员账号获取,也可以让每一个用户自行配置

  • 普通用户查看秘钥

进入任意一个仓库的设置中,查看CICD配置

准备注册专用Runner令牌

  • 管理员查看令牌

进入总设置页面,配置全局Runner令牌

Runner注册

由于Runner一般运行复杂构建、打包任务,推荐配置在性能、带宽更大的机房

准备Docker环境


1
2
3
4
1$ curl -sSL https://get.docker.com/ | sh
2$ systemctl start docker
3复制代码
4

注册

可以根据需要选择注册Runner类型

同时,为了方便配置,使用单行注册并且关闭交互

单行注册官方文档

命令说明:

  • -v挂载的目的是为了将注册后的配置文件持久化,用于运行容器

  • –rm指定容器运行结束后自动删除停止的容器

  • -it指定使用命令行交互方式运行,便于查看注册结果


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1$ docker run --rm -it \
2  -v /www/wwwroot/gitlab/srv/gitlab-runner/config:/etc/gitlab-runner \
3  gitlab/gitlab-runner:alpine-v11.8.0 register \
4  --non-interactive \
5  --executor "docker" \
6  --docker-image docker:stable \
7  --url "Gitlab URL" \
8  --registration-token "令牌" \
9  --description "描述" \
10  --tag-list "标签1,标签2" \
11  --run-untagged \
12  --docker-privileged \
13  --locked="false"
14复制代码
15

注册成功提示

此时管理面板显示新的Runner已经注册

Runner 启动

挂载本地配置信息并启动Runner


1
2
3
4
5
6
1$ docker run -d --name gitlab-runner --restart always \
2   -v /www/wwwroot/gitlab/srv/gitlab-runner/config:/etc/gitlab-runner \
3    -v /var/run/docker.sock:/var/run/docker.sock \
4    gitlab/gitlab-runner:alpine-v11.8.0
5复制代码
6

此时可见Runner已经保持和Gitlab的联系

给TA打赏
共{{data.count}}人
人已打赏
安全运维

故障复盘的简洁框架-黄金三问

2021-9-30 19:18:23

安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索