CI(Continuous Integration)は、コードの品質を維持し、バグを早期に発見するための自動化プロセスです。
name: CI
on:
pull_request:
branches: [develop, main]
push:
branches: [develop, main]
jobs:
lint-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run linter
run: pnpm run lint
- name: Run type check
run: pnpm run type-check
- name: Run tests
run: pnpm run test
- name: Build
run: pnpm run build
name: Test Coverage
on:
pull_request:
branches: [develop, main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run tests with coverage
run: pnpm run test:coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
CIの実行時間が長すぎると、開発速度が低下します。以下の最適化を検討してください。
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# ... Lintの実行
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# ... テストの実行
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# ... ビルドの実行
CD(Continuous Delivery/Deployment)は、アプリケーションを自動的にデプロイするプロセスです。
name: Deploy to Staging
on:
push:
branches: [develop]
jobs:
deploy:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
run: pnpm run build
env:
NODE_ENV: production
- name: Deploy to Staging
env:
DEPLOY_TOKEN: ${{ secrets.STAGING_DEPLOY_TOKEN }}
run: |
# デプロイコマンド(Vercel、Netlifyなど)
pnpm run deploy:staging
name: Deploy to Production
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: production
# GitHub Environmentで承認者を設定可能
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
run: pnpm run build
env:
NODE_ENV: production
- name: Deploy to Production
env:
DEPLOY_TOKEN: ${{ secrets.PRODUCTION_DEPLOY_TOKEN }}
run: |
pnpm run deploy:production
- name: Notify deployment
if: success()
run: |
# デプロイ成功の通知(Slack、Discordなど)
echo "Deployment successful"
デプロイ後に問題が発生した場合、迅速にロールバックできる体制を整えます。
# 問題のあるコミットを打ち消すコミットを作成
git revert <commit-hash>
git push origin main
# 自動的に前のバージョンへデプロイ
ロールバック手順をテストし、実際に機能することを確認します。
name: Rollback Test (Manual)
on:
workflow_dispatch:
inputs:
environment:
description: "Environment to rollback"
required: true
type: choice
options:
- staging
- production
jobs:
rollback:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2 # 直前のコミットも取得
- name: Rollback to previous commit
run: |
git revert HEAD --no-edit
git push origin main