返回
创建于
状态公开
GitHub Actions 自托管运行器配置指南:常见问题与解决方案
在配置 GitHub Actions 自托管运行器时,可能会遇到一些常见问题,比如权限限制或运行器无法持续运行。本文将直击核心,分享这些问题的解决方案,帮助你顺利完成配置。
问题 1:为什么不能用 sudo 运行自托管运行器?
现象:
运行 ./config.sh 或 ./run.sh 时使用 sudo,会报错“Must not run with sudo”。
原因:
GitHub Actions 要求运行器以非 root 用户身份运行,提升安全性,避免 root 权限带来的风险。
解决方案:
- 推荐方式:创建普通用户并以此用户运行。
1sudo adduser --disabled-password --gecos "" github_runner 2sudo -u github_runner -i 3./config.sh --url <URL> --token <TOKEN> 4./run.sh - 临时方法:设置环境变量
RUNNER_ALLOW_RUNASROOT=1,但不建议用于生产环境。 - 工作流中需要
sudo:为运行器用户配置sudoers文件,允许无密码执行sudo。
问题 2:--gecos 参数的作用是什么?
现象:
创建用户时使用 --gecos "",想知道其功能。
解释:
--gecos 用于设置 /etc/passwd 中的 GECOS 字段(用户信息,如全名)。加上 --gecos "" 可跳过交互输入,适合脚本自动化。
示例:
1sudo adduser --disabled-password --gecos "" github_runner结果在 /etc/passwd 中显示:
1github_runner:x:1001:1001::/home/github_runner:/bin/bash问题 3:如何让 ./run.sh 持续运行不退出?
现象:
直接运行 ./run.sh,关闭终端后进程终止。
解决方案:
- 临时方法:使用
nohup或screen/tmux保持后台运行。nohup示例:1nohup ./run.sh > runner.log 2>&1 &
- 推荐方法:将运行器配置为系统服务(
systemd),实现稳定运行和开机自启。
安装为系统服务的步骤
步骤:
- 进入运行器目录:
1cd /home/github_runner/actions-runner - 安装服务并指定用户:
1sudo ./svc.sh install github_runner - 启动服务:
1sudo ./svc.sh start - 检查状态:
1systemctl status actions.runner.* - 查看日志:
1journalctl -u actions.runner.* -f
优点:
- 终端关闭不影响运行。
- 支持开机自启。
- 可通过
systemctl管理。
总结
配置 GitHub Actions 自托管运行器时,需关注两点:
- 权限管理:使用普通用户运行,避免
sudo。 - 持续运行:推荐配置为
systemd服务,确保稳定性和可靠性。