GitHub Actions 自托管 Runner 搭建:告别每分钟 $0.00 的焦虑

概述

GitHub Actions 免费额度对每个人来说都够用——每月 2000 分钟。但当你同时跑多个项目,或者要在 CI 里训练小模型、跑大规模测试时,这些分钟数眨眼就没了。

自托管 Runner 就是解决方案:把 GitHub Actions 的工作流跑在你自己的 VPS 上,不限时、不限资源,还能装 GPU。配置一次,永久免费跑。

准备工作

  • 一台 Linux VPS(推荐 Ubuntu 22.04+,2C4G 起步)
  • 已安装 Docker(没有的话跑:curl -fsSL https://get.docker.com | sh)
  • 一个 GitHub 仓库(用于获取 Runner 注册 token)
  • 约 500MB 磁盘空间(Runner 二进制 + Docker 基础镜像)

确认 Docker 就绪:

docker info >/dev/null 2>&1 && echo "Docker OK" || echo "请先安装 Docker"

步骤

第一步:在 GitHub 仓库获取注册 Token

进入你要用的 GitHub 仓库,打开 Settings → Actions → Runners → New self-hosted runner。

选择操作系统和架构(Linux x64),页面会给出一个创建命令,复制下来,类似:

config.sh --url https://github.com/your-org/your-repo --token xxxxxxxx

注意:Token 5 分钟过期,拿到后立刻执行。

第二步:创建 Runner 用户和目录

sudo useradd -m -s /bin/bash runner
sudo mkdir -p /opt/runner
sudo chown runner:runner /opt/runner

切换到 runner 用户,下载 Runner 包:

sudo su - runner
cd /opt/runner
RUNNER_VERSION=$(curl -s https://api.github.com/repos/actions/runner/releases/latest | grep tag_name | cut -d'"' -f4)
curl -fsSL "https://github.com/actions/runner/releases/download/${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION#v}.tar.gz" | tar xz

第三步:运行配置脚本

用你在第一步复制的命令配置 Runner:

./config.sh --url https://github.com/your-org/your-repo --token YOUR_TOKEN_HERE

配置过程中会问你 Runner 名称(随便起,比如 my-runner-01)和工作目录(默认 /opt/runner/_work 即可)。

配置完成后 Runner 会在前台运行。先按 Ctrl+C 停下,用 systemd 管理它。

第四步:配置 systemd 服务

sudo ./svc.sh install
sudo ./svc.sh start

或者手动写 service 文件:

sudo tee /etc/systemd/system/runner.service > /dev/null <<'EOF'
[Unit]
Description=GitHub Actions Runner
After=network-online.target docker.service

[Service]
User=runner
Group=runner
WorkingDirectory=/opt/runner
ExecStart=/opt/runner/run.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable runner
sudo systemctl start runner

第五步:验证 Runner 状态

在 GitHub 仓库的 Settings → Actions → Runners 页面,你应该能看到 my-runner-01 显示绿色在线状态。

在 VPS 上确认进程运行:

systemctl status runner

看到 active (running) 就说明成功了。

第六步:让 Workflow 使用你的 Runner

在 .github/workflows/your-workflow.yml 中指定 runner:

jobs:
  build:
    runs-on: self-hosted  # 使用自托管 Runner
    steps:
      - uses: actions/checkout@v4
      - name: Run my task
        run: echo "跑在自有 VPS 上,不限时!"

或者指定标签更精确地匹配:

# 配置 Runner 时加标签
./config.sh --url https://github.com/your-org/your-repo --token TOKEN --labels ubuntu,docker,ai
jobs:
  train:
    runs-on: [self-hosted, ai]
    steps:
      - run: python train.py

验证

# 检查 Runner 进程
systemctl is-active runner
# 输出:active

# 检查 Runner 日志
journalctl -u runner -f

# 在 GitHub 仓库页面查看 Runner 状态
# Settings → Actions → Runners → 应该显示绿色在线

触发一次 workflow 运行,确认日志里看到 Runner received by self-hosted runner 的提示。

常见问题

Q: Runner 一直离线怎么办?

A: 最常见的原因是网络问题。Runner 需要连接 api.github.com。检查 DNS 和防火墙:

curl -I https://api.github.com
curl -I https://github.com

Q: 如何同时跑多个项目的 workflow?

A: 在每个项目的 Settings → Actions → Runners 页面,选择"Add runner",配置相同的 Runner。Runner 会自动处理所有项目的任务。

Q: Runner 占用太多内存怎么办?

A: 可以在 config.sh 时加 --runasuser runner --nogroup 限制权限,或者用 Docker 方式运行 Runner:

docker run -d --name runner -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/actions/runner:latest

Q: 如何自动更新 Runner?

A: 写个定时任务每月自动更新:

# 加入 crontab
0 3 1 * * cd /opt/runner && ./config.sh remove && curl -fsSL "https://github.com/actions/runner/releases/latest/download/actions-runner-linux-x64.tar.gz" | tar xz && ./config.sh --url URL --token TOKEN && ./svc.sh start

Q: Runner 可以跑 GPU 任务吗?

A: 可以。在 Runner 所在的机器上装好 NVIDIA Docker 驱动,然后在 workflow 里指定容器:

jobs:
  train:
    runs-on: [self-hosted, gpu]
    container:
      image: nvidia/cuda:12.0-runtime-ubuntu22.04
      options: --gpus all

自托管 Runner 配一次管一年,再也不怕 GitHub Actions 的分钟数告急。你的 VPS 还有多少空闲资源?够跑几个 Runner?

这篇文章对你有帮助吗?