如何在GitHub父工作流中指定子工作流的runs-on标签
我完全懂你现在的困扰——想让同一个可重用子工作流,被不同父工作流调用时跑在不同标签的自托管runner上,之前传字符串参数的方式虽然日志显示标签解析对了,但runner就是不接活,确实挺闹心的。
问题出在GitHub Actions对runs-on参数的解析逻辑上:你直接传带方括号的字符串(比如"[self-hosted, macOS, deploy]"),它会把整个字符串当成单个标签,而不是三个独立标签的数组。你的runner标签是self-hosted、macOS、deploy三个单独的,自然匹配不上这个“组合字符串标签”。
下面给你两种可行的解决方法,亲测有效:
方法一:通过标识参数+条件判断选择runner标签
这种方式更清晰,适合只有几种固定runner组合的场景:
在子工作流中新增一个环境标识参数
修改你的build-mac.yml,添加一个用来区分是build还是deploy环境的输入参数:name: Build Release Mac on: workflow_call: inputs: # 保留原有的参数 build_installer: description: 'Sign products and build installer' type: boolean required: false default: false installer_artifact: description: 'Upload installer to artifacts' type: boolean required: false default: false # 新增:指定runner环境的参数 runner_env: description: 'Choose runner environment (build/deploy)' type: string required: false default: "build" jobs: MacBuild: name: macOS Build Release # 用条件判断选择对应的标签数组,注意用fromJSON转成真正的数组 runs-on: ${{ fromJSON(inputs.runner_env == 'deploy' ? '["self-hosted", "macOS", "deploy"]' : '["self-hosted", "macOS"]') }} steps: # 原有的步骤保持不变 - name: Checkout Repository uses: actions/checkout@main with: path: ${{ github.event.repository.name }} - name: Check out other repository uses: actions/checkout@main with: repository: organization/other_repository token: ${{ secrets.RUNNERS_PAT }} path: other_repository ref: development在父工作流中传递环境标识
比如在deploy.yml里调用子工作流时,指定runner_env: "deploy":name: Deploy on: push: jobs: MacBuild: name: Mac build uses: ./.github/workflows/build-mac.yml with: build_installer: true installer_artifact: true runner_env: "deploy" # 指定用deploy环境的runner secrets: inherit而
build.yml里可以不传这个参数(用默认的build),或者显式传runner_env: "build"。
方法二:直接传递JSON格式的标签数组字符串
如果你需要更灵活的标签组合,可以直接传JSON格式的标签数组字符串,再用fromJSON解析成真正的数组:
修改子工作流接收标签参数
在build-mac.yml里新增runner_tags参数:name: Build Release Mac on: workflow_call: inputs: # 保留原有的参数 build_installer: description: 'Sign products and build installer' type: boolean required: false default: false installer_artifact: description: 'Upload installer to artifacts' type: boolean required: false default: false # 新增:接收runner标签的JSON字符串 runner_tags: description: 'Runner tags in JSON array format' type: string required: false default: '["self-hosted", "macOS"]' jobs: MacBuild: name: macOS Build Release # 用fromJSON把字符串转成标签数组 runs-on: ${{ fromJSON(inputs.runner_tags) }} steps: # 原有的步骤保持不变 ...在父工作流中传递正确格式的标签字符串
注意要传标准JSON数组格式的字符串(每个标签用双引号包裹,整体用单引号括起来):- 在
deploy.yml里:MacBuild: name: Mac build uses: ./.github/workflows/build-mac.yml with: build_installer: true installer_artifact: true runner_tags: '["self-hosted", "macOS", "deploy"]' secrets: inherit - 在
build.yml里:MacBuild: name: Mac build uses: ./.github/workflows/build-mac.yml with: build_installer: true installer_artifact: true runner_tags: '["self-hosted", "macOS"]' secrets: inherit
- 在
额外排查点
如果还是不行,可以检查这几点:
- 确认自托管runner的标签拼写完全正确(比如有没有大小写错误,
macOS不是MacOS) - 确认目标runner处于在线、空闲状态
- 确认GitHub Actions能正常连接到自托管runner(比如网络通畅,runner的权限配置正确)
备注:内容来源于stack exchange,提问作者Matouš Vrbík

