From b6efad84b51c6df5f595683d66b855c75bddabc4 Mon Sep 17 00:00:00 2001 From: yyamashita Date: Sun, 17 May 2026 11:18:12 +0900 Subject: Centralize repo and hook management: add repos.txt and install.sh All bare repo creation is now driven by repos.txt in this repo. install.sh replaces install-hooks.sh and handles both repo creation and hook deployment. Other repos' server-setup.sh no longer manage bare repos or hooks. Co-Authored-By: Claude Sonnet 4.6 --- Deploy.md | 33 +++++++++++++------------- repos.txt | 6 +++++ scripts/install-hooks.sh | 36 ---------------------------- scripts/install.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ scripts/server-setup.sh | 42 +++++++++++++++++++++----------- 5 files changed, 113 insertions(+), 66 deletions(-) create mode 100644 repos.txt delete mode 100755 scripts/install-hooks.sh create mode 100755 scripts/install.sh diff --git a/Deploy.md b/Deploy.md index a03b2a3..5faaf67 100644 --- a/Deploy.md +++ b/Deploy.md @@ -52,30 +52,31 @@ ssh golive 'docker logs tokyo-app -f' ssh golive 'docker logs whois-app -f' ``` -## サーバーフック管理 +## サーバー構成管理 -各リポジトリの post-receive フック内容は `server-hooks//post-receive` で管理する。 -git push では**自動展開しない**(セキュリティ上の理由)。フックを更新したい場合は push 後に手動でインストールスクリプトを実行する。 - -```bash -# フックを更新した場合: push → サーバーで手動インストール -git push origin master -ssh root@localhost 'bash /app/infra/scripts/install-hooks.sh' -``` +ベアリポジトリ・ワークツリー・フックはすべてこのリポジトリで一元管理する。 +git push では**自動展開しない**(セキュリティ上の理由)。変更後は手動で `install.sh` を実行する。 | ファイル | 役割 | |---|---| +| `repos.txt` | 全リポジトリの定義(`name:work-tree` 形式) | | `server-hooks//post-receive` | 各リポジトリのフック内容 | -| `scripts/install-hooks.sh` | `server-hooks/` を `/var/git/*/hooks/` に展開 | +| `scripts/install.sh` | ベアリポジトリ作成 + フック展開(要 root) | +| `scripts/server-setup.sh` | 初回のみ: Docker + hetzner-infra ブートストラップ | + +```bash +# 変更を反映する場合 +git push origin master +ssh root@localhost 'bash /app/infra/scripts/install.sh' +``` ## 新アプリ追加手順 -1. `Caddyfile` にホスト追加 → `git push origin master` -2. 新アプリ側: `docker-compose.yml` で `external: true` の `web` network を使用、`container_name` を設定 -3. 新アプリの bare repo をサーバに作成(`scripts/server-setup.sh` 参照) -4. `server-hooks//post-receive` を作成して push -5. `ssh root@localhost 'bash /app/infra/scripts/install-hooks.sh'` -6. `git remote add hetzner golive:/var/git/.git` → `git push hetzner master` +1. `repos.txt` に `:` を追記 +2. `server-hooks//post-receive` を作成 +3. `Caddyfile` にホスト追加 +4. `git push origin master` → `ssh root@localhost 'bash /app/infra/scripts/install.sh'` +5. 新アプリ側: `git remote add hetzner root@localhost:/var/git/.git` → `git push hetzner master` ## Claude Code セッション管理 diff --git a/repos.txt b/repos.txt new file mode 100644 index 0000000..25e764a --- /dev/null +++ b/repos.txt @@ -0,0 +1,6 @@ +# リポジトリ定義 +# 形式: : +# /var/git/.git が bare repo、 が checkout 先 +hetzner-infra:/app/infra +tokyo-livehouse-events:/app +whois-band:/app/whois-band diff --git a/scripts/install-hooks.sh b/scripts/install-hooks.sh deleted file mode 100755 index c502496..0000000 --- a/scripts/install-hooks.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash -# サーバー上で root として手動実行する -# server-hooks/ の内容を /var/git/*/hooks/ に展開する -set -euo pipefail - -APP_DIR="$(cd "$(dirname "$0")/.." && pwd)" -HOOKS_SRC="$APP_DIR/server-hooks" - -if [[ "$(id -u)" -ne 0 ]]; then - echo "ERROR: root として実行してください" >&2 - exit 1 -fi - -for repo_src in "$HOOKS_SRC"/*/; do - repo_name="$(basename "$repo_src")" - git_hooks_dir="/var/git/${repo_name}.git/hooks" - - if [[ ! -d "$git_hooks_dir" ]]; then - echo "SKIP: $git_hooks_dir が存在しません ($repo_name)" - continue - fi - - for hook_file in "$repo_src"*; do - hook_name="$(basename "$hook_file")" - dst="$git_hooks_dir/$hook_name" - if diff -q "$hook_file" "$dst" >/dev/null 2>&1; then - echo " unchanged: $repo_name/$hook_name" - else - cp "$hook_file" "$dst" - chmod +x "$dst" - echo " installed: $repo_name/$hook_name" - fi - done -done - -echo "Done." diff --git a/scripts/install.sh b/scripts/install.sh new file mode 100755 index 0000000..9f06489 --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# サーバー上で root として手動実行する +# repos.txt に基づいてベアリポジトリとワークツリーを作成し、 +# server-hooks/ のフックを /var/git/*/hooks/ に展開する +set -euo pipefail + +APP_DIR="$(cd "$(dirname "$0")/.." && pwd)" +REPOS_FILE="$APP_DIR/repos.txt" +HOOKS_SRC="$APP_DIR/server-hooks" + +if [[ "$(id -u)" -ne 0 ]]; then + echo "ERROR: root として実行してください" >&2 + exit 1 +fi + +# ベアリポジトリ・ワークツリー作成 +echo "=== Repositories ===" +while IFS=: read -r repo_name work_tree; do + [[ "$repo_name" =~ ^#.*$ || -z "$repo_name" ]] && continue + bare_repo="/var/git/${repo_name}.git" + + if [[ ! -d "$bare_repo" ]]; then + echo " create: $bare_repo" + mkdir -p "$bare_repo" + git init --bare "$bare_repo" + else + echo " exists: $bare_repo" + fi + + if [[ ! -d "$work_tree" ]]; then + echo " mkdir: $work_tree" + mkdir -p "$work_tree" + fi +done < "$REPOS_FILE" + +# フック展開 +echo "" +echo "=== Hooks ===" +for repo_src in "$HOOKS_SRC"/*/; do + repo_name="$(basename "$repo_src")" + git_hooks_dir="/var/git/${repo_name}.git/hooks" + + if [[ ! -d "$git_hooks_dir" ]]; then + echo " SKIP: $git_hooks_dir not found ($repo_name)" + continue + fi + + for hook_file in "$repo_src"*; do + hook_name="$(basename "$hook_file")" + dst="$git_hooks_dir/$hook_name" + if diff -q "$hook_file" "$dst" >/dev/null 2>&1; then + echo " unchanged: $repo_name/$hook_name" + else + cp "$hook_file" "$dst" + chmod +x "$dst" + echo " installed: $repo_name/$hook_name" + fi + done +done + +echo "" +echo "Done." diff --git a/scripts/server-setup.sh b/scripts/server-setup.sh index 154c6c7..ca9a3f9 100644 --- a/scripts/server-setup.sh +++ b/scripts/server-setup.sh @@ -1,9 +1,11 @@ #!/bin/bash -# Run once on a fresh Hetzner VPS (as root). Run this first before other app setups. +# Run once on a fresh Hetzner VPS (as root). +# hetzner-infra のみブートストラップとして作成する。 +# 他のリポジトリは最初の push 後に install.sh が作成する。 set -e -APP_DIR=/app/infra REPO_DIR=/var/git/hetzner-infra.git +APP_DIR=/app/infra # Install Docker curl -fsSL https://get.docker.com | sh @@ -11,18 +13,30 @@ curl -fsSL https://get.docker.com | sh # Create shared Docker network docker network create web || true -mkdir -p "$APP_DIR" -mkdir -p "$REPO_DIR" +# hetzner-infra bare repo をブートストラップ +mkdir -p "$REPO_DIR" "$APP_DIR" git init --bare "$REPO_DIR" +# 最初の push を受け取るための最小限のフック(install.sh 実行後に上書きされる) +cat > "$REPO_DIR/hooks/post-receive" << 'HOOK' +#!/bin/bash +set -e +APP_DIR=/app/infra +GIT_WORK_TREE=$APP_DIR git checkout -f +cd $APP_DIR +docker network create web 2>/dev/null || true +docker compose up -d +docker compose exec -T caddy caddy reload --config /etc/caddy/Caddyfile 2>/dev/null || true +echo "Deploy complete: hetzner-infra" +if [ -f "$APP_DIR/scripts/sync-claude-services.sh" ]; then + bash "$APP_DIR/scripts/sync-claude-services.sh" +fi +HOOK +chmod +x "$REPO_DIR/hooks/post-receive" + echo "" -echo "Setup complete. On your local machine, run:" -echo " git remote add hetzner root@:$REPO_DIR" -echo " git push hetzner master" -echo "" -echo "Then install hooks:" -echo " ssh root@ 'bash /app/infra/scripts/install-hooks.sh'" -echo "" -echo "Then run setup scripts for each app:" -echo " tokyo-livehouse-events/scripts/server-setup.sh" -echo " whois-band/scripts/server-setup.sh" +echo "Next steps:" +echo " 1. git remote add origin root@:$REPO_DIR" +echo " 2. git push origin master" +echo " 3. ssh root@ 'bash $APP_DIR/scripts/install.sh'" +echo " (他のリポジトリ作成 + 全フックの正式インストール)" -- cgit v1.2.3