gitlab-runner|gitlab-runner 部署及关联gitlab

简介

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 ~]# wgethttps://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,6342.23MB/sin 11s2021-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.cfggitlab-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 root373910 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 root380137800 10:49 pts/000:00:00 grep --color=auto runner [root@gitlab ~]# gitlab-runner uninstall Runtime platformarch=amd64 os=linux pid=3802 revision=0e5417a3 version=12.0.1 [root@gitlab ~]# gitlab-runner install --working-directory /root --user root Runtime platformarch=amd64 os=linux pid=3821 revision=0e5417a3 version=12.0.1 [root@gitlab ~]# gitlab-runner restart Runtime platformarch=amd64 os=linux pid=3853 revision=0e5417a3 version=12.0.1 [root@gitlab ~]# gitlab-runner status Runtime platformarch=amd64 os=linux pid=3868 revision=0e5417a3 version=12.0.1 gitlab-runner: Service is running! [root@gitlab ~]# ps -ef | grep gitlab-runner root386110 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 platformarch=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... succeededrunner=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!

gitlab-runner|gitlab-runner 部署及关联gitlab
文章图片

planB (外网可用)
    1. 安装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

    1. 配置Runner(可选)
【gitlab-runner|gitlab-runner 部署及关联gitlab】默认情况,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
    1. 注册runner
      把该Runner信息注册给GitLab的CI服务,告诉CI”我是谁,我能做什么”。
      先打开GitLab上需要自动部署的项目界面,找到该项目的 Settings –> CI/CD –> Runners settings 。不同版本的GitLab界面可能有些差别。
在红色的区域可以看到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... succeededrunner=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了。 前面有一个绿色的圆就代表注册成功。

    1. 配置yml文件
      在项目根目录下添加一个.gitlab-ci.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才需要执行

    推荐阅读