Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

ブランチごとにビルドコマンドを設定する

最終更新 Markdown で表示Agent セットアップ

このガイドでは、特定のブランチにビルドコマンドを設定する方法を説明します。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

変更を公開する

変更を反映するには、次の手順を実行します。

  1. Cloudflare ダッシュボードで Workers & Pages ページを開きます。

    Workers & Pages を開く ↗
  2. Pages プロジェクトを選びます。

  3. Settings > Build & deployments > Build configurations > Edit configurations を開きます。

  4. Build command の値を bash build.sh に更新し、Save を選びます。

ビルドが成功するか確認するには、プロジェクトをデプロイします。

役に立ちましたか?