Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/plugin-dev/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ CONTAINER_SCALE=1
MY_SECRET_API_KEY=sk-xxxxxxxxxxxx
```

### 2.5 ライフサイクルフック(任意)

プロジェクトディレクトリ直下に以下の実行可能ファイルを置くと、`devbase up` のライフサイクルに合わせて自動的に呼び出されます。どちらも実行できなくても問題ない場合は配置不要です。

| ファイル | 実行タイミング | 主な用途 |
|---------|---------------|---------|
| `pre-up` | `devbase up` 開始直後(`docker compose up` の前) | `build.context` 用ソースリポジトリの clone、設定ファイルの生成など、イメージビルド前に完了させたい準備 |
| `deploy` | コンテナ起動完了後、各スケールインスタンスごとに実行 | S3 からの `.env` 取得、コンテナ起動後に必要な外部リソースの初期化など |

```bash
#!/bin/bash
# projects/my-project/pre-up
set -e

# env から GIT_USER / GIT_REPO を取得
source ./env

# build context に使うリポジトリが無ければ clone
if [ ! -d "./repo" ]; then
git clone "https://github.com/${GIT_USER}/${GIT_REPO}.git" repo
fi
```

> **Note:** どちらのフックも `bash` で実行されます。`chmod +x` で実行可能ビットを立てておいてください。`pre-up` が非ゼロ終了すると `devbase up` は中断します。`deploy` は各インスタンスに対して `DEVBASE_INSTANCE_INDEX` を環境変数として渡しますが、失敗してもデプロイは続行されます。

---

## 3. ローカルでの開発・テスト
Expand Down
27 changes: 27 additions & 0 deletions lib/devbase/commands/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ def _run_deploy_script_for_instances(deploy_script: Path, indices) -> None:
logger.warning("Deploy script failed for instance %d (exit code %d)", i, e.returncode)


def _run_pre_up_hook() -> bool:
"""`./pre-up` フックがあればコンテナ起動前に実行する。

ビルドコンテキスト用のリポジトリ clone など、`docker compose up` より前に
完了しておく必要のある準備処理をプロジェクト側で記述するためのフック。

Returns:
True: フックが存在しなかった、または成功した
False: フックが失敗した(呼び出し側で `cmd_up` を中断する)
"""
pre_up_script = Path('./pre-up')
if not (pre_up_script.exists() and pre_up_script.is_file()):
return True

logger.info("Running pre-up hook: %s", pre_up_script)
try:
subprocess.run(['bash', str(pre_up_script)], check=True, env=os.environ.copy())
return True
except subprocess.CalledProcessError as e:
logger.error("pre-up hook failed (exit code %d)", e.returncode)
return False


# ---------------------------------------------------------------------------
# ディスパッチャ
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -105,6 +128,10 @@ def cmd_up(project_name: str = None, scale: int = None) -> int:
logger.error("Failed to create .env file. Please run 'devbase env init' manually.")
return 1

# Pre-step: Run ./pre-up hook (e.g. clone source repos used as build contexts)
if not _run_pre_up_hook():
return 1

# Pre-check 2: Ensure container images exist
if not _ensure_images():
logger.error(
Expand Down
Loading