跳至主要内容

如何部署到 Google Cloud Run

Google Cloud Run 是一个托管计算平台,可让您在 Google 的可扩展基础设施上运行容器。

本“如何操作”指南将向您展示如何使用 Docker 将您的 Deno 应用程序部署到 Google Cloud Run。

首先,我们将向您展示如何手动部署,然后我们将向您展示如何使用 GitHub Actions 自动化它。

先决条件

手动部署

创建 Dockerfiledocker-compose.yml

为了专注于部署,我们的应用程序将只是一个返回字符串作为 HTTP 响应的 `main.ts` 文件。

import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use((ctx) => {
ctx.response.body = "Hello from Deno and Google Cloud Run!";
});

await app.listen({ port: 8000 });

然后,我们将创建两个文件 - `Dockerfile` 和 `docker-compose.yml` - 来构建 Docker 镜像。

在我们的 `Dockerfile` 中,让我们添加

FROM denoland/deno

EXPOSE 8000

WORKDIR /app

ADD . /app

RUN deno cache main.ts

CMD ["run", "--allow-net", "main.ts"]

然后,在我们的 `docker-compose.yml` 中

version: '3'

services:
web:
build: .
container_name: deno-container
image: deno-image
ports:
- "8000:8000"

让我们通过运行 `docker compose -f docker-compose.yml build`,然后 `docker compose up`,并访问 `localhost:8000` 来在本地测试它。

Hello from localhost

它有效!

设置 Artifact Registry

Artifact Registry 是 GCP 的私有 Docker 镜像注册表。

在我们使用它之前,请访问 GCP 的 Artifact Registry 并点击“创建仓库”。您将被要求提供一个名称(`deno-repository`)和一个区域(`us-central1`)。然后点击“创建”。

New repository in Google Artifact Repository

构建、标记和推送到 Artifact Registry

创建仓库后,我们可以开始将镜像推送到它。

首先,让我们将注册表的地址添加到 `gcloud`

gcloud auth configure-docker us-central1-docker.pkg.dev

然后,让我们构建您的 Docker 镜像。(请注意,镜像名称在我们的 `docker-compose.yml` 文件中定义。)

docker compose -f docker-compose.yml build

然后,标记 它,使用新的 Google Artifact Registry 地址、仓库和名称。镜像名称应遵循以下结构:`{{ location }}-docker.pkg.dev/{{ google_cloudrun_project_name }}/{{ repository }}/{{ image }}`。

docker tag deno-image us-central1-docker.pkg.dev/deno-app-368305/deno-repository/deno-cloudrun-image

如果您没有指定标签,它将默认使用 `:latest`。

接下来,推送镜像

docker push us-central1-docker.pkg.dev/deno-app-368305/deno-repository/deno-cloudrun-image

有关如何将镜像推送到 Google Artifact Registry 和从 Google Artifact Registry 拉取镜像的更多信息.

您的镜像现在应该出现在您的 Google Artifact Registry 中!

Image in Google Artifact Registry

创建 Google Cloud Run 服务

我们需要一个实例来构建这些镜像,所以让我们访问 Google Cloud Run 并点击“创建服务”。

让我们将其命名为“hello-from-deno”。

选择“从现有容器镜像部署一个修订版”。使用下拉菜单从 `deno-repository` Artifact Registry 中选择镜像。

选择“允许未经身份验证的请求”,然后点击“创建服务”。确保端口为 `8000`。

完成后,您的应用程序现在应该已上线

Hello from Google Cloud Run

太棒了!

使用 gcloud 部署

现在它已经创建好了,我们可以使用 gcloud CLI 部署到这个服务。命令遵循以下结构:gcloud run deploy {{ service_name }} --image={{ image }} --region={{ region }} --allow-unauthenticated。请注意,image 名称遵循上面的结构。

对于这个例子,命令是

gcloud run deploy hello-from-deno --image=us-central1-docker.pkg.dev/deno-app-368305/deno-repository/deno-cloudrun-image --region=us-central1 --allow-unauthenticated

Hello from Google Cloud Run

成功!

使用 GitHub Actions 自动化部署

为了使自动化工作,我们首先需要确保以下两项都已创建:

  • Google Artifact Registry
  • Google Cloud Run 服务实例

(如果您还没有完成,请参阅前面的部分。)

现在我们已经完成了这些,我们可以使用 GitHub 工作流来自动化它。以下是 yaml 文件

name: Build and Deploy to Cloud Run

on:
push:
branches:
- main

env:
PROJECT_ID: {{ PROJECT_ID }}
GAR_LOCATION: {{ GAR_LOCATION }}
REPOSITORY: {{ GAR_REPOSITORY }}
SERVICE: {{ SERVICE }}
REGION: {{ REGION }}

jobs:
deploy:
name: Deploy
permissions:
contents: 'read'
id-token: 'write'

runs-on: ubuntu-latest
steps:
- name: CHeckout
uses: actions/checkout@v3

- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'

- name: Login to GAR
uses: docker/[email protected]
with:
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev
username: _json_key
password: ${{ secrets.GCP_CREDENTIALS }}

- name: Build and Push Container
run: |-
docker build -t "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" ./
docker push "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}"

- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v0
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
image: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}

- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}

我们需要设置的环境变量是(括号中的示例是此存储库的示例)

  • PROJECT_ID:您的项目 ID(deno-app-368305
  • GAR_LOCATION:您的 Google Artifact Registry 设置的位置(us-central1
  • GAR_REPOSITORY:您为 Google Artifact Registry 指定的名称(deno-repository
  • SERVICE:Google Cloud Run 服务的名称(hello-from-deno
  • REGION:Google Cloud Run 服务所在的区域(us-central1

我们需要设置的秘密变量是

查看更多关于从 GitHub Actions 部署到 Cloud Run 的详细信息和示例。

参考:https://github.com/google-github-actions/example-workflows/blob/main/workflows/deploy-cloudrun/cloudrun-docker.yml