PR

GitHub Actions無料プランの制限と効率的な使い方:コスト最適化の実践テクニック

GitHub Actionsを始めたばかりで、「無料でどこまで使えるの?」「制限に引っかからないか心配」と感じていませんか?この記事では、GitHub Actionsの無料プランの詳細な制限と、その範囲内で最大限活用するための実践的なテクニックを解説します。

シリーズ記事

GitHub Actions の料金プラン概要

GitHub Actionsは基本的に使用量課金制ですが、各プランには無料枠が設定されています。

プラン別無料枠一覧

プラン 月間実行時間 ストレージ 同時実行ジョブ数
Free 2,000分 500MB 20ジョブ
Pro 3,000分 1GB 40ジョブ
Team 3,000分 2GB 60ジョブ
Enterprise 50,000分 50GB 180ジョブ

重要:これらの無料枠はパブリックリポジトリでは無制限で、制限があるのはプライベートリポジトリのみです。

実行時間の詳細な計算方法

OS別の実行時間係数

実行時間は使用するOSによって異なる係数で計算されます:

OS 係数 実際の1分間の消費時間
Linux 1倍 1分
Windows 2倍 2分
macOS 10倍 10分

実際の使用例での計算

例1:Node.js プロジェクトのCI

jobs:
  test:
    runs-on: ubuntu-latest  # Linux = 1倍
    # 実行時間:約5分
    # 消費時間:5分 × 1 = 5分

  test-windows:
    runs-on: windows-latest  # Windows = 2倍  
    # 実行時間:約5分
    # 消費時間:5分 × 2 = 10分

  test-macos:
    runs-on: macos-latest   # macOS = 10倍
    # 実行時間:約5分  
    # 消費時間:5分 × 10 = 50分

月間実行回数の計算例

  • Linux のみ:2,000分 ÷ 5分 = 400回実行可能
  • Windows のみ:2,000分 ÷ 10分 = 200回実行可能
  • macOS のみ:2,000分 ÷ 50分 = 40回実行可能

ストレージ制限の詳細

ストレージの種類と消費内訳

GitHub Actionsで消費されるストレージには以下があります:

種類 説明 保存期間
アーティファクト ワークフローで生成されるファイル 90日間(設定可能)
ログ ワークフロー実行ログ 90日間
キャッシュ 依存関係等のキャッシュ 7日間

ストレージ使用量の確認方法

リポジトリの設定から確認できます:

  1. リポジトリの「Settings」タブ
  2. 左メニューの「Actions」→「General」
  3. 「Artifacts and logs」セクションで使用量を確認

無料枠を最大限活用する最適化テクニック

1. 実行時間の最適化

① 適切なランナーの選択

# 悪い例:不要にmacOSを使用
jobs:
  test:
    runs-on: macos-latest  # 10倍の消費!
    steps:
      - run: echo "Hello World"  # Linux で十分

# 良い例:適材適所
jobs:
  test-linux:
    runs-on: ubuntu-latest    # 1倍消費
    steps:
      - run: npm test
      
  test-macos-specific:
    runs-on: macos-latest     # 10倍消費だが必要
    if: github.event_name == 'release'  # リリース時のみ
    steps:
      - run: xcodebuild test  # macOS特有の処理

② 条件付き実行の活用

jobs:
  expensive-job:
    runs-on: ubuntu-latest
    # PRの場合はスキップし、mainブランチのみで実行
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Heavy integration test
        run: npm run test:integration

  quick-job:
    runs-on: ubuntu-latest  
    # PRでは軽量なテストのみ実行
    if: github.event_name == 'pull_request'
    steps:
      - name: Unit tests only
        run: npm run test:unit

③ マトリックス戦略の最適化

# 悪い例:すべての組み合わせをテスト
strategy:
  matrix:
    os: [ubuntu-latest, windows-latest, macos-latest]
    node-version: [16, 18, 20]
# 合計9ジョブ = 非効率

# 良い例:重要な組み合わせのみ
strategy:
  matrix:
    include:
      - os: ubuntu-latest
        node-version: 18    # メイン環境
      - os: windows-latest  
        node-version: 18    # Windows互換性確認
      - os: ubuntu-latest
        node-version: 20    # 最新版テスト
# 合計3ジョブ = 効率的

2. キャッシュの効果的活用

① 依存関係のキャッシュ

- name: Cache node modules
  uses: actions/cache@v3
  with:
    path: ~/.npm
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-node-

- name: Install dependencies
  run: npm ci  # キャッシュがあれば高速化

② ビルド成果物のキャッシュ

- name: Cache build output
  uses: actions/cache@v3
  with:
    path: dist/
    key: build-${{ github.sha }}
    
- name: Build if not cached
  if: steps.cache.outputs.cache-hit != 'true'
  run: npm run build

3. 並列処理の最適化

① ジョブの並列実行

jobs:
  test:
    runs-on: ubuntu-latest
    steps: [...]
    
  lint:
    runs-on: ubuntu-latest  # testと並列実行
    steps: [...]
    
  build:
    needs: [test, lint]     # test, lint完了後に実行
    runs-on: ubuntu-latest
    steps: [...]

② ステップレベルでの効率化

# 悪い例:順次実行
- run: npm run test:unit
- run: npm run test:integration  
- run: npm run lint

# 良い例:並列実行可能なものは同時実行
- run: |
    npm run test:unit &
    npm run lint &
    wait  # すべて完了まで待機
- run: npm run test:integration  # 上記完了後実行

ストレージ最適化のテクニック

