背景:现有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:手动配置证书(推荐)
-
获取 GitLab 服务器的证书:
bashopenssl s_client -connect example.com:7443 -showcerts </dev/null 2>/dev/null | openssl x509 -outform PEM > gitlab.crt
-
将证书添加到系统信任链:
CentOS/RHEL:
go```bash sudo cp gitlab.crt /etc/pki/ca-trust/source/anchors/ sudo update-ca-trust ``` ```
-
重新注册 Runner:
bashgitlab-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 部署服务器的系统即可。