GitHub Actions とは?
GitHub Actionsは、GitHubが提供するCI/CD(継続的インテグレーション/継続的デリバリー)プラットフォームです。リポジトリへのプッシュやPR作成をトリガーに、テスト・ビルド・デプロイを自動化できます。
GitHub Actions の特徴
| 特徴 | 説明 |
|---|---|
| GitHub統合 | リポジトリ内で完結。別サービス不要 |
| 無料枠が充実 | パブリックリポジトリは無制限 |
| マーケットプレイス | 再利用可能なアクションが豊富 |
| マトリクスビルド | 複数のOS・言語バージョンで同時テスト |
| セルフホストランナー | 自前のサーバーでも実行可能 |
基本概念
ワークフロー・ジョブ・ステップの関係
ワークフロー (.github/workflows/ci.yml)
└── ジョブ (test, build, deploy...)
└── ステップ (actions/checkout, npm test...)
- ワークフロー:
.github/workflows/に配置するYAMLファイル - ジョブ: ワークフロー内の実行単位。デフォルトで並列実行
- ステップ: ジョブ内で順番に実行されるコマンドやアクション
最初のワークフローを作る
Node.js プロジェクトの自動テスト
.github/workflows/ci.yml を作成します:
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npm test
- run: npm run lint
ワークフローの各部分を解説
on(トリガー)
on:
# プッシュ時
push:
branches: [main, develop]
paths:
- 'src/**'
- 'package.json'
# PR時
pull_request:
branches: [main]
# 手動実行
workflow_dispatch:
inputs:
environment:
description: 'デプロイ先'
required: true
default: 'staging'
type: choice
options:
- staging
- production
# スケジュール実行(cron式)
schedule:
- cron: '0 9 * * 1' # 毎週月曜9:00 UTC
runs-on(実行環境)
jobs:
test:
runs-on: ubuntu-latest # Linux
test-mac:
runs-on: macos-latest # macOS
test-win:
runs-on: windows-latest # Windows
よく使うアクション
actions/checkout
- uses: actions/checkout@v4
with:
fetch-depth: 0 # 全履歴を取得(デフォルトは1)
actions/setup-node
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm' # npm, yarn, pnpm から選択
actions/cache
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
actions/upload-artifact / download-artifact
# ビルド成果物をアップロード
- uses: actions/upload-artifact@v4
with:
name: build
path: dist/
# 別のジョブでダウンロード
- uses: actions/download-artifact@v4
with:
name: build
マトリクスビルド
複数のNode.jsバージョンとOSでテストを実行:
jobs:
test:
strategy:
matrix:
node-version: [18, 20, 22]
os: [ubuntu-latest, macos-latest]
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
ジョブ間の依存関係
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
build:
needs: test # test が成功した後に実行
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
deploy:
needs: build # build が成功した後に実行
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/download-artifact@v4
with:
name: dist
- run: echo "デプロイ処理..."
環境変数とシークレット
環境変数
env:
# ワークフロー全体で使える
NODE_ENV: test
jobs:
test:
env:
# ジョブ内で使える
DATABASE_URL: postgresql://localhost:5432/test
steps:
- run: echo $NODE_ENV
- run: echo $DATABASE_URL
シークレット
GitHubのリポジトリ設定 → Secrets and variables → Actions で登録:
steps:
- name: Deploy
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
run: |
curl -H "Authorization: Bearer $DEPLOY_TOKEN" \
https://api.example.com/deploy
実践的なワークフロー例
PR にリントとテスト結果を表示
name: PR Check
on:
pull_request:
permissions:
contents: read
pull-requests: write
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- name: Lint
run: npm run lint
- name: Test with coverage
run: npm test -- --coverage
- name: Report coverage
if: always()
uses: davelosert/vitest-coverage-report-action@v2
Cloudflare Pages へのデプロイ
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npm run build
- name: Deploy to Cloudflare Pages
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy dist --project-name=my-project
リリースの自動化
name: Release
on:
push:
tags:
- 'v*'
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npm run build
- name: Create Release
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
files: |
dist/*.zip
料金体系
| プラン | パブリックリポジトリ | プライベートリポジトリ |
|---|---|---|
| Free | 無制限 | 2,000分/月 |
| Pro | 無制限 | 3,000分/月 |
| Team | 無制限 | 3,000分/月 |
| Enterprise | 無制限 | 50,000分/月 |
macOS ランナーは Linux の10倍、Windows は2倍の時間としてカウントされます。
デバッグのコツ
ステップのデバッグ出力を有効にする
リポジトリのシークレットに ACTIONS_STEP_DEBUG を true で設定すると、詳細なログが出力されます。
act でローカル実行
# act をインストール
brew install act
# ローカルでワークフローを実行
act push
# 特定のジョブだけ実行
act -j test
ベストプラクティス
- アクションのバージョンを固定する:
uses: actions/checkout@v4のようにメジャーバージョンで固定 - キャッシュを活用する:
setup-nodeのcacheオプションなどでビルド時間を短縮 - 必要最小限の権限を設定する:
permissionsで最小権限を明記 - シークレットは直接参照しない: 環境変数経由で渡す
- タイムアウトを設定する:
timeout-minutesで暴走を防止
まとめ
GitHub Actions は GitHub に統合された CI/CD ツールとして、設定のシンプルさと豊富な機能を両立しています。YAMLファイルをリポジトリに追加するだけで始められるので、まずは自動テストから導入してみましょう。
スケジュール実行で使うcron式の作成には、Assistyのcron式ビルダーが便利です。GUIで直感的にcron式を作成でき、次回実行日時も確認できます。