参考链接:https://help.aliyun.com/document_detail/106968.html#title-xfl-dp6-kta

大概过程: 获取runner token -> 进行注册

GitLabRunner 类型

  • shared : 运行整个平台项目的作业(gitlab)
  • group: 运行特定group下的所有项目的作业(group)
  • specific: 运行指定的项目作业(project)
  • locked: 无法运行项目作业
  • paused: 不会运行作业

GitLabRunner 注册流程

获取runner token

获取shared类型runnertoken

进入系统设置 -> Runners

获取group类型的runnertoken

进入group -> Settings -> CI/CD -> Runners -> Group Runners

获取specific类型的runnertoken

进入具体的项目 -> Settings -> CI/CD -> Runners -> Specific Runners

进行注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[root@k3s-node4 ~]# gitlab-runner register
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/)gitlab url:
http://192.168.56.104:30088/
Please enter the gitlab-ci token for this runner - gitlab token:
sm1Zsg-hnz5vyYwBczC9
Please enter the gitlab-ci description for this runner - 随意:
[k3s-node4]: build-runner
Please enter the gitlab-ci tags for this runner (comma separated)这里是需要注意的,需要和gitlab-ci.yaml配置文件一一对应才会执行:
build-runner
Whether to run untagged builds [true/false] - 如果没有对应到tag(也就是上面这个tag),会不会执行:
[false]: true
Whether to lock Runner to current project [true/false] - 是否会锁定z当前的runner的:
[false]: false
Registering runner... succeeded runner=sm1Zsg-h
Please enter the executor: docker+machine, docker-ssh+machine, docker-ssh, ssh, virtualbox, kubernetes, docker, parallels, shell:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@k3s-node4 ~]# gitlab-runner register
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.56.104:30088/
Please enter the gitlab-ci token for this runner:
sm1Zsg-hnz5vyYwBczC9
Please enter the gitlab-ci description for this runner:
[k3s-node4]: back-release
Please enter the gitlab-ci tags for this runner (comma separated):
back-release
Whether to run untagged builds [true/false]:
[false]: true
Whether to lock Runner to current project [true/false]:
[false]: false
Registering runner... succeeded runner=sm1Zsg-h
Please enter the executor: docker+machine, docker, parallels, shell, ssh, docker-ssh, virtualbox, docker-ssh+machine, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@k3s-node4 ~]# gitlab-runner register
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.56.104:30088/
Please enter the gitlab-ci token for this runner:
sm1Zsg-hnz5vyYwBczC9
Please enter the gitlab-ci description for this runner:
[k3s-node4]: back-master
Please enter the gitlab-ci tags for this runner (comma separated):
back-master
Whether to run untagged builds [true/false]:
[false]: true
Whether to lock Runner to current project [true/false]:
[false]: false
Registering runner... succeeded runner=sm1Zsg-h
Please enter the executor: docker, docker+machine, docker-ssh+machine, kubernetes, docker-ssh, parallels, shell, ssh, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

GitLab Runner命令

GitLab Runner包含一组命令,可用于注册,管理和运行构建。

注册命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 默认交互模式下使用
gitlab-runner register

# 非交互模式添加 --non-interactive
```
--non-interactive:以非交互模式运行,不会提示用户输入。
--executor "docker":指定使用 docker 执行器。
--docker-image alpine:latest:指定使用 alpine:latest 镜像,因为alpine:latest镜像十分的轻量。
--url "http://192.168.220.20:1180/":指定 GitLab 实例的 URL。
--registration-token "_HSstx8BzrWbLfo8n8XB":指定用于注册 runner 的注册令牌。
--description "test":为 runner 指定描述。
--tag-list "rouyi-runner,vue3":为 runner 指定标签,可以指定多个用‘,’隔开。
--run-untagged="true":允许 runner 运行未标记的作业。
--locked="false":指定 runner 不被锁定。
--access-level="not_protected":指定 runner 可以访问未受保护的作业。
```
gitlab-runner register \
--non-interactive \
--url https://gitlab.example.com \
--registration-token REGISTRATION_TOKEN \
--description "My Runner" \
--executor docker \
--docker-image ubuntu:latest \
--tag-list linux,x86_64

#此命令检查注册的runner是否可以连接,但不验证GitLab服务是否正在使用runner
gitlab-runner verify

启动命令

1
2
3
4
gitlab-runner --debug <command>   #调试模式排查错误特别有用。
gitlab-runner <command> --help #获取帮助信息
gitlab-runner run #普通用户模式 配置文件位置 ~/.gitlab-runner/config.toml
sudo gitlab-runner run # 超级用户模式 配置文件位置/etc/gitlab-runner/config.toml

服务管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#该命令启动GitLab Runner服务。
gitlab-runner start

#该命令停止GitLab Runner服务。
gitlab-runner stop

#该命令将停止,然后启动GitLab Runner服务。
gitlab-runner restart

#此命令列出了保存在配置文件中的所有运行程序
gitlab-runner list

#此命令显示GitLab Runner服务的状态。当服务正在运行时,退出代码为零;而当服务未运行时,退出代码为非零。
gitlab-runner status

注销命令

1
2
3
4
5
6
7
8
#使用令牌注销
gitlab-runner unregister --url http://gitlab.example.com/ --token t0k3n

#使用名称注销(同名删除第一个)
gitlab-runner unregister --name test-runner

#注销所有
gitlab-runner unregister --all-runners

注销报错

ERROR: Unregistering runner from GitLab forbidden runner=xv2Ng6Tc
FATAL: Failed to unregister runner

1
2
3
4
5
6
7
如果已经在 gitlab runners 页面中删除了 runner,但它仍然会出现在 gitlab-runner 机器上(使用命令检查 `gitlab-runner list`)。
注销离线的Runner,可以执行以下命令来删除旧的、已从GitLab中移除的Runner:`gitlab-runner verify --delete`

或者手动运行:
gitlab-runner stop
vi /etc/gitlab-runner/config.toml # 更新runner部分
gitlab-runner start