You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase App Hosting部署Angular应用时Git子模块未下载求助

解决Firebase App Hosting部署Angular应用时Cloud Build未拉取Git子模块的问题

问题背景

你的Angular 18应用使用Git子模块托管在Cloud Source Repositories中,通过Firebase App Hosting部署时,Cloud Build未自动拉取子模块,导致构建因找不到模块引用失败,且已尝试的cloudbuild.yaml和简单prebuild脚本均无效。

可行解决方案

方案一:通过package.json prebuild脚本配置(推荐)

Firebase App Hosting默认使用Buildpacks执行构建,需确保Git能通过项目ServiceAccount认证拉取Cloud Source Repos的子模块,步骤如下:

  1. 修改package.json的scripts
    添加带Git凭证配置的prebuild命令,确保拉取子模块时自动认证:

    {
      "scripts": {
        "prebuild": "git config --global credential.helper gcloud.sh && git submodule update --init --recursive",
        "build": "ng build"
      }
    }
    
    • git config --global credential.helper gcloud.sh:让Git使用gcloud的凭证访问Cloud Source Repositories,自动继承当前Cloud Build Service Account的权限
    • --recursive:确保拉取嵌套子模块(如果有的话)
  2. 配置firebase.json明确构建命令
    确保Firebase触发npm构建流程,从而自动执行prebuild:

    {
      "hosting": {
        "build": {
          "command": "npm run build",
          "output": "dist/[你的Angular应用输出目录]"
        }
      }
    }
    

    替换[你的Angular应用输出目录]为angular.json中projects.[app-name].architect.build.options.outputPath指定的路径。

方案二:使用自定义Cloud Build配置

若需要更灵活的构建流程,可启用自定义Cloud Build配置:

  1. 修改firebase.json启用自定义Cloud Build

    {
      "hosting": {
        "useCloudBuild": true,
        "build": {
          "output": "dist/[你的Angular应用输出目录]"
        }
      }
    }
    
  2. 创建cloudbuild.yaml定义构建步骤
    在项目根目录创建该文件,添加拉取子模块、安装依赖、构建的步骤:

    steps:
      # 拉取Git子模块
      - name: 'gcr.io/cloud-builders/git'
        args: ['submodule', 'update', '--init', '--recursive']
      # 安装依赖
      - name: 'node:18'
        entrypoint: 'npm'
        args: ['install']
      # 执行Angular构建
      - name: 'node:18'
        entrypoint: 'npm'
        args: ['run', 'build']
    # 指定构建产物路径
    artifacts:
      objects:
        location: 'gs://$PROJECT_ID.appspot.com/'
        paths: ['dist/**']
    

    注意:node版本需与项目兼容(如日志中的v18.20.3)。

关键说明

  • 之前prebuild脚本失败的核心原因:未配置Git凭证助手,导致拉取Cloud Source Repos子模块时认证失败,添加credential.helper gcloud.sh后可自动使用Cloud Build的ServiceAccount权限完成认证。
  • 已配置的ServiceAccount权限(firebase-app-hosting-compute@whatever-project.iam.gserviceaccount.com拥有子模块仓库的Cloud Build Service Account角色)无需额外修改,上述方案会自动利用该权限。

内容的提问来源于stack exchange,提问作者maganap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:53:09