Skip to main content

配置 ssh-runner 访问远程服务器

前置条件:

  • Ubunut
  • root 用户身份下进行操作
  • GitLab 15
  • GitLab Runner 15
  • 配置的 Runner 用于生产环境部署的,因此权限很高

简写:

  • 远程服务器:remote-server
  • Runner所在服务器:runner-server

确保可 ssh 免登陆

复制 runner-server 的公钥到 remote-server 的 authorized_keys

注:

  • runner-server 的公钥在 /root/.ssh/id_ed25519.pub 或 id_rsa.pub 中。
  • remote-server 的信任列表在 /root/.ssh/authorized_keys 其中每一行代表一个受信公钥。

若 runner-server 没有以上秘钥文件,则需创建秘钥对 ssh-keygen -t ed25519 -C "GitLab Runner Server" 若没有 authorized_keys 则需要先手动创建该文件。

切记手动在 runner-server 通过 ssh 登录一次 remote-server 看是否能通,然后再进入下一步。

注册并配置新的 SSH Runner

https://docs.gitlab.com/runner/executors/ssh.html

1. 在 UI 取得注册码 https://gitlab.xxx.com/groups/[group-name]/-/runners

2. 注册并绑定一个新的 Runner:

gitlab-runner register --url https://gitlab.xxx.com/ --registration-token xxx 按提示一直按回车。

注意,新版的 gitlab-runner register 不再支持传参:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872

3. 手动补全配置 vi /etc/gitlab-runner/config.toml

[[runners]]
name = "[the-server-name]-ssh-runner"
url = "https://gitlab.xxx.com"
token = "xxx"
executor = "ssh"
[runners.ssh]
user = "root"
host = "xxx.xxx.xxx"
port = "22"
identity_file = "/root/.ssh/[the-server-name]"
known_hosts_file = "/root/.ssh/known_hosts"

know_hosts_file 默认值是 ~/.ssh/known_hosts,不用特意配。见:https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/3074

identity_file 和 known_hosts_file 要对应,否则容易出现 有关 known_hosts 的错误。

4. 应用修改后的配置。

不用重启 gitlab-runner,它每隔3秒就会自动检测配置的更新并自动应用。

但是,GitLab 上显示的 Runner 状态可能在 30 分钟内都是 never contacted 让人疑惑。

实践是检验成功的唯一标准,最直接的测试方法是立刻在 CI 使用一下该 Runner。

注意点

一定要在 root 下注册新 runner

若是用普通用户身份执行 gitlab-runner 相关命令,你注册的 runner 是属于你自己的,并且不会自启动,需要手动 gitlab-runner run

反之,root 用户不会有这些问题。

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...

如果不小心用了普通用户注册 runner 且 runner 通过手动 gitlab-runner run 一切工作正常,可以复制 ~/.gitlab-runner/config.toml 的内容至 /etc/gitlab-runner/config.toml 中,使其成为 root 级别的自启动的 runner。

  • 配置里的秘钥路径要改成全路径。
  • 为保证不跟原来已注册的用户级别 runner 冲突:
    • token 要在 GitLab UI 里面取一个新的。
    • name 取一个新名字。

一定要注册 group 作用域下的 runner

若是管理员身份注册的 runner,是全 GitLab 共享的,不安全。

一定要通过 gitlab-runner register 命令创建新的 runner

首次注册一定要用 register,它会主动调用 GitLab 接口在数据库生成一个 Runner 记录。

后期改配置参数可以手动编辑 config.toml 文件。

ssh-runner 首次登录进远程服务器可能会报 known_hosts 问题

Preparing the "ssh" executor
Using SSH executor...
ERROR: Preparation failed: ssh command Connect() error: getting host key callback: open /root/.ssh/known_hosts: no such file or directory

解决方法一:手动在命令行 ssh 登录一次该远程服务器,会自动提示是否将公钥保存到 known_hosts 文件。

解决方法二:手动将远程服务器的公钥添加到 known_hosts 文件 ssh-keyscan -H the_host_name_or_ip >> ~/.ssh/known_hosts

还有可能,IP 或 域名是否写错了,特别是有多个服务器 IP 时,很容易弄混。

若遇到问题,最简单的方法是重新注册一次 runner

排查很久不值得。

【已解决】ERROR: Job failed: prepare environment

ERROR: Job failed: prepare environment: Process exited with status 1. Check https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading for more information

解决第一步:确认远程服务器的 Git LFS 已可用

文档有特别说明,远程服务器需要安装好 git-lfs,GitHub 主页 https://github.com/git-lfs/git-lfs

https://docs.gitlab.com/runner/executors/ssh.html GitLab Runner uses the git lfs command if Git LFS is installed on the remote machine. Ensure Git LFS is up-to-date on any remote systems where GitLab Runner runs using SSH executor.

操作步骤:

  1. 添加源 curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash https://packagecloud.io/github/git-lfs/install

  2. 安装 sudo apt-get install git-lfs

解决第二步:删除远程服务器对应用户的 ~/.bash_logout 文件

手动登录过,会生成默认的 .bash_logout 文件,里面的 clear console 相关代码会让 ssh-runner 无法工作。

参考