本指南将指导你如何在特定分支上设置构建命令。你将使用 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 页面。
Go to Workers & Pages ↗ -
选择你的 Pages 项目。
-
前往 Settings(设置) > Build & deployments > Build configurations > Edit configurations。
-
将 Build command(构建命令) 字段值更新为
bash build.sh,然后选择 Save(保存)。
要测试构建是否成功,请部署项目。