1. アーティファクト管理

① 必要なファイルのみ保存

# 悪い例:全ファイルを保存
- uses: actions/upload-artifact@v3
  with:
    name: build-output
    path: .  # ルートディレクトリ全体

# 良い例:必要なファイルのみ
- uses: actions/upload-artifact@v3
  with:
    name: dist-files
    path: |
      dist/
      !dist/**/*.map    # ソースマップは除外
      !dist/**/*.test.* # テストファイルは除外

② 保存期間の短縮

- uses: actions/upload-artifact@v3
  with:
    name: temporary-files
    path: temp/
    retention-days: 1  # 1日後に自動削除(デフォルト90日)

2. ログサイズの制御

# 冗長なログを避ける
- name: Build with minimal output
  run: npm run build --silent

# 大量出力は要約する  
- name: Test with summary
  run: |
    npm test > test-output.txt 2>&1
    if [ $? -eq 0 ]; then
      echo "✅ All tests passed"
    else
      echo "❌ Tests failed - showing last 50 lines:"
      tail -50 test-output.txt
    fi

コスト監視とアラート設定

使用量の監視方法

1. GitHub の設定画面での確認

  1. アカウント設定の「Billing and plans」
  2. 「Usage this month」セクション
  3. Actions の使用時間とストレージを確認

2. API での使用量取得

# GitHub CLI での使用量確認
gh api -H "Accept: application/vnd.github+json" \
  /user/settings/billing/actions

# レスポンス例
{
  "total_minutes_used": 1500,
  "total_paid_minutes_used": 0,
  "included_minutes": 2000,
  "minutes_used_breakdown": {
    "UBUNTU": 1000,
    "WINDOWS": 500,
    "MACOS": 0
  }
}

使用量アラートの実装

name: Usage Alert

on:
  schedule:
    - cron: '0 6 * * *'  # 毎日午前6時にチェック

jobs:
  check-usage:
    runs-on: ubuntu-latest
    steps:
      - name: Get usage data
        id: usage
        run: |
          USAGE=$(gh api /user/settings/billing/actions --jq '.total_minutes_used')
          LIMIT=2000
          PERCENT=$((USAGE * 100 / LIMIT))
          echo "usage=$USAGE" >> $GITHUB_OUTPUT
          echo "percent=$PERCENT" >> $GITHUB_OUTPUT
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Send alert if over 80%
        if: steps.usage.outputs.percent > 80
        run: |
          echo "⚠️ GitHub Actions usage: ${{ steps.usage.outputs.percent }}%"
          # Slack通知などの処理

実際のプロジェクト例での最適化Before/After

Before:最適化前(月間使用量:1,800分)

name: Unoptimized CI

on: [push, pull_request]

jobs:
  test:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        node-version: [16, 18, 20]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install  # キャッシュなし
      - run: npm test
      - run: npm run build
      - uses: actions/upload-artifact@v3
        with:
          name: build-${{ matrix.os }}-${{ matrix.node-version }}
          path: .  # 全ファイル保存

After:最適化後(月間使用量:400分)

name: Optimized CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest  # Linuxメイン
    strategy:
      matrix:
        node-version: [18, 20]  # 必要な版のみ
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'  # ビルトインキャッシュ
      - run: npm ci
      - run: npm test
      
  cross-platform:
    runs-on: ${{ matrix.os }}
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    strategy:
      matrix:
        os: [windows-latest]  # 重要なOSのみ
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'npm'
      - run: npm ci
      - run: npm test
      
  build:
    needs: test
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'npm'
      - run: npm ci
      - run: npm run build
      - uses: actions/upload-artifact@v3
        with:
          name: dist
          path: dist/
          retention-days: 7  # 短縮保存期間

最適化効果

  • 実行時間:1,800分 → 400分(78%削減)
  • 実行回数:月40回 → 月200回(5倍増加)
  • ストレージ:300MB → 50MB(83%削減)

有料プランへの移行タイミング

移行を検討すべき状況

  • 月間使用量が1,800分を超える:無料枠の90%到達
  • macOSでの頻繁なテストが必要:iOS/macOSアプリ開発
  • 大容量アーティファクトの保存:500MBを頻繁に超過
  • 同時実行ジョブ数の制限:20ジョブでは不足

プラン選択の指針

用途 推奨プラン 理由
個人開発 Free 2,000分で十分な場合が多い
小規模チーム Pro ($4/月) 3,000分 + 1GB ストレージ
中規模チーム Team ($4/user/月) 60並列ジョブ + 2GB
エンタープライズ Enterprise 50,000分 + 180並列ジョブ

まとめ:無料枠を賢く活用するポイント

GitHub Actions の無料プランを最大限活用するための重要なポイント:

実行時間最適化

  • Linuxを優先使用:Windows・macOSは必要な場合のみ
  • 条件付き実行:PRと本番で処理を使い分け
  • 効率的なマトリックス:全組み合わせテストを避ける

ストレージ最適化

  • 必要なファイルのみ保存:アーティファクトの精選
  • 短期保存の活用:retention-daysの適切な設定
  • キャッシュの効果的利用:依存関係とビルド成果物

監視と改善

  • 定期的な使用量確認:月間消費量の把握
  • 継続的な最適化:ワークフローの定期見直し
  • 適切なタイミングでの有料移行:成長に合わせたプラン変更

これらの最適化により、無料枠内で充実した CI/CD 環境を構築・運用できます。次回は、具体的なプロジェクト例でのワークフロー実装方法を詳しく解説していきます!

タイトルとURLをコピーしました