共计 6353 个字符,预计需要花费 16 分钟才能阅读完成。
简介
GitLab-CI
GitLab-CI 就是一套配合 GitLab 应用的继续集成系统(当然,还有其它的继续集成系统,同样能够配合 GitLab 应用,比方 Jenkins)。而且 GitLab8.0 当前的版本是默认集成了 GitLab-CI 并且默认启用的。
GitLab-Runner
GitLab-Runner 是配合 GitLab-CI 进行应用的。个别地,GitLab 外面的每一个工程都会定义一个属于这个工程的软件集成脚本,用来自动化地实现一些软件集成工作。当这个工程的仓库代码产生变动时,比方有人 push 了代码,GitLab 就会将这个变动告诉 GitLab-CI。这时 GitLab-CI 会找出与这个工程相关联的 Runner,并告诉这些 Runner 把代码更新到本地并执行预约义好的执行脚本。
- 指标:代码提交到 GitLab 上,由 GitLab 的 CI 性能主动实现部署。
- 原理:GitLab 在接管到代码提交事件时,通过.gitlab-ci.yml 的配置信息与对应节点上的 runner 进行交互。
装置部署
planA(内网可用)
- 下载 && 装置
[root@gitlab ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm
--2021-08-16 10:11:02-- https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm
Resolving mirrors.xxxxe.com (mirrors.xxxxe.com)... 10.130.104.43
Connecting to mirrors.xxxxe.com (mirrors.xxxxe.com)|10.130.104.43|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 32189634 (31M) [application/x-redhat-package-manager]
Saving to:‘gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm’100%[=============================================================================================================================>] 32,189,634 2.23MB/s in 11s
2021-08-16 10:11:13 (2.80 MB/s) -‘gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm’saved [32189634/32189634]
[root@gitlab ~]# ls
anaconda-ks.cfg gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm
[root@gitlab ~]# rpm -ivh gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm
warning: gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 880721d4: NOKEY
error: Failed dependencies:
git is needed by gitlab-runner-12.0.1-1.x86_64
[root@gitlab ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@gitlab ~]# rpm -ivh gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm --nodeps --force
warning: gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 880721d4: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:gitlab-runner-12.0.1-1 ################################# [100%]
GitLab Runner: creating gitlab-runner...
- 批改 gitlab-runner 启动用户
[root@gitlab ~]# ps -ef | grep runner
root 3739 1 0 10:23 ? 00:00:00 /usr/lib/gitlab-runner/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user gitlab-runner
root 3801 3780 0 10:49 pts/0 00:00:00 grep --color=auto runner
[root@gitlab ~]# gitlab-runner uninstall
Runtime platform arch=amd64 os=linux pid=3802 revision=0e5417a3 version=12.0.1
[root@gitlab ~]# gitlab-runner install --working-directory /root --user root
Runtime platform arch=amd64 os=linux pid=3821 revision=0e5417a3 version=12.0.1
[root@gitlab ~]# gitlab-runner restart
Runtime platform arch=amd64 os=linux pid=3853 revision=0e5417a3 version=12.0.1
[root@gitlab ~]# gitlab-runner status
Runtime platform arch=amd64 os=linux pid=3868 revision=0e5417a3 version=12.0.1
gitlab-runner: Service is running!
[root@gitlab ~]# ps -ef | grep gitlab-runner
root 3861 1 0 10:51 ? 00:00:00 /usr/lib/gitlab-runner/gitlab-runner run --working-directory /root --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user root
- 注册
[root@gitlab ~]# gitlab-ci-multi-runner register
Runtime platform arch=amd64 os=linux pid=3878 revision=0e5417a3 version=12.0.1
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.xxxx.com #gitlab url
Please enter the gitlab-ci token for this runner:
Gxxxxxxxxxxxxxxxx7 #gitlab token
Please enter the gitlab-ci description for this runner:
[gitlab]: wuhan-tongj-10.82.16.44
Please enter the gitlab-ci tags for this runner (comma separated):
wuhan-tongj-10.82.16.44
Registering runner... succeeded runner=GLNByiz6
Please enter the executor: shell, virtualbox, docker+machine, docker-ssh+machine, docker, parallels, kubernetes, docker-ssh, ssh:
shell #脚本执行的语言用处等
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
planB (外网可用)
-
- 装置 Runner
gitlab-runner 下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum
CC : 区别 el6 和 el7, 去相应的目录下下载即可
rpm -ivh gitlab-runner-10.5.0-1.x86_64.rpm
还有一种形式为脚本装置(有外网拜访权限下能够应用):
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
yum install gitlab-ci-multi-runner
-
- 配置 Runner(可选)
默认状况,Runner 是通过 gitlab-runner 的这个用户来执行一系列操作,其工作目录也是在 gitlab-runner 的用户目录上面。如果应用默认 gitlab-runner 用户操作一些文件时常常会遇到权限问题,就须要给 gitlab-runner 赋权。咱们通过以下形式批改。
gitlab-runner uninstall # 删除服务
gitlab-runner install –working-directory /root –user root # 增加服务,别离制订工作目录和执行用户为 /root 和 root
gitlab-runner restart # 重启服务
gitlab-runner status # 查看状态
输入:gitlab-runner: Service is running!
ps -ef | grep gitlab-runner # 查看是否失效
输入:root 17454 1 0 Mar23 ? 01:18:03 /usr/bin/gitlab-runner run –working-directory
/home/jack –config /etc/gitlab-runner/config.toml –service gitlab-runner –syslog –user jack
-
- 注册 runner
把该 Runner 信息注册给 GitLab 的 CI 服务,通知 CI”我是谁,我能做什么”。
先关上 GitLab 上须要主动部署的我的项目界面,找到该项目标 Settings –> CI/CD –> Runners settings。不同版本的 GitLab 界面可能有些差异。
- 注册 runner
在红色的区域能够看到 URL 和 Token,这两个加起来就是该项目标惟一信息了。而后咱们 Runner 服务的 root 用户下执行以下命令
# gitlab-runner register
WARNING: Running in user-mode.
WARNING: The user-mode requires you to manually start builds processing:
WARNING: $ gitlab-runner run
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab.xxxx.com # 填写方才看到的 URL
Please enter the gitlab-ci token for this runner:
Cxxxxxxxxxxxxx4 # 填写方才看到的 Token
Please enter the gitlab-ci description for this runner:
[dev_srv]:iuh_801_testlab_master # 形容一下该 runner,该和.gitlab-ci.yml 文件外面配置的 tags 雷同即可
Please enter the gitlab-ci tags for this runner (comma separated):
tags_of_iuh_801_testlab_master # 该 runner 起个名字
Whether to run untagged builds [true/false]:
[false]: # 间接回车
Whether to lock the Runner to current project [true/false]:
[true]: # 间接回车
Registering runner... succeeded runner=eHMqXjw9
Please enter the executor: shell, ssh, docker+machine, docker, docker-ssh, parallels, virtualbox, docker-ssh+machine, kubernetes:
shell # 填写 runner 执行时须要应用什么执行器,个别都填 shell 或者 docker。Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
![](/media//202108/2021-08-12_100406.png)
这样就实现了 Runner 与 CI 之间的信息互注册。Runner 晓得了本人须要操作哪个我的项目,CI 也晓得了该 runner 的存在。接着刷新一下 Runners settings 界面,就会看到新注册的 Runner 了。后面有一个绿色的圆就代表注册胜利。
-
- 配置 yml 文件
在我的项目根目录下增加一个.gitlab-ci.yml 文件。内容如下:
- 配置 yml 文件
stages:
- update_master
update_production_env:
stage: update_master
script:
- cd /u02/nginx/html/mercial.iu95522.com
- git pull origin master
- if [-f "system/config.php"]; then
- rm -rf system/config.php
- fi
- cp system/config.php.master system/config.php
- chown -R nginx.nginx *
only:
- master
tags:
- tags_of_iuh_801_mercial.iu95522.com_master
stages 下的 deploy 阐明在代码提交后 CI 须要执行 deploy 节点的内容。deploy 的 script 就是一个个 shell 命令,这里须要留神每个命令都以杠 + 空格结尾。only:只有向 dev 分支提交代码时才失效。tags:只有领有该 tags 的 Runner 才须要执行