From cc62d128021ed393f190560297ffaa4a2c259290 Mon Sep 17 00:00:00 2001 From: yyamashita Date: Sun, 17 May 2026 11:09:01 +0900 Subject: Manage all server-side git hooks in this repository Add server-hooks//post-receive for all repos and scripts/install-hooks.sh to deploy them manually on the server. Hooks are not auto-installed on git push for security. Co-Authored-By: Claude Sonnet 4.6 --- Deploy.md | 23 +++++++++++++-- scripts/install-hooks.sh | 36 ++++++++++++++++++++++++ server-hooks/hetzner-infra/post-receive | 14 +++++++++ server-hooks/tokyo-livehouse-events/post-receive | 10 +++++++ server-hooks/whois-band/post-receive | 8 ++++++ 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100755 scripts/install-hooks.sh create mode 100755 server-hooks/hetzner-infra/post-receive create mode 100755 server-hooks/tokyo-livehouse-events/post-receive create mode 100755 server-hooks/whois-band/post-receive diff --git a/Deploy.md b/Deploy.md index 3c6e89b..a03b2a3 100644 --- a/Deploy.md +++ b/Deploy.md @@ -52,13 +52,30 @@ 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' +``` + +| ファイル | 役割 | +|---|---| +| `server-hooks//post-receive` | 各リポジトリのフック内容 | +| `scripts/install-hooks.sh` | `server-hooks/` を `/var/git/*/hooks/` に展開 | + ## 新アプリ追加手順 -1. `Caddyfile` にホスト追加 → `git push hetzner master` +1. `Caddyfile` にホスト追加 → `git push origin master` 2. 新アプリ側: `docker-compose.yml` で `external: true` の `web` network を使用、`container_name` を設定 3. 新アプリの bare repo をサーバに作成(`scripts/server-setup.sh` 参照) -4. `git remote add hetzner golive:/var/git/.git` -5. `git push hetzner master` +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` ## Claude Code セッション管理 diff --git a/scripts/install-hooks.sh b/scripts/install-hooks.sh new file mode 100755 index 0000000..c502496 --- /dev/null +++ b/scripts/install-hooks.sh @@ -0,0 +1,36 @@ +#!/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/server-hooks/hetzner-infra/post-receive b/server-hooks/hetzner-infra/post-receive new file mode 100755 index 0000000..cc3fa8e --- /dev/null +++ b/server-hooks/hetzner-infra/post-receive @@ -0,0 +1,14 @@ +#!/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" + +# Claude Code セッション同期 +if [ -f "$APP_DIR/scripts/sync-claude-services.sh" ]; then + bash "$APP_DIR/scripts/sync-claude-services.sh" +fi diff --git a/server-hooks/tokyo-livehouse-events/post-receive b/server-hooks/tokyo-livehouse-events/post-receive new file mode 100755 index 0000000..8d5ef84 --- /dev/null +++ b/server-hooks/tokyo-livehouse-events/post-receive @@ -0,0 +1,10 @@ +#!/bin/bash +set -e +APP_DIR=/app +GIT_DIR=/var/git/tokyo-livehouse-events.git + +git --work-tree=$APP_DIR --git-dir=$GIT_DIR checkout -f +cd $APP_DIR +mkdir -p data +docker compose up -d --build +echo "Deploy complete: tokyo-livehouse-events" diff --git a/server-hooks/whois-band/post-receive b/server-hooks/whois-band/post-receive new file mode 100755 index 0000000..60b368f --- /dev/null +++ b/server-hooks/whois-band/post-receive @@ -0,0 +1,8 @@ +#!/bin/bash +set -e +APP_DIR=/app/whois-band +GIT_WORK_TREE=$APP_DIR git checkout -f +cd $APP_DIR +mkdir -p data +docker compose up -d --build +echo "Deploy complete: whois-band" -- cgit v1.2.3