# 错误：自托管运行器注册令牌已过期。请生成新令牌并重新注册运行器。

- **ID:** `policy/github-actions-self-hosted-runner-token-expired`
- **领域:** policy
- **类别:** auth_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

GitHub 自托管运行器注册令牌具有可配置的过期时间（默认 1 小时），如果运行器注册过程延迟或令牌在过期后被存储并重用，注册将失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| GitHub Actions Runner v2.311.0 | active | — | — |
| GitHub Actions Runner v2.312.0 | active | — | — |
| GitHub Actions Runner v2.313.0 | active | — | — |

## 解决方案

1. ```
   从 GitHub 仓库设置生成新的注册令牌并重新注册运行器。
示例：
  # 在 GitHub 上：设置 > 操作 > 运行器 > 新建自托管运行器 > 复制令牌
  # 在运行器机器上：
  ./config.sh remove --token OLD_TOKEN
  ./config.sh --url https://github.com/owner/repo --token NEW_TOKEN
  ./run.sh
   ```
2. ```
   使用 GitHub API 在脚本中自动化令牌生成，避免手动步骤。
示例：
  # 使用 GitHub API 获取令牌
  TOKEN=$(curl -s -X POST -H "Authorization: token $GITHUB_PAT" \
    https://api.github.com/repos/owner/repo/actions/runners/registration-token | jq -r '.token')
  ./config.sh --url https://github.com/owner/repo --token $TOKEN
   ```
3. ```
   使用 Terraform 或 Ansible 等基础设施即代码工具管理运行器注册，确保每次生成全新的令牌。
示例（Terraform）：
  resource "github_actions_runner_registration_token" "this" {
    repository = "owner/repo"
  }
  # 然后在 provisioner 中使用令牌
   ```

## 无效尝试

- **Restarting the runner service** — Restarting the runner service does not refresh the expired token; the token is only valid for initial registration, not for ongoing operations. (90% 失败率)
- **Editing .runner file to extend token expiration** — Modifying the .runner file to extend the expiration date manually will not work because the token is validated server-side by GitHub. (95% 失败率)
- **Deleting and re-adding with same token** — Deleting the runner from GitHub and re-adding it with the same expired token will still fail; a new token must be generated. (85% 失败率)
