CI/CD — 継続的インテグレーション/デプロイ

一言で

コードをpushするたびにテスト・Lint・デプロイを自動実行し、常に動く状態を保つ手法。

CI/CD の違い

CI — 継続的インテグレーション
pushのたびに自動で:
• テスト実行
• Lint・型チェック
• ビルド確認
CD — 継続的デプロイ
CIが通ったら自動で:
• ステージング/本番デプロイ
• パッケージ公開(ライブラリ)

Trunk-Based Development

1. mainブランチを常にデプロイ可能に保つ
2. 短命のブランチで作業 → すぐmainにマージ
3. 長期間放置するブランチを作らない

個人開発なら feature ブランチ不要。main 直 push が基本。

いつ使うか

対象
全プロジェクト(最低でもCIは導入推奨)
テストがあるプロジェクト(CIで自動実行)
公開ライブラリ(CDで自動公開)
プロトタイプ・使い捨てスクリプト

CI設定例 — Python pytest + ruff

# .github/workflows/ci.yml
name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11", "3.12"]

    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install pytest ruff mypy

      - name: Lint
        run: ruff check .

      - name: Type check
        run: mypy src/ --ignore-missing-imports

      - name: Test
        run: pytest tests/ -v

CI設定例 — TypeScript vitest + biome

# .github/workflows/ci.yml
name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [20, 22]

    steps:
      - uses: actions/checkout@v4
      - name: Set up Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npx biome check src/

      - name: Type check
        run: npx tsc --noEmit

      - name: Test
        run: npx vitest run

CD設定例 — ライブラリ公開

# .github/workflows/publish.yml
name: Publish

on:
  push:
    tags: ["v*"]  # タグpushで自動公開

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: "https://registry.npmjs.org"
      - run: npm ci
      - run: npm test
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

よくあるつまずき

つまずき解決策
「CIの設定に時間がかかる」最初はテストだけ。Lint・型チェックは後で追加
「CIが遅い」キャッシュを使う(cache: npm等)
「テストがないのにCIを作った」先にテストを書く。TDDを参照
「mainに直接pushしてよいか」個人開発ならOK。Trunk-Basedの原則に従う

組み合わせ

TDD — TDDで書いたテストをCIで自動実行 設計原則 — CIが品質の安全網 仕様駆動開発 — 仕様テストをCIで常時検証
CLAUDE.md用プロンプト:
## 開発手法: CI/CD
- push時にテスト・Lint・型チェックを自動実行(GitHub Actions)
- mainブランチは常にデプロイ可能な状態を保つ
- 個人開発はmain直push運用(Trunk-Based)。featureブランチは放置禁止
- テストツール: Python=pytest+ruff, TypeScript=vitest+biome
- CI設定: .github/workflows/ci.yml
← 開発手法セレクタに戻る ← ガイド一覧に戻る