简介
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.rpmResolving mirrors.xxxxe.com (mirrors.xxxxe.com)... 10.130.104.43Connecting to mirrors.xxxxe.com (mirrors.xxxxe.com)|10.130.104.43|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 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 ~]# lsanaconda-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: NOKEYerror: Failed dependencies: git is needed by gitlab-runner-12.0.1-1.x86_64[root@gitlab ~]# cat /etc/redhat-releaseCentOS Linux release 7.4.1708 (Core) [root@gitlab ~]# rpm -ivh gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm --nodeps --forcewarning: gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 880721d4: NOKEYPreparing... ################################# [100%]Updating / installing... 1:gitlab-runner-12.0.1-1 ################################# [100%]GitLab Runner: creating gitlab-runner...
- 批改gitlab-runner 启动用户
[root@gitlab ~]# ps -ef | grep runnerroot 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-runnerroot 3801 3780 0 10:49 pts/0 00:00:00 grep --color=auto runner[root@gitlab ~]# gitlab-runner uninstallRuntime platform arch=amd64 os=linux pid=3802 revision=0e5417a3 version=12.0.1[root@gitlab ~]# gitlab-runner install --working-directory /root --user rootRuntime platform arch=amd64 os=linux pid=3821 revision=0e5417a3 version=12.0.1[root@gitlab ~]# gitlab-runner restartRuntime platform arch=amd64 os=linux pid=3853 revision=0e5417a3 version=12.0.1[root@gitlab ~]# gitlab-runner statusRuntime platform arch=amd64 os=linux pid=3868 revision=0e5417a3 version=12.0.1gitlab-runner: Service is running![root@gitlab ~]# ps -ef | grep gitlab-runnerroot 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 registerRuntime platform arch=amd64 os=linux pid=3878 revision=0e5417a3 version=12.0.1Running in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):https://gitlab.xxxx.com #gitlab urlPlease enter the gitlab-ci token for this runner:Gxxxxxxxxxxxxxxxx7 #gitlab tokenPlease enter the gitlab-ci description for this runner:[gitlab]: wuhan-tongj-10.82.16.44Please enter the gitlab-ci tags for this runner (comma separated):wuhan-tongj-10.82.16.44Registering runner... succeeded runner=GLNByiz6Please 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/yumCC : 区别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 bashyum 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 registerWARNING: 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 # 填写方才看到的URLPlease enter the gitlab-ci token for this runner:Cxxxxxxxxxxxxx4 # 填写方才看到的TokenPlease 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=eHMqXjw9Please 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!这样就实现了Runner与CI之间的信息互注册。Runner晓得了本人须要操作哪个我的项目,CI也晓得了该runner的存在。接着刷新一下Runners settings界面,就会看到新注册的Runner了。后面有一个绿色的圆就代表注册胜利。
- 配置yml文件
在我的项目根目录下增加一个.gitlab-ci.yml 文件。内容如下:
- 配置yml文件
stages: - update_masterupdate_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_masterstages下的deploy阐明在代码提交后CI须要执行deploy节点的内容。deploy的script就是一个个shell命令,这里须要留神每个命令都以杠+空格结尾。only:只有向dev分支提交代码时才失效。tags:只有领有该tags的Runner才须要执行