メインコンテンツまでスキップ

Github Actionsのランナーの使い分けと設定方法+α

Github Actionsのランナーの種類

  • 右側のWorkflow labelは設定時に使用する識別子
  • このリスト以外に大規模なジョブ向けの大型ランナーも存在するが、価格が高いためここでは割愛
Virtual MachineProcessor (CPU)Memory (RAM)Storage (SSD)ArchitectureWorkflow labelPrice (USD/min)
Linux15 GB14 GBx64ubuntu-slim$0.002
Linux28 GB14 GBx64ubuntu-latest, ubuntu-24.04, ubuntu-22.04$0.006
Windows28 GB14 GBx64windows-latest, windows-2025, windows-2022$0.010
Linux28 GB14 GBarm64ubuntu-24.04-arm, ubuntu-22.04-arm$0.005
Windows28 GB14 GBarm64windows-11-arm$0.010
macOS414 GB14 GBIntelmacos-15-intel$0.062
macOS3 (M1)7 GB14 GBarm64macos-latest, macos-14, macos-15, macos-26 (public preview)$0.062

使い分け

  • ubuntu-slim: 最軽量でこのランナーのみVMではなくコンテナで実行される。簡単なジョブで利用する。一部ツールがインストールされていないため、セットアップに時間がかかる可能性がある
  • ubuntu-latest: Linuxの最新安定版で、一般的なジョブで利用する
    • arm64: Arm64アーキテクチャが必要な場合か、コストを抑えたい場合に利用する
    • x86: デフォルトのLinuxランナーで、とりあえず選択する場合はこちら。性能は選択されるVMによって異なるため実行時間が変動する可能性がある
  • windows・macOS: WindowsやmacOSでのビルドやテストが必要な場合に利用する。コストが高いため、必要な場合にのみ選択する

Github Actionsのランナーの設定方法

ワークフローの定義ファイルに以下のように記述します。

jobs:
a_job:
runs-on: ubuntu-latest # ← ここにランナーのWorkflow labelを指定
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Build project
run: echo "Building on Ubuntu latest"

マトリクスを使用した複数ランナーの設定

複数のランナーで同じジョブを並行実行したい場合、matrixストラテジーを使用する。 この機能を利用すると、異なるOSや環境でのテストやビルドを条件や組み合わせに合わせて柔軟に指定できる。

基本的な使い方(複数OSでのテスト)

matrixに一つのパラメータ(この例ではOS)を指定すると、そのパラメータに定義された全ての値でジョブが実行される。例えば、Ubuntu、Windows、macOSの3つのOSでテストを実行する設定は以下のようになる。

jobs:
test:
runs-on: ${{ matrix.os }} # matrixで指定したOSを使用
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Run tests
run: echo "Testing on ${{ matrix.os }}"

OS✖️Node.jsバージョンの組み合わせ全てで実行する

matrixに複数のパラメータを指定すると、全ての組み合わせでジョブが実行される。例えば、UbuntuとWindowsの2つのOSと、Node.jsの18、20、22の3つのバージョンでテストを実行する設定は以下のようになる。 この設定では、2つのOS × 3つのNode.jsバージョン = 6つのジョブが並行実行される。

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18, 20, 22]
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Run tests
run: npm test

特定の組み合わせを除外する

excludeを使用することで、不要な組み合わせ(この例ではmacOS + Node.js 18)を除外可能。

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [18, 20, 22]
exclude:
- os: macos-latest
node-version: 18
steps:
- name: Run tests
run: npm test

特定の組み合わせを追加する

includeを使用すると、マトリクスに特定の組み合わせを追加可能(この例ではWindows + Node.js 22)。 experimental等のカスタムパラメータも追加してジョブ内で利用できる。

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
node-version: [20]
include:
- os: windows-latest
node-version: 22
experimental: true

steps:
- name: Run tests
run: npm test

実用例:コスト最適化

例えば、ユニットテストと統合テストは軽量なubuntu-slimで実行し、重いE2Eテストのみフルスペックのubuntu-latestで実行する設定は以下のようになる。

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-slim] # 軽量テストはubuntu-slimで実行
test-type: [unit, integration]
include:
- os: ubuntu-latest # E2Eテストのみフルスペックで実行
test-type: e2e
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Run ${{ matrix.test-type }} tests
run: npm run test:${{ matrix.test-type }}