GitLab CI/CD Runner安装注册配置

Gitlab CI/CD简单介绍:通过配置好的流程Runner帮助我们集成部署。

GitLab CI/CD

GitLab CI/CD Runner安装注册配置_ci

所以要实现CI/CD,两个关键步骤:CI/CD流程(.gitlab.yml脚本)、Runner安装。

今天介绍Runner的安装及注册。

1 Runner安装

1.1 yum安装

设置国内yum源北京外国语镜像网站,国外因为网络原因太慢了。

新建gitlab-runner.repo文件

touch /etc/yum.repos.d/gitlab-runner.repo

内容如下

[gitlab-runner]
name=gitlab-runner
baseurl=https://mirrors.bfsu.edu.cn/gitlab-runner/yum/el$releasever-$basearch/
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

开始安装

sudo yum makecache
sudo yum install gitlab-runner

1.2 安装包安装

sudo curl -L --output /usr/local/bin/gitlab-runner-14.7.0-1.x86_64.rpm "https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7/gitlab-runner-14.7.0-1.x86_64.rpm"

解压

rpm -ivh gitlab-runner-14.7.0-1.x86_64.rpm

2 Runner注册

从gitlab项目配置中获取注册runner所需的url和token

GitLab CI/CD Runner安装注册配置_docker_02

注册Runner

gitlab-runner register

## 输入url
a、Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
 
## 输入token
b、Please enter the gitlab-ci token for this runner
 
## 写个描述
c、Please enter the gitlab-ci description for this runner
 
## 这个tag很重要,好好想个名字并记住,随后在ci配置中需要对应上。
d、Please enter the gitlab-ci tags for this runner (comma separated)
 
## ci没有配置tags时是否执行这个runner?建议采用默认值。            
e、Whether to run untagged builds [true/false]
 
## 是否只对当前工程有效?理论上讲只有“Shared runners”才有效。选true。            
f、Whether to lock Runner to current project [true/false]
 
## 选择一个执行器。我们接来下的方案是基于shell的,输入shell。    
g、Please enter the executor: virtualbox, docker+machine, kubernetes, parallels, docker-ssh, shell, ssh, docker-ssh+machine, docker:

注册成功后列表中会出现Runner,绿色状态即为可用,注册成功。

GitLab CI/CD Runner安装注册配置_docker_03

3 Runner 配置

注册好的runner配置会放在/etc/gitlab-runner/config.toml中,配置参数详解

vi /etc/gitlab-runner/config.toml

# config.toml 
# 全局变量
concurrent = 5  # 限制可以同时运行的作业数量
check_interval = 0
user = "root" 
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

# 第一个runner gitlab-runner register 命令注册后,会自动将配置信息保存在该文件下
[[runners]]
  name = "first runner"
  url = "https://gitcode.xxx.com/"
  id = 1856
  token = ""
  token_obtained_at = 2023-03-05T05:49:39Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "shell"

# 第二个runner
[[runners]]
  name = "second runner"
  url = "https://gitcode.xxx.com/"
  id = 1857
  token = ""
  token_obtained_at = 2023-03-07T02:14:18Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "cr-dev.yealinkops.com/tools/worklink/packager:1.0.0"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false

保存后,runner会自动读取配置。

4 问题

4.1 Runner启动后用户权限问题

Runner启动时默认用户为gitlab-runner,如果你的打包脚本涉及更高用户权限的操作就会提示权限不足。

要让runner拥有执行权限有两个思路:1、提升gitlab-runner用户的权限 2、使用root用户启动runner

查看gitlab-runner进程

ps aux | grep gitlab-root      

# 可以看到 --user gitlab-runner  即用户为gitlab-runner
1109  0.0  0.0 757304 26476 ?        Ssl  Mar06   0:42 /usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --user gitlab-runner
4.1.1 方法一 提升gitlab-runner用户的权限
vim /etc/sudoers

最后一行加入

# ALL 拥有全部权限 NOPASSWD 无需密码
gitlab-runner ALL=(ALL) NOPASSWD : ALL
4.1.2 方法二 使用root用户启动runner

卸载掉gitlab-runner默认用户

sudo gitlab-runner uninstall

重新安装gitlab-runner并将用户设置为root

# (这里如果出现错误,不能重新安装,可能是/home/gitlab-runner目录不存在,需要先建立gitlab-runner目录
gitlab-runner install --working-directory /home/gitlab-runner --user root

启动Runner

sudo gitlab-runner start