このガイドでは、特定のブランチにビルドコマンドを設定する方法を説明します。CF_PAGES_BRANCH 環境変数を使い、本番ブランチ以外の指定したブランチでスクリプトを実行します。Cloudflare アカウントと Pages プロジェクトがあることを前提とします。
プロジェクトディレクトリに .sh ファイルを作成します。ファイル名は自由ですが、build.sh を推奨します。
次のスクリプトでは、CF_PAGES_BRANCH 環境変数で、現在ビルド中のブランチを確認します。.sh ファイルに次を記入します。
# !/bin/bash
if [ "$CF_PAGES_BRANCH" == "production" ]; then
# Run the "production" script in `package.json` on the "production" branch
# "production" should be replaced with the name of your Production branch
npm run production
elif [ "$CF_PAGES_BRANCH" == "staging" ]; then
# Run the "staging" script in `package.json` on the "staging" branch
# "staging" should be replaced with the name of your specific branch
npm run staging
else
# Else run the dev script
npm run dev
fi変更を反映するには、次の手順を実行します。
-
Cloudflare ダッシュボードで Workers & Pages ページを開きます。
Workers & Pages を開く ↗ -
Pages プロジェクトを選びます。
-
Settings > Build & deployments > Build configurations > Edit configurations を開きます。
-
Build command の値を
bash build.shに更新し、Save を選びます。
ビルドが成功するか確認するには、プロジェクトをデプロイします。