gitlab-runner 独立服务器部署

背景:现有gitlab 服务资源紧张,之前的 ci/cd 流程一次部署需要十分钟左右,为了优化,决定在其他服务器部署远程 runner 进行任务。

环境: gitlab 服务使用的乌班图系统,gitlab-runner(截止发文最新版本:17.11.0) 环境是 CentOS 7

私有化部署的 gitlab 使用了免费签发的 ssl 证书(可能会导致问题)

安装 GitLab Runner(使用官方仓库)

1. 添加 GitLab Runner 的官方仓库

bash 复制代码
sudo curl -L --output /etc/yum.repos.d/gitlab-runner.repo https://packages.gitlab.com/install/repositories/runner/gitlab-runner/config_file.repo

2. 安装 GitLab Runner

bash 复制代码
sudo yum install -y gitlab-runner

安装完成后,可以通过命令验证版本:

bash 复制代码
gitlab-runner --version

3. 注册 Runner(连接到你的 GitLab 项目)

在注册之前先到你的 gitlab 服务setting 页面获取 Token

Settings > CI/CD > Runners > Expand

bash 复制代码
sudo gitlab-runner register

执行后会依次提示以下信息:

提示 示例输入
GitLab instance URL gitlab.com 或你的 GitLab 地址
Registration token 你在 GitLab 项目中获得的 Token
Description runner 的描述,比如 my-runner
Tags 比如 docker,linux(可选)
Executor 常用:shell, docker,输入你想用的执行器

如果你选择 docker 执行器,还会询问默认镜像,比如:

bash 复制代码
Please enter the default Docker image (e.g. ruby:2.7):
alpine:latest

这里因为部署的是前端项目所以选择 node18

4. 启动并启用 GitLab Runner 服务

bash 复制代码
sudo systemctl start gitlab-runner
sudo systemctl enable gitlab-runner


sudo gitlab-runner status // 查看运行状态
sudo gitlab-runner list // 查看runner 列表

以上是常规的注册路径如果操作完没有提示错误那么接下来配置gitlab-ci.yml即可完成工作流的配置,但是笔者遇到了

bash 复制代码
Post "https://example.com:7443/api/v4/runners": tls: failed to verify certificate: x509: certificate signed by unknown authority PANIC: Failed to register the runner.

简单查阅得知 GitLab Runner 无法验证 GitLab 服务器的 TLS 证书(可能是自签名证书或未受信任的 CA 签发的证书)。以下是解决方法:

方法 1:临时禁用 TLS 验证(不推荐生产环境)

chatGPT 给的答案是通过 export CI_SERVER_TLS_VERIFY="false" 设置不校验 TLS 经测试在 17.11.0 版本无效

方法 2:直接修改 Runner 配置

bash 复制代码
sudo vim /etc/gitlab-runner/config.toml

对应的 [runners] 部分添加:

bash 复制代码
tls_skip_verify = true

重启服务

bash 复制代码
sudo gitlab-runner restart

经测试在 17.11.0 版本无效(docker 模式)

方法 3:手动配置证书(推荐)

  1. 获取 GitLab 服务器的证书

    bash 复制代码
    openssl s_client -connect example.com:7443 -showcerts </dev/null 2>/dev/null | openssl x509 -outform PEM > gitlab.crt
  2. 将证书添加到系统信任链

    CentOS/RHEL

    go 复制代码
    ```bash
    sudo cp gitlab.crt /etc/pki/ca-trust/source/anchors/
    sudo update-ca-trust
    ```
    
    ```
  3. 重新注册 Runner

    bash 复制代码
    gitlab-runner register \
      --url "https://example.com:7443" \
      --registration-token "JTxavHtWMVgMe5os9vBv" \
      --executor "docker" \
      --description "my-237-runner" \
      --tag-list "vuejs,nodejs,frontend"

经过上面的步骤仍然无效(值得一提的是在 gitlab 服务器部署 gitlab-runner 到这里就成功了)

继续排查

通过

bash 复制代码
curl https://example.com:7443

得到

bash 复制代码
curl: (60) Peer's Certificate issuer is not recognized. More details here: http://curl.haxx.se/docs/sslcerts.html curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs). If the default bundle file isn't adequate, you can specify an alternate file using the --cacert option. If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might not match the domain name in the URL). If you'd like to turn off curl's verification of the certificate, use the -k (or --insecure) option. [root@VM-0-9-centos ~]#

意思是说明系统依然不信任你添加的证书,可能原因是:

  • .crt 只是服务器证书,没有包含完整的 证书链(中间 CA)
  • 所以 curl 验证时无法找到可信的颁发者

ok,那我从浏览器访问https://example.com:7443通过查看证书,或者直接去 gitlab 部署服务器拿到证书放到 gitlab-runner 部署服务器是不是就可以了

答案是不行,究极原因推测,gitlab 服务器证书使用在 nginx 三方证书提供给 nginx 的是pem文件为了符合系统 anchor 的规则,只是改为了crt格式,chartGPT 给出的说法是不影响,但在实际使用中明显存在问题,如何解决?从证书颁发机构下载原生 crt格式证书导入到 gitlab-runner 部署服务器的系统即可。

相关推荐
尖椒土豆sss7 小时前
关于GitLab代码仓库工具使用分享
gitlab
极小狐1 天前
极狐GitLab 项目 API 的速率限制如何设置?
大数据·运维·git·elasticsearch·gitlab
rider1891 天前
【1】CICD持续集成-docker本地搭建gitlab代码仓库社区版
ci/cd·docker·gitlab
极小狐2 天前
极狐GitLab 登录限制如何设置?
ci/cd·gitlab·devops·mcp
极小狐2 天前
极狐GitLab 议题和史诗创建的速率限制如何设置?
linux·运维·数据库·git·安全·gitlab·极狐gitlab
极小狐2 天前
极狐GitLab 项目和群组的导入导出速率限制如何设置?
ci/cd·gitlab·devops·mcp
极小狐2 天前
极狐GitLab 账号限制有哪些?
运维·ssh·gitlab·github·极狐gitlab
半个脑袋儿2 天前
Git提交修正的核心技巧:git commit --amend 的专业实践与深度解析
git·gitlab
极小狐4 天前
极狐GitLab 外部流水线验证功能解读
运维·ssh·gitlab·极狐gitlab