Skip to content

将你的 Astro 网站部署到 Google Cloud

谷歌云 是一个功能齐全的 Web 应用托管平台,可用于部署 Astro 网站。

¥Google Cloud is a full-featured web app hosting platform that can be used to deploy an Astro site.

¥How to deploy

¥Cloud Storage (static only)

  1. Create a new GCP project, or select one you already have.

  2. Create a new bucket under Cloud Storage.

  3. Give it a name and the other required settings.

  4. Upload your dist folder into it or upload using Cloud Build.

  5. Enable public access by adding a new permission to allUsers called Storage Object Viewer.

  6. Edit the website configuration and add ìndex.html as the entrypoint and 404.html as the error page.

¥Cloud Run (SSR and static)

Cloud Run 是一个无服务器平台,允许你运行容器而无需管理任何基础设施。它可用于部署静态站点和 SSR 站点。

¥Cloud Run is a serverless platform that allows you to run a container without having to manage any infrastructure. It can be used to deploy both static and SSR sites.

¥Prepare the Service

  1. Create a new GCP project, or select one you already have.

  2. Make sure the Cloud Run API is enabled.

  3. Create a new service.

创建 Dockerfile 并构建容器

Section titled 创建 Dockerfile 并构建容器

¥Create Dockerfile & Build the Container

在将 Astro 站点部署到 Cloud Run 之前,你需要创建一个用于构建容器的 Dockerfile。在我们的秘诀部分查找有关 如何将 Docker 与 Astro 结合使用 的更多信息。

¥Before you can deploy your Astro site to Cloud Run, you need to create a Dockerfile that will be used to build the container. Find more information about how to use Docker with Astro in our recipe section.

创建 Dockerfile 后,将其构建为映像并将其推送到 Google Cloud。有几种方法可以实现此目的:

¥Once the Dockerfile is created, build it into an image and push it to Google Cloud. There are a few ways to accomplish this:

使用 Docker 本地构建:

¥Build locally using Docker:

使用 docker build 命令构建映像,使用 docker tag 命令为其添加标签,然后使用 docker push 命令将其推送到注册表。对于 Google Cloud,Artifact Registry 是最简单的选项,但你也可以使用 Docker 中心

¥Use the docker build command to build the image, docker tag to give it a tag, then docker push to push it to a registry. In the case of Google Cloud, Artifact Registry is the easiest option, but you can also use Docker Hub.

Terminal window
# build your container
docker build .
docker tag SOURCE_IMAGE HOSTNAME/PROJECT-ID/TARGET-IMAGE:TAG
# Push your image to a registry
docker push HOSTNAME/PROJECT-ID/IMAGE:TAG

更改上述命令中的以下值以匹配你的项目:

¥Change the following values in the commands above to match your project:

  • SOURCE_IMAGE:本地镜像名称或镜像 ID。

  • HOSTNAME:注册表主机(gcr.ioeu.gcr.ioasia.gcr.ious.gcr.io)。

  • PROJECT:你的 Google Cloud 项目 ID。

  • TARGET-IMAGE:图片存储在注册表中时的名称。

  • TAG 是与图片关联的版本。

请参阅 Google Cloud 文档了解更多信息。

¥Read more in the Google Cloud docs.

使用其他工具:

¥Using another tool:

你可以使用支持 Docker 的 CI/CD 工具,例如 GitHub Actions

¥You can use a CI/CD tool that supports Docker, like GitHub Actions.

使用 云构建 构建:

¥Build using Cloud Build:

你可以指示 Google Cloud 远程构建映像,而不是在本地构建 Dockerfile。参见 Google Cloud Build 文档在这里

¥Instead of building the Dockerfile locally, you can instruct Google Cloud to build the image remotely. See the Google Cloud Build documentation here.

¥Deploying the container

部署可以在终端 使用 gcloud 中手动处理,也可以使用 云构建 或任何其他 CI/CD 系统自动处理。

¥Deployment can be handled manually in your terminal using gcloud or automatically using Cloud Build or any other CI/CD system.

More Deployment Guides

Astro 中文网 - 粤ICP备13048890号