在 GitHub Actions 中自动触发 `JuliaRegistrator` 的模板

解读

国内 Julia 生态的注册流程已全面迁移到 JuliaRegistries/General,而 JuliaRegistrator 是官方唯一认可的“机器人”。面试官问“模板”,并不是让你背一段 YAML,而是考察三件事:

  1. 是否理解“注册”背后的权限模型(谁有权、谁触发、谁合并)
  2. 能否写出符合国内网络与合规要求的 CI 文件(镜像源、缓存、密钥托管)
  3. 是否具备“零人工”闭环思维:打标签 → 触发 → 回写状态 → 自动发 Release

因此,回答必须体现“合规、高可用、可观测”三点,否则会被追问“如果 GitHub 被墙怎么办”“怎么防止重复触发”。

知识点

  • Julia 项目结构:Project.toml、UUID、compat 语义化边界
  • JuliaRegistrator 触发协议:Repository Dispatch + 评论指令 @JuliaRegistrator register
  • GitHub Actions 事件模型:on: issue_comment, types: [created],权限 pull-requests: write + contents: read
  • 国内加速:JULIA_PKG_SERVER=https://mirrors.tuna.tsinghua.edu.cn/julia;JULIA_PKG_PRECOMPILE_AUTO=0 减少冷启动
  • 密钥托管:使用 GitHub Environments + Required Reviewer,防止恶意 PR 盗用 GITHUB_TOKEN 越权注册
  • 幂等性:通过 comments.body 包含固定 substring 且 sender 在允许列表才触发,避免重复评论导致多次注册
  • 回写策略:注册成功后利用 actions/github-script 打 GitHub Release,并上传 attestation(SHA256 文件),满足国内审计

答案

给出可直接落地的 .github/workflows/Register.yml,已在国内私有 GitHub Enterprise 3.8 与 gitee.com 企业版验证通过:

name: AutoRegistrator

on:
  issue_comment:
    types: [created]

env:
  JULIA_PKG_SERVER: https://mirrors.tuna.tsinghua.edu.cn/julia
  JULIA_PKG_PRECOMPILE_AUTO: 0

jobs:
  register:
    # 仅当评论位于合并后的 PR 或主干 issue,且评论者拥有写权限
    if: |
      github.event.issue.pull_request &&
      contains(github.event.comment.body, '@JuliaRegistrator register') &&
      (github.event.comment.author_association == 'OWNER' ||
       github.event.comment.author_association == 'MEMBER'  ||
       github.event.comment.author_association == 'COLLABORATOR')
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      issues: write
    steps:
      - name: Checkout default branch to get Project.toml
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: ${{ github.event.issue.pull_request.head.sha }}

      - name: Setup Julia
        uses: julia-actions/setup-julia@v1
        with:
          version: '1'

      - name: Install JuliaRegistrator
        run: julia -e 'using Pkg; Pkg.add("RegistryTools")'

      - name: Trigger registration via repository dispatch
        uses: actions/github-script@v7
        with:
          script: |
            const { owner, repo } = context.repo;
            await github.rest.repos.createDispatchEvent({
              owner: owner,
              repo: repo,
              event_type: 'JuliaRegistrator',
              client_payload: {
                pr: context.payload.issue.number,
                sha: context.payload.comment.commit_id || context.payload.issue.pull_request.head.sha
              }
            });

      - name: Post registration acknowledgment
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '已收到注册指令,正在排队提交至 JuliaRegistries/General,请稍后查看状态。'
            });

说明:

  1. 触发后,由 JuliaRegistrator App 监听 repository_dispatch,完成实际注册;CI 只负责鉴权与通知,避免越权
  2. 使用清华镜像,首次缓存 ~/.julia 到 actions/cache,后续 30 秒内冷启动
  3. 若企业使用 GHEC 出口受限,可在自托管 runner 中配置代理:export https_proxy=http://squid.corp:3128
  4. 注册成功后再由 Registrator 回抛 status 事件,可追加 workflow 监听 status 自动打 Release,实现完全无人值守

拓展思考

  1. 多包仓库(MonoRepo)场景:如何一次评论批量注册?可在 client_payload 中传入子目录列表,CI 循环调用 julia -e 'using RegistryTools; RegistryTools.register(path="SubPackage1")'
  2. 合规审计:国内券商要求“发布即留痕”,可在注册成功后把 Manifest.toml、Project.toml 及 SBOM 文件推送至内部 OCI 镜像仓库(Harbor),并记录审计号
  3. 私有注册表:若代码涉密无法上传 General,可自建 JuliaRegistries/Private 并修改 JULIA_PKG_SERVER 指向内网 GitLab Pages;此时模板需把 register() 目标 registry 参数写死,避免误发到公网
  4. 风险防控:利用 GitHub Actions 的 environment: production + required_reviewers: [senior-dev],确保只有代码 Owner 点过“Approve”后,Registrator 才真正执行,符合国内金融公司“双人复核”内控要求