Search code examples
githubgithub-actionscicd

Github actions how to configure two runners in two servers


enter image description here

I have a GitHub repo called api. api has two branches DEV and QA

I have set up a workflow for the DEV branch and worked correctly.

This is the workflow for DEV branch

# 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: self-hosted
    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

Then I created my second EC2 instance and second runner and another workflow file

# 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: self-hosted
    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

But whenever I push some code to the QA branch still my first runner runs and first EC-2 instance. Seems the second instance or workflow doesn't use at all. How do I specify the runner and the instance based on the branch?


Solution

  • If you just have two runners with the default setup, you will not be able to differentiate between the two. As such a job just takes any of the two.

    A label can mark one specific runner, which you can then choose directly. See the GitHub self-hosted runners docs on labels

    You can then use the specific runner like this

    runs-on: [self-hosted, dev]