部署到 Firebase

本页面介绍如何使用 Cloud Build 将应用部署到 Firebase。如果您是刚接触 Cloud Build,请先阅读快速入门构建配置概览

准备工作

  • 启用 Cloud Build, Firebase, and Resource Manager API。

    启用 API

  • 如需运行此页面中的 gcloud 命令,请安装 Google Cloud CLI

  • 保留您的应用源代码,包括 firebase.json。您的源代码需要存储在 Cloud Source Repositories、GitHub 或 Bitbucket 等代码库中。

  • 如果您还没有要部署到 Firebase 的项目,可以通过安装并初始化 Firebase 来创建默认项目。

必需的 IAM 权限

  1. 在 Google Cloud 控制台中打开 IAM 页面:

    打开 IAM 页面

  2. 选择您的项目,然后点击打开

  3. 在权限表中,找到以 @cloudbuild.gserviceaccount.com 结尾的电子邮件,然后点击铅笔图标。这是 Cloud Build 服务账号

  4. 添加 Cloud Build Service AccountFirebase AdminAPI Keys Admin 角色。

  5. 点击保存

使用 firebase 社区构建器

Cloud Build 提供 Firebase 社区构建器映像,可用于在 Cloud Build 中调用 firebase 命令。要在 Cloud Build 配置文件中使用此构建器,您必须先构建映像并将其推送到项目中的 Container Registry。

要构建并推送 firebase 社区构建器,请执行以下操作:

  1. 导航到您的项目根目录。

  2. 克隆 cloud-builders-community 代码库:

    git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git
    
  3. 导航到 firebase 构建器映像:

    cd cloud-builders-community/firebase
    
  4. 将构建器提交到您的项目,其中 REGION受支持的构建区域之一:

    gcloud builds submit --region=REGION .
    
  5. 导航回您的项目根目录:

    cd ../..
    
  6. 从根目录中移除代码库:

    rm -rf cloud-builders-community/
    

配置部署

构建 firebase 社区构建器后,您可以在构建配置文件中使用 firebase 构建步骤将其部署到 Firebase:

  1. 创建一个名为 cloudbuild.yamlcloudbuild.json 的构建配置文件,其中 project-id 是您的 Google Cloud 项目 ID,firebase-project-id 是您的 Firebase 项目 ID:

    YAML

          steps:
          - name: gcr.io/project-id/firebase
            args: ['deploy', '--project=firebase-project-id', '--only=hosting']
    

    JSON

    {
      "steps": [
       {
          "name": "gcr.io/project-id/firebase",
          "args": [
             "deploy",
             "--project",
             "firebase-project-id",
             "--only",
             "hosting"
           ]
      }
      ]
    }
    
  2. 使用构建配置文件启动构建:

    gcloud builds submit --region=REGION --config config-file-path source-directory
    

    其中:

    • config-file-path 是构建配置文件的路径。
    • source-directory 是源代码的路径或网址。
    • REGION受支持的构建区域之一。

持续部署

通过创建 Cloud Build 触发器,您可以将软件自动部署到 Firebase。您可以将触发器配置为您在更新源代码时构建和部署映像。

要自动部署到 Firebase,请执行以下操作:

  1. 在您的代码库中,添加一个构建配置文件,其中包含调用 firebase deploy 命令的步骤,其中 project-id 是您的 Google Cloud 项目 ID:

    YAML

    steps:
          - name: gcr.io/project-id/firebase
            args: ['deploy', '--project=project-id', '--only=hosting']
    

    JSON

    {
      "steps": [
       {
          "name": "gcr.io/project-id/firebase",
          "args": [
             "deploy",
             "--project",
             "project-id",
             "--only",
             "hosting"
           ]
      }
      ]
    }
    
  2. 使用上一步中创建的构建配置文件创建触发器:

    1. 在 Google Cloud 控制台中打开触发器页面:

      打开“触发器”页面

    2. 从页面顶部的项目选择器下拉菜单中选择您的项目。

    3. 点击打开

    4. 点击创建触发器

      创建触发器页面上,输入以下设置:

      1. 输入触发器的名称。

      2. 选择用于启动触发器的代码库事件。

      3. 选择包含源代码和构建配置文件的代码库。

      4. 指定用于启动触发器的分支名称或标记名称的正则表达式。

      5. 配置:选择您之前创建的构建配置文件。

    5. 点击创建以保存您的构建触发器。

每当您将新代码推送到代码库时,系统都将自动在 Firebase 上启动构建和部署。

如需详细了解如何创建 Cloud Build 触发器,请参阅创建和管理构建触发器

代码示例

要查看使用 Cloud Build 部署到 Firebase 的代码示例,请转到 deploy-firebase-example

后续步骤