如何在 Google Cloud 运行 与 Firebase 托管配对后正确部署更改
How to correctly deploy changes after pairing Google Cloud Run with Firebase Hosting
我刚刚 docker 化了我的 Angular 通用应用程序并使用创建的 docker 图像创建了一个 Google 云 运行 服务来呈现 HTML 在服务器上。此外,由于这是一个已经在使用 Firebase 托管的 Firebase 项目,我将 Cloud 运行 与 Firebase 托管配对(参见参考文献 https://firebase.google.com/docs/hosting/cloud-run?hl=en)。
1) 在根中创建Dockerfile
:
Dockerfile
:
FROM node:14
WORKDIR usr/src/app
COPY package*.json ./
RUN npm install
# Copy local angular/nest code to the container
COPY . .
# Build development app
RUN npm run build:ssr
CMD ["npm", "run", "serve:ssr"]
2) 将 docker 容器添加到 Google Container Registry
gcloud builds submit --tag gcr.io/PROJECT_ID/CONTAINER_NAME --timeout=1200
3) 使用新容器创建云 运行 服务
gcloud beta run deploy --image gcr.io/PROJECT_ID/CONTAINER_NAME
4) 更新 firebase.json
以将托管与云配对 运行
firebase.json
:
"hosting": {
// ...
// Add the "rewrites" attribute within "hosting"
"rewrites": [ {
"source": "**",
"run": {
"serviceId": "SERVICE_NAME", // "service name" (from when you deployed the container image)
"region": "europe-west3" // optional (if omitted, default is us-central1)
}
} ]
}
按照本指南,我设法使其按预期工作。但是,现在我对将来如何正确部署代码更改感到困惑。在 Cloud 运行 之前,我只是 运行 firebase deploy --only hosting
将更改推送到静态服务器上。我是 docker 和容器的新手,但现在看来我必须将每个代码更改部署到 Firebase Hosting 以及 Cloud 运行(并且可能还需要为之前的每个小更改创建一个新容器?)。
您的源现在在容器中。如果您需要重新部署源,请重新打包容器并再次部署。
但是,您也可以采取不同的方式。如果您有静态资源,您可以直接将它们托管在 Firebase 托管上。建议仅在 Cloud 运行 上托管后端。您可以使用 source
参数并仅将后端 URL 路由到云 运行 容器。 (不要忘记设置 CORS,因为 Cloud 运行 URL 和 Firebase URL 不一样!)
我刚刚 docker 化了我的 Angular 通用应用程序并使用创建的 docker 图像创建了一个 Google 云 运行 服务来呈现 HTML 在服务器上。此外,由于这是一个已经在使用 Firebase 托管的 Firebase 项目,我将 Cloud 运行 与 Firebase 托管配对(参见参考文献 https://firebase.google.com/docs/hosting/cloud-run?hl=en)。
1) 在根中创建Dockerfile
:
Dockerfile
:
FROM node:14
WORKDIR usr/src/app
COPY package*.json ./
RUN npm install
# Copy local angular/nest code to the container
COPY . .
# Build development app
RUN npm run build:ssr
CMD ["npm", "run", "serve:ssr"]
2) 将 docker 容器添加到 Google Container Registry
gcloud builds submit --tag gcr.io/PROJECT_ID/CONTAINER_NAME --timeout=1200
3) 使用新容器创建云 运行 服务
gcloud beta run deploy --image gcr.io/PROJECT_ID/CONTAINER_NAME
4) 更新 firebase.json
以将托管与云配对 运行
firebase.json
:
"hosting": {
// ...
// Add the "rewrites" attribute within "hosting"
"rewrites": [ {
"source": "**",
"run": {
"serviceId": "SERVICE_NAME", // "service name" (from when you deployed the container image)
"region": "europe-west3" // optional (if omitted, default is us-central1)
}
} ]
}
按照本指南,我设法使其按预期工作。但是,现在我对将来如何正确部署代码更改感到困惑。在 Cloud 运行 之前,我只是 运行 firebase deploy --only hosting
将更改推送到静态服务器上。我是 docker 和容器的新手,但现在看来我必须将每个代码更改部署到 Firebase Hosting 以及 Cloud 运行(并且可能还需要为之前的每个小更改创建一个新容器?)。
您的源现在在容器中。如果您需要重新部署源,请重新打包容器并再次部署。
但是,您也可以采取不同的方式。如果您有静态资源,您可以直接将它们托管在 Firebase 托管上。建议仅在 Cloud 运行 上托管后端。您可以使用 source
参数并仅将后端 URL 路由到云 运行 容器。 (不要忘记设置 CORS,因为 Cloud 运行 URL 和 Firebase URL 不一样!)