(共556篇)
全部分类

CI/CD中的runner及类型
[ CICD ] 

使用 CI/CD 必须提前注册至少一个 runner, runner 是用来具体执行任务的工具

runner 通常被安装在某个主机上, 并通过"注册"的方式, 注册到 gitlab 所在的服务器, 通常可以在gitlab设置-overview-runner中看到当前环境中所有的 runner 信息, 页面中也提供了安装/注册 runner 的教程, 根据提示注册至少一个 runner 即可

也可以指定一个 docker 容器, 让 runner 在容器中运行, (注意: “在 docker 容器中执行 runner” 和"把应用程序打包为一个镜像"是两个概念)这个稍后再说

runner 根据执行权限的不同分为共享 runner 和专属 runner, 专属 runner 只能用于指定的项目, 共享 runner 可以同时多个项目

runner 在注册的时候, 会被要求提供一个标签名, 这个标签名在创建 job 时会用到

同一个主机上可以注册多个 runner, 多个 runner 的配置信息默认都保存在"/etc/gitlab-runner/config.toml"文件中

runner 在注册的时候支持多种类型, 这里只说两种

shell 类型的 runner

shell 类型的 runner 会在 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
28
29
30
31
32
[root@node12 ~]# gitlab-runner register --url http://192.168.20.12:8101/ --registration-token BAAdKfSbs-vAeACtvGGw
Runtime platform                                    arch=amd64 os=linux pid=88022 revision=7f093137 version=15.2.0
Running in system-mode.

# 输入gitlab所在的主机地址
Enter the GitLab instance URL (for example, https://gitlab.com/):
[http://192.168.20.12:8101/]:

# 输入gitrunner的token, token可以在"Admin-Overview-Runners(创建的是共享runner)"或者"Project-Setting-Runners(创建的是私有runner)"中查看
Enter the registration token:
[BAAdKfSbs-vAeACtvGGw]:

# 输入runner的描述信息
Enter a description for the runner:
[node12]:

# 输入runner的标签名, 创建job时要指定这个名称
Enter tags for the runner (comma-separated):
nuxt3

# 输入runner的维护信息, 空白即可
Enter optional maintenance note for the runner:

Registering runner... succeeded                     runner=BAAdKfSb

# 输入runner的类型
Enter an executor: custom, parallels, virtualbox, docker+machine, kubernetes, docker, docker-ssh, shell, ssh, docker-ssh+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

# 这是runner的配置文件所在位置, 如果有其他的配置,可以直接在配置文件中修改, 然后重启runner服务
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

ssh 类型的 runner

有一些任务是需要在远程主机上执行的, 比如一些项目在 runner 所在的主机上测试/打包/发布到生产服务器上之后, 需要在生产服务器上执行某些类似于重启服务的脚本, 此时就需要 ssh 类型的 runner 了

ssh 类型的 runner 会把项目拉取到远程主机上, 并保存在"~/builds////“位置

 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
[root@node12 ~]# gitlab-runner register --url http://192.168.20.12:8101/ --registration-token BAAdKfSbs-vAeACtvGGw
Runtime platform                                    arch=amd64 os=linux pid=78919 revision=7f093137 version=15.2.0
Running in system-mode.

# 输入gitlab所在的主机地址
Enter the GitLab instance URL (for example, https://gitlab.com/):
[http://192.168.20.12:8101/]:

# 输入gitrunner的token, token可以在"Admin-Overview-Runners(创建的是共享runner)"或者"Project-Setting-Runners(创建的是私有runner)"中查看
Enter the registration token:
[BAAdKfSbs-vAeACtvGGw]:

# 输入runner的描述信息
Enter a description for the runner:
[node12]:

# 输入runner的标签名, 创建job时要指定这个名称
Enter tags for the runner (comma-separated):
ssh

# 输入runner的维护信息, 空白即可
Enter optional maintenance note for the runner:

Registering runner... succeeded                     runner=BAAdKfSb

# 输入runner的类型
Enter an executor: custom, docker, parallels, virtualbox, docker+machine, docker-ssh+machine, docker-ssh, shell, ssh, kubernetes:
ssh

# 输入远程主机的地址
Enter the SSH server address (for example, my.server.com):
192.168.20.13

# 输入远程主机的端口
Enter the SSH server port (for example, 22):

# 输入要在远程主机上执行任务的用户
Enter the SSH user (for example, root):
root

# 输入远程主机用户的密码
Enter the SSH password (for example, docker.io):
admk8s

# 输入ssh连接远程主机时要用的秘钥
Enter the path to the SSH identity file (for example, /home/user/.ssh/id_rsa):
/root/.ssh/id_rsa

# 注册成功
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

# 这是runner的配置文件所在位置, 如果有其他的配置,可以直接在配置文件中修改, 然后重启runner服务
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

生成的配置文件是这样的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@node12 ~]# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "node12"
  url = "http://192.168.20.12:8101/"
  token = "8xWdhLe8JNYGgo8uH5Rg"
  executor = "ssh"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.ssh]
    user = "root"
    password = "admk8s"
    host = "192.168.20.13"
    identity_file = "/root/.ssh/id_rsa"

项目在远程主机上的位置是可以修改的,通过 build_dir 属性来重新定义:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "node12"
  url = "http://192.168.20.12:8101/"
  token = "8xWdhLe8JNYGgo8uH5Rg"
  executor = "ssh"
  [runners.custom_build_dir]
    build_dir = '/save-path-directory'
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.ssh]
    user = "root"
    password = "admk8s"
    host = "192.168.20.13"
    identity_file = "/root/.ssh/id_rsa"

如果想把 artifacts 文件发送到远程主机上, 可以在 shell 类型的 job 中, 通过 scp 的方式把文件发送过去, 再通过 ssh 类型的 job 对 artifacts 文件执行其他操作