Appearance
Google Cloud Run
Google Cloud Run 是由 Google Cloud 构建的无服务器平台。你可以根据事件运行代码,Google 会自动为你管理底层计算资源。
Google Cloud Run 使用容器来运行你的服务。这意味着你可以通过提供 Dockerfile 来使用任何你喜欢的运行时(例如 Deno 或 Bun)。如果没有提供 Dockerfile,Google Cloud Run 将使用默认的 Node.js 构建包。
🌐 Google Cloud Run uses containers to run your service. This means you can use any runtime you like (E.g., Deno or Bun) by providing a Dockerfile. If no Dockerfile is provided Google Cloud Run will use the default Node.js buildpack.
本指南假设你已拥有 Google Cloud 账户和结算账户。
🌐 This guide assumes you already have a Google Cloud account and a billing account.
1. 安装 CLI
🌐 1. Install the CLI
在使用 Google Cloud Platform 时,使用 gcloud CLI 最为方便。
🌐 When working with Google Cloud Platform, it is easiest to work with the gcloud CLI.
例如,在 MacOS 上使用 Homebrew:
🌐 For example, on MacOS using Homebrew:
sh
brew install --cask gcloud-cli使用 CLI 进行身份验证。
🌐 Authenticate with the CLI.
sh
gcloud auth login2. 项目设置
🌐 2. Project setup
创建一个项目。在提示时接受自动生成的项目ID。
🌐 Create a project. Accept the auto-generated project ID at the prompt.
sh
gcloud projects create --set-as-default --name="my app"为你的项目 ID 和项目编号创建环境变量以便于重复使用。项目通过 gcloud projects list 命令成功返回可能需要大约 30 秒。
🌐 Create environment variables for your project ID and project number for easy reuse. It may take ~30 seconds before the project successfully returns with the gcloud projects list command.
sh
PROJECT_ID=$(gcloud projects list \
--format='value(projectId)' \
--filter='name="my app"')
PROJECT_NUMBER=$(gcloud projects list \
--format='value(projectNumber)' \
--filter='name="my app"')
echo $PROJECT_ID $PROJECT_NUMBER查找你的结算账户 ID。
🌐 Find your billing account ID.
sh
gcloud billing accounts list将上一个命令中的结算账户添加到项目中。
🌐 Add your billing account from the prior command to the project.
sh
gcloud billing projects link $PROJECT_ID \
--billing-account=[billing_account_id]启用所需的 API。
🌐 Enable the required APIs.
sh
gcloud services enable run.googleapis.com \
cloudbuild.googleapis.com更新服务账户权限以访问 Cloud Build。
🌐 Update the service account permissions to have access to Cloud Build.
sh
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \
--role=roles/run.builder3. 你好,世界
🌐 3. Hello World
使用 "create-hono" 命令开始你的项目。选择 nodejs。
🌐 Start your project with "create-hono" command. Select nodejs.
sh
npm create hono@latest my-app移动到 my-app 并安装依赖。
🌐 Move to my-app and install the dependencies.
sh
cd my-app
npm i将 src/index.ts 中的端口更新为 8080。
🌐 Update the port in src/index.ts to be 8080.
ts
import { serve } from '@hono/node-server'
import { Hono } from 'hono'
const app = new Hono()
app.get('/', (c) => {
return c.text('Hello Hono!')
})
serve({
fetch: app.fetch,
port: 3000
port: 8080
}, (info) => {
console.log(`Server is running on http://localhost:${info.port}`)
})在本地运行开发服务器。然后,在你的网页浏览器中访问 http://localhost:8080。
🌐 Run the development server locally. Then, access http://localhost:8080 in your Web browser.
sh
npm run dev4. 部署
🌐 4. Deploy
启动部署并按照交互式提示操作(例如,选择区域)。
🌐 Start the deployment and follow the interactive prompts (E.g., select a region).
sh
gcloud run deploy my-app --source . --allow-unauthenticated更改运行时
🌐 Changing runtimes
如果你想使用 Deno 或 Bun 运行时(或自定义的 Nodejs 容器)进行部署,请添加一个 Dockerfile(可选地还可以添加 .dockerignore)以及你所需的环境。
🌐 If you want to deploy using Deno or Bun runtimes (or a customised Nodejs container), add a Dockerfile (and optionally .dockerignore) with your desired environment.
有关容器化的信息,请参阅:
🌐 For information on containerizing, please refer to: