You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在GitHub Actions同一工作流中动态分配runs-on值?

如何在GitHub Actions同一工作流中动态设置runs-on值

完全可以在同一工作流中根据推送分支动态设置runs-on的值,只需利用GitHub Actions的上下文表达式实现分支判断即可。

修改后的工作流配置如下:

name: test if conditions
on:
  push:
    branches: [master, test]
env:
  TEST_BRANCH: "test"
  EMPTY_VALUE: ""
jobs:
  test-conditions:
    runs-on: ${{ github.ref_name == 'master' ? ['self-hosted', 'products', 'latest'] : 'ubuntu-latest' }}
    steps:
      - name: simple evaluation on test
        run: echo "runs"

关键说明:

  • 通过github.ref_name上下文变量获取当前触发工作流的分支名称
  • 使用三元表达式进行分支判断:当分支为master时,使用自托管运行器数组['self-hosted', 'products', 'latest'];当分支为test时,使用官方托管的ubuntu-latest
  • 确保YAML语法正确,数组形式的运行器标签需要完整包裹在表达式中

内容的提问来源于stack exchange,提问作者Wiki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 08:27:26