跳转到内容
搜索文档

为各分支设置构建命令

最后更新 查看 MarkdownAgent 设置

本指南将指导你如何在特定分支上设置构建命令。你将使用 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 页面。

    Go to Workers & Pages ↗
  2. 选择你的 Pages 项目。

  3. 前往 Settings(设置) > Build & deployments > Build configurations > Edit configurations

  4. Build command(构建命令) 字段值更新为 bash build.sh,然后选择 Save(保存)

要测试构建是否成功,请部署项目。

这篇文档对您有帮助吗?