コンテンツにスキップ

第7章 GitHub Actions(CI/CD)

GitHub Actions は、GitHub に組み込まれた CI/CD プラットフォームです。
コードの変更をトリガーに、テスト・ビルド・デプロイを 自動化 できます。

管理者として、チームの開発フローを支える ワークフローの設計と運用 を学びましょう ⚡


flowchart TD
    WF["📄 Workflow
(.github/workflows/ci.yml)"] WF --> J1["Job: test"] WF --> J2["Job: build"] WF --> J3["Job: deploy"] J1 --> S1["Step 1: Checkout"] J1 --> S2["Step 2: Setup Node"] J1 --> S3["Step 3: Install deps"] J1 --> S4["Step 4: Run tests"] J1 -- "成功したら" --> J2 J2 -- "成功したら" --> J3 style WF fill:#F3E5F5,stroke:#8E24AA,color:#4A148C style J1 fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1 style J2 fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1 style J3 fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1
概念説明
Workflow.github/workflows/ 配下の YAML ファイル。
1ファイル = 1ワークフロー
Eventワークフローを起動するトリガー(push, PR, schedule など)
Jobワークフロー内の実行単位。
デフォルトで並列実行
StepJob 内の各ステップ。
Action を使うか、シェルコマンドを実行
Action再利用可能な処理ユニット(公式 or コミュニティ製)
Runnerジョブを実行するサーバー(GitHub-hosted or Self-hosted)
.github/workflows/ci.yml
name: CI
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
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

7.2 トリガー(on)の種類と設計

Section titled “7.2 トリガー(on)の種類と設計”
トリガー発火タイミング主な用途
pushブランチへの push 時CI テスト、デプロイ
pull_requestPR の作成・更新時コードレビュー前のチェック
schedulecron 式でスケジュール実行定期的なバッチ処理
workflow_dispatch手動実行(ボタン)デプロイ、メンテナンス
releaseリリースの作成時パッケージ公開
workflow_call他のワークフローから呼び出し再利用ワークフロー
on:
# ブランチとパスでフィルタリング
push:
branches: [main]
paths:
- "src/**"
- "package.json"
paths-ignore:
- "**.md"
- "docs/**"
# PR イベントの種類を指定
pull_request:
types: [opened, synchronize, reopened]
branches: [main, develop]
# 毎日午前3時(UTC)に実行
schedule:
- cron: "0 3 * * *"
# 手動実行(入力パラメータ付き)
workflow_dispatch:
inputs:
environment:
description: "デプロイ先の環境"
required: true
type: choice
options:
- staging
- production
flowchart LR
    P["push to main"] --> CI["CI ワークフロー"]
    PR["PR 作成/更新"] --> CI
    S["毎日3時 (UTC)"] --> BATCH["バッチワークフロー"]
    M["手動ボタン"] --> DEPLOY["デプロイワークフロー"]
    R["リリース作成"] --> PUB["パッケージ公開"]

    style CI fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1
    style BATCH fill:#FFF3E0,stroke:#FB8C00,color:#E65100
    style DEPLOY fill:#E8F5E9,stroke:#43A047,color:#1B5E20
    style PUB fill:#F3E5F5,stroke:#8E24AA,color:#4A148C

API キー、トークンなど 絶対に公開してはいけない値 を安全に保存します。

flowchart TD
    subgraph "Secrets の設定場所"
        RS["Repository Secrets
1リポジトリで使用"] ES["Environment Secrets
特定環境でのみ使用"] OS["Organization Secrets
複数リポジトリで共有"] end style RS fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1 style ES fill:#E8F5E9,stroke:#43A047,color:#1B5E20 style OS fill:#F3E5F5,stroke:#8E24AA,color:#4A148C
# ワークフロー内での使用
steps:
- name: Deploy
env:
API_KEY: ${{ secrets.API_KEY }}
run: ./deploy.sh

機密ではない設定値を保存します。
ログにそのまま出力されます。

env:
APP_NAME: ${{ vars.APP_NAME }}
NODE_ENV: ${{ vars.NODE_ENV }}

承認フロー環境固有の Secrets を管理するための仕組みです。

jobs:
deploy-staging:
environment: staging
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to staging"
deploy-production:
environment: production # 承認が必要な環境
needs: deploy-staging
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to production"

各ワークフロー実行時に 自動発行される一時的なトークン です。
明示的に作成する必要はなく、リポジトリに対する操作に使えます。

# ワークフロー全体のデフォルト権限を最小に
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm test
release:
runs-on: ubuntu-latest
# このジョブだけ書き込み権限を付与
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- run: gh release create v1.0.0
権限説明
contentsリポジトリの読み書き
pull-requestsPR の作成・コメント
issuesIssue の操作
packagesパッケージの読み書き
deploymentsデプロイメントの管理
id-tokenOIDC トークンの取得
actionsワークフロー実行の管理

7.5 Reusable Workflows と Composite Actions

Section titled “7.5 Reusable Workflows と Composite Actions”

複数のリポジトリで同じワークフローを 呼び出し で再利用できます。

.github/workflows/reusable-test.yml(呼ばれる側)
name: Reusable Test
on:
workflow_call:
inputs:
node-version:
required: false
type: string
default: "20"
secrets:
NPM_TOKEN:
required: false
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- run: npm ci
- run: npm test
.github/workflows/ci.yml(呼ぶ側)
name: CI
on:
pull_request:
jobs:
test:
uses: your-org/shared-workflows/.github/workflows/reusable-test.yml@main
with:
node-version: "20"
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

複数のステップを 1つの Action にまとめて再利用できます。

.github/actions/setup-project/action.yml
name: Setup Project
description: プロジェクトのセットアップ
inputs:
node-version:
description: Node.js のバージョン
default: "20"
runs:
using: "composite"
steps:
- uses: actions/checkout@v4
shell: bash
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: "npm"
- run: npm ci
shell: bash
使用例
steps:
- uses: ./.github/actions/setup-project
with:
node-version: "20"
- run: npm test
項目Reusable WorkflowsComposite Actions
粒度ワークフロー全体複数ステップ
呼び出し方jobs.*.usessteps.*.uses
ジョブ定義含められる含められない
共有範囲クロスリポジトリ同リポジトリ or 公開

場面理由
社内ネットワークへのアクセスが必要GitHub-hosted は外部ネットワーク
特殊なハードウェア(GPU など)GitHub-hosted では提供されない
実行時間の制限を超えるGitHub-hosted は6時間まで
コストを最適化したい大量実行時に自前のほうが安い場合
flowchart LR
    A["① Settings → Actions
→ Runners"] --> B["② New self-hosted
runner"] B --> C["③ OS を選択
スクリプトを実行"] C --> D["④ ラベルを設定"] D --> E["⑤ ワークフローで
runs-on 指定"] style A fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1 style E fill:#E8F5E9,stroke:#43A047,color:#1B5E20
jobs:
build:
runs-on: [self-hosted, linux, gpu] # ラベルで指定
steps:
- uses: actions/checkout@v4
- run: ./train-model.sh

# ❌ タグ指定(作者が上書き可能)
- uses: actions/checkout@v4
# ✅ コミットSHA指定(改ざん不可)
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

Organization 設定で、使用可能な Actions を制限できます。

設定説明
Allow all actionsすべて許可
Allow local actions only同一リポジトリの Actions のみ
Allow select actions指定した Actions のみ(推奨)

.github/workflows/ci.yml
name: CI
on:
pull_request:
branches: [main, develop]
permissions:
contents: read
jobs:
lint:
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 run lint
test:
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: "20", cache: "npm" }
- run: npm ci
- run: npm test -- --coverage
.github/workflows/release.yml
name: Release
on:
push:
tags: ["v*"]
permissions:
contents: write
jobs:
release:
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 run build
- name: Create GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release create ${{ github.ref_name }} --generate-notes

機能管理者として押さえるポイント
ワークフロー構造.github/workflows/ に YAML で定義
トリガーイベントに応じた適切な発火条件を設計
Secrets / Environments機密情報は Secrets、環境ごとに分離
Permissions最小権限の原則を徹底
Reusable / Composite共通処理を再利用して保守性向上
Self-hosted Runner必要な場合のみ、セキュリティに注意
セキュリティSHA ピン留め、許可リスト