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

如何为GitHub Actions配置两台服务器的Runner并按分支指定执行实例

按分支指定GitHub Actions自托管Runner的解决方案

你的问题核心在于两个自托管Runner都只使用了默认的self-hosted标识,GitHub Actions会在所有满足self-hosted条件的可用Runner中随机分配任务,导致QA分支的Workflow没有定向到你准备的第二个Runner。要解决这个问题,我们可以通过给Runner添加专属标签,并在Workflow中指定对应标签来实现分支与Runner的绑定,具体步骤如下:

1. 给自托管Runner添加专属标签

  1. 打开你的GitHub仓库,进入Settings -> Actions -> Runners页面
  2. 找到对应DEV分支的第一台EC2实例的Runner,点击右侧的三个点选择Edit labels
  3. 添加一个专属标签,比如dev-runner,保存修改
  4. 对QA分支的第二台EC2实例的Runner执行同样操作,添加标签qa-runner

2. 修改DEV分支的Workflow配置

将原Workflow中的runs-on: self-hosted替换为你给DEV Runner设置的标签dev-runner,确保DEV分支的任务只会分配到对应Runner:

# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
  push:
    branches: [DEV]
  pull_request:
    branches: [DEV]
jobs:
  build:
    runs-on: dev-runner # 替换为DEV Runner的专属标签
    strategy:
      matrix:
        node-version: [14.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: "npm"
    - run: npm ci
    # - run: pm2 stop app.js
    - run: pm2 start ecosystem.config.js --update-env

3. 修改QA分支的Workflow配置

同样地,将QA Workflow中的runs-on: self-hosted替换为qa-runner,定向到第二台EC2实例的Runner:

# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: QA Build
on:
  push:
    branches: [ QA ]
  pull_request:
    branches: [ QA ]
jobs:
  build:
    runs-on: qa-runner # 替换为QA Runner的专属标签
    strategy:
      matrix:
        node-version: [14.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm i
    - run: pm2 start ecosystem.config.js --update-env

完成以上配置后,当你向DEV分支推送代码时,任务会自动分配到带有dev-runner标签的第一台EC2实例Runner;向QA分支推送时,则会使用带有qa-runner标签的第二台实例Runner,实现分支与Runner的精准绑定。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 04:02:31