コードをpushするたびにテスト・Lint・デプロイを自動実行し、常に動く状態を保つ手法。
mainブランチを常にデプロイ可能に保つ個人開発なら feature ブランチ不要。main 直 push が基本。
| 対象 | |
|---|---|
| ✅ | 全プロジェクト(最低でもCIは導入推奨) |
| ✅ | テストがあるプロジェクト(CIで自動実行) |
| ✅ | 公開ライブラリ(CDで自動公開) |
| ❌ | プロトタイプ・使い捨てスクリプト |
# .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
# .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
# .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の原則に従う |
## 開発手法: CI/CD
- push時にテスト・Lint・型チェックを自動実行(GitHub Actions)
- mainブランチは常にデプロイ可能な状態を保つ
- 個人開発はmain直push運用(Trunk-Based)。featureブランチは放置禁止
- テストツール: Python=pytest+ruff, TypeScript=vitest+biome
- CI設定: .github/workflows/ci.yml