From 567feca1a07ad7a47e4d31bd3c5821b1bf1565c4 Mon Sep 17 00:00:00 2001 From: Wada Yusuke Date: Sat, 21 Feb 2026 23:50:35 +0900 Subject: [PATCH] ci create --- .githooks/pre-commit | 10 +++++++ .githooks/pre-push | 13 +++++++++ .github/workflows/ci.yml | 56 ++++++++++++++++++++++++++++++++++++++ .gitignore | 2 ++ README.md | 31 +++++++++++++++++++++ scripts/setup-git-hooks.sh | 10 +++++++ 6 files changed, 122 insertions(+) create mode 100755 .githooks/pre-commit create mode 100755 .githooks/pre-push create mode 100644 .github/workflows/ci.yml create mode 100755 scripts/setup-git-hooks.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 00000000..ed1d7621 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,10 @@ +#!/bin/sh + +branch_name="$(git symbolic-ref --short HEAD 2>/dev/null || true)" + +if [ "$branch_name" = "main" ]; then + echo "Error: main ブランチへの直接コミットは禁止です。feature ブランチを切って作業してください。" + exit 1 +fi + +exit 0 diff --git a/.githooks/pre-push b/.githooks/pre-push new file mode 100755 index 00000000..ce402583 --- /dev/null +++ b/.githooks/pre-push @@ -0,0 +1,13 @@ +#!/bin/sh + +blocked_ref="refs/heads/main" + +while read -r local_ref local_sha remote_ref remote_sha +do + if [ "$remote_ref" = "$blocked_ref" ]; then + echo "Error: main ブランチへの直接 push は禁止です。Pull Request 経由で反映してください。" + exit 1 + fi +done + +exit 0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..03135a0b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,56 @@ +name: CI + +on: + pull_request: + branches: + - main + push: + branches: + - main + +permissions: + contents: read + +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + runs-on: ubuntu-latest + timeout-minutes: 15 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: "20" + cache: npm + cache-dependency-path: frontend/package-lock.json + + - name: Install frontend dependencies + run: npm ci --prefix frontend + + - name: Run frontend tests + run: npm run test --prefix frontend + + - name: Build frontend + run: npm run build --prefix frontend + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + cache: pip + cache-dependency-path: backend/requirements.txt + + - name: Install backend dependencies + run: | + python -m pip install --upgrade pip + pip install -r backend/requirements.txt + + - name: Run backend tests + run: pytest -q backend/tests diff --git a/.gitignore b/.gitignore index fa616cac..1e76fd0f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ venv/ # Node node_modules/ dist/ +frontend/.test-dist/ +frontend/tsconfig.app.tsbuildinfo # Env files .env diff --git a/README.md b/README.md index 81ccc28b..0602247a 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,37 @@ cd backend .venv/bin/python -m pytest -q ``` +## CI (GitHub Actions) +- ワークフロー: `.github/workflows/ci.yml` +- 実行タイミング: + - `pull_request` (target: `main`) + - `push` (`main`) +- 実行内容: + - frontend: `npm run test`, `npm run build` + - backend: `pytest -q backend/tests` +- 低コスト運用の工夫: + - Linuxランナーのみ使用 + - Node/Python依存キャッシュを利用 + - `concurrency` で古い実行を自動キャンセル + +## main ブランチ保護 +### ローカル(ターミナル)での直コミット/直push防止 +```bash +./scripts/setup-git-hooks.sh +``` + +- `.githooks/pre-commit`: `main` への直接コミットを拒否 +- `.githooks/pre-push`: `main` への直接pushを拒否 + +### GitHub 側での強制保護(推奨) +1. GitHub リポジトリの `Settings` -> `Branches` -> `Add branch protection rule` +2. `Branch name pattern` に `main` を設定 +3. 以下を有効化 + - `Require a pull request before merging` + - `Require status checks to pass before merging`(`CI` を選択) + - `Do not allow bypassing the above settings`(利用可能な場合) +4. 保存 + ## メモ - DBテーブルはFastAPI起動時に自動作成されます。 - CORS許可元は `backend/.env` の `CORS_ORIGINS` で調整できます。 diff --git a/scripts/setup-git-hooks.sh b/scripts/setup-git-hooks.sh new file mode 100755 index 00000000..e484d3b9 --- /dev/null +++ b/scripts/setup-git-hooks.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" + +chmod +x "$repo_root/.githooks/pre-commit" "$repo_root/.githooks/pre-push" +git -C "$repo_root" config core.hooksPath .githooks + +echo "Git hooks enabled (core.hooksPath=.githooks)."