能否在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
相关产品推荐
相关产品推荐

