跳到主要内容

1、GitLabRunner安装部署

实战:GitLabRunner安装部署-2023.4.26(安装成功)

image-20230426063250435

目录

[toc]

实验环境

gitlab/gitlab-ce:15.0.3-ce.0
gitlab-runner-15.0.1-1.x86_64.rpm

实验软件

链接:https://pan.baidu.com/s/10aL_kWzDKnZXBXG3-hS2tA 提取码:0820 2023.4.26-实战:GitLabRunner安装部署-(安装成功)

image-20230426072018932

什么是Gitlab runner

image-20230426063416914

  • GitLab Runner是一个开源项目,用于运行作业并将结果发送回GitLab。
  • 与GitLabCI结合使用,GitLabCI是GitLab随附的用于协调作业的开源持续集成服务。
  • GitLab Runner是用Go编写的,可以在Linux,macOS和Windows操作系统上运行。
  • 容器部署需使用最新Docker版本。GitLab Runner需要最少的Docker v1.13.0
  • GitLab Runner版本应与GitLab版本同步。(避免版本不一致导致差异化)
  • 可以根据需要配置任意数量的Runner。

image-20220505104412269

注意:建议将runner和gitlab server放在不同机器上运行,否则可能不能正常运行,并且也会给servre带来性能上的影响。

本次是实验环境,是可以使用同的一台机器!(gitlab是容器方式,而gitlab Runner是直接跑在宿主机上的,类似于jenkins master是以容器方式运行,而agent是直接跑在宿主机上的)!

类型

  • shared 共享类型,运行整个平台项目的作业**(gitlab)**
  • group 项目组类型,运行特定group下的所有项目的作业**(group)**
  • specific项目类型,运行指定的项目作业**(project)**

后面我们都是在平台类型上注册runner的:(注册时需要带上token)

image-20220505110229006

平台级别的runner:

image-20230425214907901

组级别的runner:

image-20230425215000146

项目,这里也是可以看到3种runner类型的。

image-20230425215117386

状态

  • locked: 锁定状态,无法运行项目作业
  • paused: 暂停状态,暂时不会接受新的作业

步骤

  • 安装gitlab-runner工具
  • 使用gitlab-runner工具向GitLab Server注册Runner节点。

1、Runner安装

在Linux系统安装GR

  • 基于Linux CentOS8操作系统, 部署GitLabRunner。

GitLab部署包下载:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el8/

Runner部署包下载:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el8-x86_64/

自己本次测试:

1、下载gitlab-runner安装包

查看自己gitlab版本,下载和当前gitlab差不多一致的gitlab-runner安装包

image-20230426064144269

image-20230426064107032

Runner部署包下载:

https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7-x86_64/

