温馨提示×

centos gitlab怎样加runner

小樊
40
2026-10-05 20:01:23
栏目: 智能运维

在 CentOS 上给 GitLab 添加 Runner,一般分为安装 GitLab Runner → 注册 Runner → 启动并验证几个步骤。下面以**GitLab 官方 Runner(shell / docker executor)**为例说明。


一、安装 GitLab Runner(CentOS)

1. 添加 GitLab 官方仓库

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash

2. 安装 gitlab-runner

sudo yum install gitlab-runner -y

安装完成后,会创建一个 gitlab-runner 用户。


二、获取 GitLab 的注册信息

在 GitLab 页面中:

  • 项目级 Runner
    项目 → Settings → CI/CD → Runners
  • 全局 Runner(管理员)
    Admin → Runners

你会看到:

  • URL(如 https://gitlab.example.com/)
  • Registration Token

三、注册 Runner

执行注册命令:

sudo gitlab-runner register

按提示输入:

Enter the GitLab instance URL:
https://gitlab.example.com/

Enter the registration token:
xxxxxx

Enter a description for the runner:
centos-runner

Enter tags for the runner (comma-separated):
build,centos

Enter an executor:
shell

常用 executor:

  • shell:直接在 CentOS 上执行(简单)
  • docker:需要已安装 Docker
  • docker+machine、kubernetes 等(高级)

四、启动并设置开机自启

sudo systemctl start gitlab-runner
sudo systemctl enable gitlab-runner

查看状态:

sudo systemctl status gitlab-runner

五、验证 Runner 是否在线

回到 GitLab:

  • Settings → CI/CD → Runners
  • 看到 Runner 状态为 green(online) 即成功

六、常见问题

1. shell executor 没权限

Runner 默认用 gitlab-runner 用户执行:

sudo usermod -aG docker gitlab-runner
sudo chmod 666 /var/run/docker.sock

2. 防火墙 / 网络问题

确保 CentOS 能访问 GitLab URL:

curl https://gitlab.example.com

3. 使用 Docker executor(示例)

sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker

注册时 executor 选 docker,默认镜像填 alpine:latest


如果你告诉我:

  • GitLab 是 自建还是 gitlab.com
  • Runner 要用 shell / docker / k8s
  • CentOS 版本(7 还是 8/9)

我可以给你更精确的配置示例(含 .gitlab-ci.yml)。

0 踩