Google Cloud Build - firebase 部署错误:“公共目录‘dist/browser’不存在,无法将托管部署到站点 PROJECT-ID”

2024-05-23

我正在尝试将我的 Angular Universal Web 应用程序同时部署到 Cloud Run(用于 SSR)和 Firebase Hosting(用于缓存)。 Cloud Run 的部署工作正常,我遵循了本指南(https://cloud.google.com/build/docs/deploying-builds/deploy-firebase#using_the_firebase_community_builder https://cloud.google.com/build/docs/deploying-builds/deploy-firebase#using_the_firebase_community_builder)以通过 Google Cloud Build 启用 Firebase 部署。

然而,每当我跑步时gcloud builds submit --config=cloudbuild.yaml错误:public directory 'dist/browser' does not exist, can't deploy hosting to site PROJECT-ID在 Firebase 部署步骤中抛出cloudbuild.yaml.

cloudbuild.yaml:

steps:
  # build image
  - name: 'gcr.io/cloud-builders/docker'
    args: [ 'build', '-t', 'gcr.io/PROJECT-ID/SERVICE-ID', '.' ]
  # push image
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/PROJECT-ID/SERVICE-ID']
  # deploy to Cloud Run
  - name: "gcr.io/cloud-builders/gcloud"
    args: [ "run", "deploy", "SERVICE-ID", "--image", "gcr.io/PROJECT-ID/SERVICE-ID", "--region", "us-central1", "--platform", "managed", "--allow-unauthenticated" ]
  # deploy to Firebase (ERROR HAPPENS HERE Specified public directory 'xx' does not exist, can't deploy hosting to site PROJECT-ID)
  - name: gcr.io/PROJECT-ID/firebase
    args: ['deploy', '--project=PROJECT-ID', '--only=hosting']
images:
  - 'gcr.io/PROJECT-ID/SERVICE-ID'
timeout: 1800s
substitutions:
  _ENV_VARIABLE: development

Dockerfile

FROM node:14

# set work directory
WORKDIR usr/src/app

# copy package.json and package-lock
COPY package*.json ./

# install dependencies
RUN npm install

# copy local code to container
COPY . .

# build app
RUN npm run build:ssr

# serve app
CMD ["npm", "run", "serve:ssr"]

firebase.json

{
...
  "hosting": [
    {
      "public": "dist/browser",
      "ignore": [
        "**/.*"
      ],
      ...
    }
  ]
...
}

error-log

DEBUG: https://storage.googleapis.com:443 "GET /987290120943.cloudbuild-logs.googleusercontent.com/log-93082fbf-1b87-499a-9132-fed5f1c06aad.txt HTTP/1.1" 206 290
DEBUG: Reading GCS logfile: 206 (read 290 bytes)
Step #3:
Step #3: ←[1m←[37m===←[39m Deploying to 'PROJECT-ID'...←[22m
Step #3:
Step #3: ←[1m←[36mi ←[39m←[22m deploying ←[1mhosting←[22m
Step #3:
Step #3: ←[1m←[31mError:←[39m←[22m Specified public directory 'dist/browser' does not exist, can't deploy hosting to site PROJECT-ID
DEBUG: https://cloudbuild.googleapis.com:443 "GET /v1/projects/PROJECT-ID/locations/global/builds/93082fbf-1b87-499a-9132-fed5f1c06aad?alt=json HTTP/1.1" 200 None
DEBUG: https://storage.googleapis.com:443 "GET /987290120943.cloudbuild-logs.googleusercontent.com/log-93082fbf-1b87-499a-9132-fed5f1c06aad.txt HTTP/1.1" 206 120
DEBUG: Reading GCS logfile: 206 (read 120 bytes)
Finished Step #3
ERROR
ERROR: build step 3 "gcr.io/PROJECT-ID/firebase" failed: step exited with non-zero status: 1
DEBUG: https://storage.googleapis.com:443 "GET /987290120943.cloudbuild-logs.googleusercontent.com/log-93082fbf-1b87-499a-9132-fed5f1c06aad.txt HTTP/1.1" 416 168
DEBUG: Reading GCS logfile: 416 (no new content; keep polling)

由于本地一切正常,我认为它可能与gcr.io/PROJECT-ID/firebase与服务位于不同的容器中gcr.io/PROJECT-ID/SERVICE-ID那里也许是dist文件夹已创建。

Edit #1:

我测试了在项目的根目录中创建一个名为“static”的文件夹,并更改了public财产在firebase.json到“静态”。它起作用了,这让我相信出于某种原因dist/browser在“部署到 Firebase”步骤中,目录不存在cloudbuild.yaml即使它是在期间创建的RUN npm run build:ssr of the Dockerfile.

Edit #2:

我发现有些东西不太足够(https://stackoverflow.com/a/64215324/8581106 https://stackoverflow.com/a/64215324/8581106)关于类似的问题。但是,建议似乎还运行构建命令npm run build:ssr在 Firebase 部署之前cloudbuild.yaml以确保dist/browser目录存在。但我真的很想避免在 Cloud Run 上提供一个版本并将新版本部署到 Firebase,因为 Angular 使用独特的哈希值.js and .css每次都会文件,我觉得这可能会导致缓存问题。

Edit #3:

我创建了一个图像来更好地可视化我的问题(见下图)。我是 docker 和 Cloud Build 的新手,但我觉得这就是我运行时的过程gcloud builds submit --config=cloudbuild.yaml。我不明白为什么dist/browser存在于Dockerfile但不在范围内cloudbuild.yaml scope.


根据https://stackoverflow.com/a/66714846/8581106 https://stackoverflow.com/a/66714846/8581106,我无法访问dist/browser目录内的cloudbuild.yaml,因为该目录是通过以下方式在不同的上下文中创建的Dockerfile.

目前,我找到的唯一解决方案是将构建部署到 Cloud Run 后立即将空文件夹部署到 Firebase Hosting。据我了解,用户现在向 Firebase 托管发出请求,并获取响应(如果响应已缓存在 Firebase 托管服务器上)。如果请求的数据没有被缓存rewrites in the firebase.json允许从 Cloud Run 获取渲染数据,并在可能的情况下将其缓存。

虽然它似乎有效,但我不知道 Cloud Run 中的文件是否正确缓存在 Firebase Hosting 上,此外,由于缺少对文本、图像和文件的压缩,Lighthouse 在性能方面的得分要差得多。之前由 Firebase Hosting 自动管理。尽管如此,目前这仍然是最可行的解决方案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Cloud Build - firebase 部署错误:“公共目录‘dist/browser’不存在,无法将托管部署到站点 PROJECT-ID” 的相关文章

随机推荐