作者:mobiledu2502880317 | 来源:互联网 | 2023-08-29 10:54
特点
- 开源: CI/CD是开源GitLab社区版和专有GitLab企业版的一部分。(极狐)
- 易于学习: 官方具有详细的学习操作文档。
- 无缝集成: CI/CD是GitLab的一部分,支持从计划到部署,具有出色的用户体验。
- 可扩展: 可以根据需要添加任意数量的构建节点。*(从架构上有gitlab server和gitlab runner,类似于slave构建节点)
- 更快的结果: 每个构建可以拆分为多个作业,这些作业可以在多台计算机上并行运行。
- 针对交付进行了优化: 多个阶段,手动部署, 环境 和 变量。
点开一个项目就可以看到流水线的CI/CD管道信息,包含了流水线的状态,流水线的ID,后面就是分支,谁触发的,流水线每个步骤的状态。
可以看到gitlab自带了CI/CD的功能,不需要额外的安装。gitlab提供了完整的CI/CD功能,不要额外的安装工具链来实现,如果公司的项目不是特别多,可以使用gitlab,就不需要安装太多的工具链,减少运维成本。![](https://img6.php1.cn/3cdc5/c555/243/cb927ad5119206de.png)
GitLab部署
![image.png](https://img6.php1.cn/3cdc5/c555/243/0ff66bf91c445275.png)
版本: 14.0.0
RPM参考文档:https://www.yuque.com/zeyangli/bqsigl/rgdrnw
## 下载镜像
docker pull gitlab/gitlab-ce:14.0.0-ce.0
## 创建数据目录
mkdir -p /data/cicd/gitlab/{config,logs,data}
chmod 777 -R /data/cicd/gitlab/
## 运行
docker run -d -p 443:443 -p 80:80 -p 222:22 --name gitlab \
--restart always \
-v /data/cicd/gitlab/config:/etc/gitlab \
-v /data/cicd/gitlab/logs:/var/log/gitlab \
-v /data/cicd/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:14.0.0-ce.0
- 222端口 ssh协议
- 80端口 http端口
- 443端口 https端口
FAQ
[root@zeyang-nuc-service ~]# git clone http://927c154cfffa/devops/demo-hello-service.git
Cloning into 'demo-hello-service'...
fatal: unable to access 'http://927c154cfffa/devops/demo-hello-service.git/': Could not resolve host: 927c154cfffa
[root@zeyang-nuc-service ~]# docker ps | grep gitlab
927c154cfffa gitlab/gitlab-ce:13.10.0-ce.0 "/assets/wrapper" 21 minutes ago Up 21 minutes (healthy) 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:222->22/tcp gitlab
[root@zeyang-nuc-service ~]#
解决方法
docker exec -it gitlab bash
root@927c154cfffa:/# vi /etc/gitlab/gitlab.rb
![](https://img6.php1.cn/3cdc5/c555/243/31cc483ba76d9da9.png)
root@927c154cfffa:/# gitlab-ctl reconfigure
![image.png](https://img6.php1.cn/3cdc5/c555/243/e2419fe52d350c43.png)
GitLab Runner配置管理
gitlab是一个项目,gitlab runner也是一个单独的项目,runner类似于Jenkins的slave节点,也是server去调度,然后分配作业。
- GitLab Runner是一个开源项目,用于运行作业并将结果发送回GitLab。
- 与GitLabCI结合使用,GitLabCI是GitLab随附的用于协调作业的开源持续集成服务。
- GitLab Runner是用Go编写的,可以在Linux,macOS和Windows操作系统上运行。
- 容器部署需使用最新Docker版本。GitLab Runner需要最少的Docker v1.13.0。
- GitLab Runner版本应与GitLab版本同步。(避免版本不一致导致差异化)
- 可以根据需要配置任意数量的Runner。
![image.png](https://img6.php1.cn/3cdc5/c555/243/ac9cdced4d6254f4.png)
类型
- shared 共享类型,运行整个平台项目的所有作业(gitlab 点开Menu----》admin)下面可以看到在后台拿到的token基本上都是shared类型的token
![](https://img6.php1.cn/3cdc5/c555/243/be1ca4ceecdfbd66.png)
- group 项目组类型,运行特定group下的所有项目的作业(group)这是注册一个group级别的token,那么就是这个组的runner,不能让其他组去使用
![](https://img6.php1.cn/3cdc5/c555/243/723ebb9242419844.png)
- specific项目类型,运行指定的项目作业(project)
状态
- locked: 锁定状态,无法运行项目作业
- paused: 暂停状态,暂时不会接受新的作业
步骤:
- 安装
gitlab-runner
工具 - 使用
gitlab-runner
工具向GitLab Server
注册Runner节点。
Runner部署包下载:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7-x86_64/
[root@gitlab-dockerhub ~]# ls
gitlab-runner-14.0.0-1.x86_64.rpm
[root@gitlab-dockerhub ~]# rpm -ivh gitlab-runner-14.0.0-1.x86_64.rpm
warning: gitlab-runner-14.0.0-1.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 35dfa027: NOKEY
error: Failed dependencies:
git is needed by gitlab-runner-14.0.0-1.x86_64
[root@gitlab-dockerhub ~]# yum install git -y
[root@gitlab-dockerhub ~]# rpm -ivh gitlab-runner-14.0.0-1.x86_64.rpm
warning: gitlab-runner-14.0.0-1.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 35dfa027: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:gitlab-runner-14.0.0-1 ################################# [100%]
GitLab Runner: creating gitlab-runner...
Home directory skeleton not used
Runtime platform arch=amd64 os=linux pid=7771 revision=3b6f852e version=14.0.0
gitlab-runner: the service is not installed
Runtime platform arch=amd64 os=linux pid=7781 revision=3b6f852e version=14.0.0
gitlab-ci-multi-runner: the service is not installed
Runtime platform arch=amd64 os=linux pid=7811 revision=3b6f852e version=14.0.0
Runtime platform arch=amd64 os=linux pid=7847 revision=3b6f852e version=14.0.0
Check and remove all unused containers (both dangling and unreferenced) including volumes.
------------------------------------------------------------------------------------------
Total reclaimed space: 0B
#/etc/gitlab-runner/config.toml这个为其配置文件
[root@gitlab-dockerhub ~]# ps aux | grep -v grep | grep runner
root 7857 0.0 0.2 151960 17388 ? Ssl 10:27 0:00 /usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --user gitlab-runner
Runner注册配置使用
- 获取GitLab地址和Runner 注册token(和Jenkins类似,下载一个通用的jar包,然后传不同都没密钥信息,就可以启动不同的slave)现在要去找gitlab的token
![](https://img6.php1.cn/3cdc5/c555/243/b3f9bfcbfe807790.png)
1ssCE2ri76RiinkMGKgj
注册的时候有一系列的命令,注册的过程是一个交互的过程,需要填一些信息
gitlab url地址-----》token-----》备注,也就是描述信息
标签信息就像Jenkins一样
[root@gitlab-dockerhub ~]# cat /etc/gitlab-runner/config.toml
cOncurrent= 1
check_interval = 0
[session_server]
session_timeout = 1800
[root@gitlab-dockerhub ~]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=22183 revision=3b6f852e version=14.0.0
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://139.198.166.235:81
Enter the registration token:
1ssCE2ri76RiinkMGKgj
Enter a description for the runner:
[gitlab-dockerhub]: devops01
Enter tags for the runner (comma-separated):
build,k8sbuild,go,java
Registering runner... succeeded runner=1ssCE2ri
Enter an executor: docker-ssh, virtualbox, docker-ssh+machine, shell, ssh, docker+machine, kubernetes, custom, docker, parallels:
![](https://img6.php1.cn/3cdc5/c555/243/c10bb4ab3a671cb8.png)
现在还是锁定状态,这样runner就注册好了。后面就要写执行器了,执行器概念和Jenkins类似,运行的作业,执行器就是帮我们去运行作业的,gitlab执行器有很多种,有docker类型如下:
Enter an executor: docker-ssh, virtualbox, docker-ssh+machine, shell, ssh, docker+machine, kubernetes, custom, docker, parallels:
- docker 类型:这时候来了一个作业,运行在docker类型的runner上面,那么会要你输入就像的ID,比如maven的镜像,这个时候在运行作业的时候,就会以设置的这个镜像起来一个容器,然后在容器里面进行构建
- shell 类型:执行shell命令
- kubernetes类型:启动一个pod
这里选择shell类型执行器
Enter tags for the runner (comma-separated):
build,k8sbuild,go,java
Registering runner... succeeded runner=1ssCE2ri
Enter an executor: docker-ssh, virtualbox, docker-ssh+machine, shell, ssh, docker+machine, kubernetes, custom, docker, parallels:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
可以看到执行器注册好了
[root@gitlab-dockerhub ~]# gitlab-runner list
Runtime platform arch=amd64 os=linux pid=26860 revision=3b6f852e version=14.0.0
Listing configured runners COnfigFile=/etc/gitlab-runner/config.toml
devops01 Executor=shell Token=Tt2yusRjFFFDPQ87Rvsd URL=http://139.198.166.235:81
![](https://img6.php1.cn/3cdc5/c555/243/a0a2f2bce5e34f51.png)
查看一下现在的状态
Indicates whether this runner can pick jobs without tags:在运行作业的时候会分配一个标签,这个标签会指定在哪个runner上面执行,但作业没有指定这个标签,如果想让其执行勾选就行了。
![](https://img6.php1.cn/3cdc5/c555/243/3d64d56c5e48f79b.png)
因为是共享类型的runner,那些作业可以使用这个runner可以在这里启用
![](https://img6.php1.cn/3cdc5/c555/243/2238993f29396ac6.png)
上面就一个一个runner的注册过程就完成了。
上面是使用交互模式进行配置,使用非交互模式注册
gitlab-runner register \
--non-interactive \
--url "http://192.168.1.200/" \
--registration-token "Bd6gzBK5evJqa9wN99Nf" \
--executor "shell" \
--description "buildrunner" \
--tag-list "build,k8s,go" \
--run-untagged="true" \
--locked="false" \
--access-level="not_protected"