[root@Devops6 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7/gitlab-runner-15.0.1-1.x86_64.rpm --no-check-certificate
--2023-04-26 06:42:18-- https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7/gitlab-runner-15.0.1-1.x86_64.rpm
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.15.130, 2402:f000:1:400::2
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.15.130|:443... connected.
WARNING: cannot verify mirrors.tuna.tsinghua.edu.cn's certificate, issued by ‘/C=US/O=Let's Encrypt/CN=R3’:
Issued certificate has expired.
HTTP request sent, awaiting response... 200 OK
Length: 434734559 (415M) [application/x-redhat-package-manager]
Saving to: ‘gitlab-runner-15.0.1-1.x86_64.rpm’

100%[================================================================================================================================================================>] 434,734,559 14.3MB/s in 33s

2023-04-26 06:42:52 (12.4 MB/s) - ‘gitlab-runner-15.0.1-1.x86_64.rpm’ saved [434734559/434734559]
[root@Devops6 ~]#ll -h gitlab-runner-15.0.1-1.x86_64.rpm
-rw-r--r-- 1 root root 415M Jul 21 2022 gitlab-runner-15.0.1-1.x86_64.rpm

2、安装gitlab-runner rpm包

## 安装runner
[root@Devops6 ~]#rpm -ivh gitlab-runner-15.0.1-1.x86_64.rpm
warning: gitlab-runner-15.0.1-1.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 35dfa027: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:gitlab-runner-15.0.1-1 ################################# [100%]
GitLab Runner: creating gitlab-runner...
Home directory skeleton not used
Runtime platform arch=amd64 os=linux pid=51841 revision=7674edc7 version=15.0.1
gitlab-runner: the service is not installed
Runtime platform arch=amd64 os=linux pid=51848 revision=7674edc7 version=15.0.1
gitlab-ci-multi-runner: the service is not installed
Runtime platform arch=amd64 os=linux pid=51872 revision=7674edc7 version=15.0.1
Runtime platform arch=amd64 os=linux pid=51908 revision=7674edc7 version=15.0.1

Check and remove all unused containers (both dangling and unreferenced) including volumes.
------------------------------------------------------------------------------------------
Total reclaimed space: 0B

3、验证

## 查看进程
[root@Devops6 ~]#ps aux|grep gitlab-runner
root 51916 1.1 0.1 748072 18532 ? Ssl 06:44 0:00 /usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --user gitlab-runner
root 51995 0.0 0.0 112708 980 pts/0 S+ 06:44 0:00 grep --color=auto gitlab-runner


#查看版本
[root@Devops6 ~]#gitlab-runner -v
Version: 15.0.1
Git revision: 7674edc7
Git branch: 15-0-stable
GO version: go1.17.7
Built: 2022-07-20T15:13:22+0000
OS/Arch: linux/amd64

2、注册Runner

1、获取GitLab地址和Runner 注册token

  • 本次注册平台级别runner:

点击Menu/Admin/Runners

image-20220505105632269

image-20220505105641693

保存下上面这个token:

osNyPCDx47f52iD4Hh5F

2、运行注册命令

  • 开始交互式注册
[root@Devops6 ~]#gitlab-runner register
Runtime platform arch=amd64 os=linux pid=53427 revision=7674edc7 version=15.0.1
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
http://172.29.9.101:8076/
Enter the registration token:
osNyPCDx47f52iD4Hh5F
Enter a description for the runner:
[Devops6]: build01
Enter tags for the runner (comma-separated):
build,go,maven
Enter optional maintenance note for the runner:

Registering runner... succeeded runner=osNyPCDx
Enter an executor: parallels, shell, docker+machine, docker-ssh+machine, kubernetes, custom, docker, docker-ssh, ssh, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

image-20230426065725839

说明:默认注册采用的交互式,需要交互填写信息。

[root@zeyang-nuc-service gitlab-runner]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=40969 revision=f188edd7 version=14.9.1
Running in system-mode.
## 输入GitLab实例的URL
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.1.200

## 输入Runner注册Token
Enter the registration token:
m5ucdGk2_uPJ2K9BP8-d

## 填写该注册Runner的描述信息
Enter a description for the runner:
[zeyang-nuc-service]: build runner

## 为该Runner配置一个标签(后续作业可以通过tag指定在哪个runner上面运行构建)
Enter tags for the runner (comma-separated):
build,go,maven
Enter optional maintenance note for the runner:

Registering runner... succeeded runner=m5ucdGk2
## 为Runner选择一个执行器
Enter an executor: docker, parallels, ssh, docker+machine, docker-ssh+machine, custom, docker-ssh, shell, virtualbox, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
  • 本地查看runner信息
[root@Devops6 ~]#gitlab-runner list
Runtime platform arch=amd64 os=linux pid=53774 revision=7674edc7 version=15.0.1
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
build01 Executor=shell Token=PzMYek9JphRmM5Fo5qKo URL=http://172.29.9.101:8076/
[root@Devops6 ~]#gitlab-runner verify
Runtime platform arch=amd64 os=linux pid=53789 revision=7674edc7 version=15.0.1
Running in system-mode.

Verifying runner... is alive runner=PzMYek9J

image-20230426065933483

  • 当然也可以使用非交互模式来注册:
gitlab-runner register \
--non-interactive \
--url "http://192.168.1.200/" \
--registration-token "m5ucdGk2_uPJ2K9BP8-d" \
--executor "shell" \
--description "buildrunner" \
--tag-list "build,k8s,go" \
--run-untagged="true" \
--locked="false" \
--access-level="not_protected"


#本次测试虚机代码
gitlab-runner register \
--non-interactive \
--url "http://172.29.9.101:8076/" \
--registration-token "osNyPCDx47f52iD4Hh5F" \
--executor "shell" \
--description "buildrunner" \
--tag-list "build,mvn,go" \
--run-untagged="true" \
--locked="false" \
--access-level="not_protected"

3、刷新GitLab管理页面

image-20230426065933483

3、配置Runner

  • 通过Gitlab后台可以看下runner的配置:

image-20230426070209955

image-20230426070221000

  • gitlab-runner配置文件 (改完配置文件,它会自动生效的。)
[root@Devops6 ~]#cat /etc/gitlab-runner/config.toml 
concurrent = 1
check_interval = 0

[session_server]
session_timeout = 1800

[[runners]]
name = "build01"
url = "http://172.29.9.101:8076/"
token = "PzMYek9JphRmM5Fo5qKo"
executor = "shell"
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]

说明:

concurrent = 1    						## 可并行运行作业的数量, 0表示不限制;
check_interval = 0 ## 检查新作业的时间间隔, 0表示默认 3秒;

[session_server] ## 允许用户与作业进行交互,例如web终端;
session_timeout = 1800

[[runners]]
name = "my first runner" ## Runner名称;
url = "http://192.168.1.200/" ## GitLab Server地址;
token = "z6QEqyGpDrvzNgfxLiVh" ## Runner token;
executor = "shell" ## Runner的执行器;
[runners.custom_build_dir] ## 允许用户为作业定义自定义构建目录;
[runners.cache] ## 分布式缓存目录;
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]

高级配置参考: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnerscustom-section

4、Runner常用命令

  • gitlab Runner常用命令
gitlab-runner register  #默认交互模式下使用,非交互模式添加 --non-interactive
gitlab-runner list #此命令列出了保存在配置文件中的所有运行程序
gitlab-runner verify #此命令检查注册的runner是否可以连接,但不验证GitLab服务是否正在使用runner。 --delete 删除
gitlab-runner unregister #该命令使用GitLab取消已注册的runner。

#使用令牌注销
gitlab-runner unregister --url http://gitlab.example.com/ --token t0k3n

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

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

FAQ

升级完git版本后,gitlabrunner会被删除的

  • 升级完git后,gitlab-runner没掉了,再次安装gitlab-runner时报错

image-20230510075035521

  • 那就使用yum来安装依赖包,就可以解决问题了
[root@Devops6 ~]#yum install -y gitlab-runner-15.0.1-1.x86_64.rpm

image-20230510075300644

image-20230510075319476

centos8安装时报错

自己是在centos7上测试的,这里先做记录。

  • 报错现象

image-20230426071902663

  • 解决办法

image-20230426071917534

注意:runner的Run untagged jobs配置

默认是勾选了的。

image-20230516072615230

image-20230516072626943

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 微信二维码 x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号 《云原生架构师实战》

image-20230107215126971

🍀 语雀

https://www.yuque.com/xyy-onlyone

image-20230306221144511

🍀 csdn https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

image-20230107215149885

🍀 知乎 https://www.zhihu.com/people/foryouone

image-20230107215203185

最后

好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!