跳到正文
Skillpedia
部署与云(DevOps)33k · #8738

cloud-run-basics

它能做什么

这个技能讲解并指导使用 Google Cloud Run,这是一个无需你管理服务器即可运行代码的平台。适合在 Google Cloud 上构建或托管应用的人员。

适用场景

  • ✓你想把网页应用或 API 部署到 Google Cloud Run 上线的时候。
  • ✓你需要任务按计划运行或按需运行一次的时候。
  • ✓你有一个需要从队列读取数据、且必须始终运行的后台工作进程的时候。

技能链接

https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/development/cloud-run-basics/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/development/cloud-run-basics/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我把我的网页应用部署到 Google Cloud Run,使其能在互联网上访问。”

每周 5 个新技能,1 分钟读完。

工作原理

这个技能指导你完成启用所需的 Google Cloud 服务、设置访问角色,并创建三种资源类型之一:服务、作业或工作进程池。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Cloud Run Basics

Cloud Run is a fully managed application platform for running your code, function, or container on top of Google's highly scalable infrastructure. It abstracts away infrastructure management, providing three primary resource types:

  1. Services: Responds to HTTP requests sent to a unique and stable endpoint, using stateless instances that autoscale based on a variety of key metrics, also responds to events and functions.
  2. Jobs: Executes parallelizable tasks that are executed manually, or on a schedule, and run to completion.
  3. Worker pools: Handles always-on background workloads such as pull-based workloads, for example, Kafka consumers, Pub/Sub pull queues, or RabbitMQ consumers.

Prerequisites

  1. Enable the Cloud Run Admin API and Cloud Build APIs:

    gcloud services enable run.googleapis.com cloudbuild.googleapis.com --quiet
    
  2. If you are under a domain restriction organization policy restricting unauthenticated invocations for your project, you will need to access your deployed service as described under Testing private services.

Required roles

You need the following roles to deploy your Cloud Run resource:

  • Cloud Run Admin (roles/run.admin) on the project
  • Cloud Run Source Developer (roles/run.sourceDeveloper) on the project
  • Service Account User (roles/iam.serviceAccountUser) on the service identity
  • Logs Viewer (roles/logging.viewer) on the project

Cloud Build automatically uses the Compute Engine default service account as the default Cloud Build service account to build your source code and Cloud Run resource, unless you override this behavior.

For Cloud Build to build your sources, grant the Cloud Build service account the Cloud Run Builder (roles/run.builder) role on your project:

gcloud projects add-iam-policy-binding PROJECT_ID \
    --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \
    --role=roles/run.builder \
    --quiet

Replace PROJECT_ID with your Google Cloud project ID and SERVICE_ACCOUNT_EMAIL_ADDRESS with the email address of the Cloud Build service account.

Deploy a Cloud Run service

You can deploy your service to Cloud Run by using a container image or deploy directly from source code using a single Google Cloud CLI command.

CRITICAL RULE: Any deployed code MUST listen on 0.0.0.0 (not 127.0.0.1) and use the injected $PORT environment variable (defaults to 8080), or it will crash on boot.

Deploy a container image to Cloud Run

Cloud Run imports your container image during deployment. Cloud Run keeps this copy of the container image as long as it is used by a serving revision. Container images are not pulled from their container repository when a new Cloud Run instance is started.

Supported container images

You can directly use container images stored in the Artifact Registry, or Docker Hub. Google recommends the use of Artifact Registry since Docker Hub images are cached for up to one hour.

You can use container images from other public or private registries (like JFrog Artifactory, Nexus, or GitHub Container Registry), by setting up an Artifact Registry remote repository.

You should only consider Docker Hub for deploying popular container images such as Docker Official Images or Docker Sponsored OSS images. For higher availability, Google recommends deploying these Docker Hub images using an Artifact Registry remote repository.

To deploy a container image, run the following command:

    gcloud run deploy SERVICE_NAME \
        --image IMAGE_URL \
        --region us-central1 \
        --allow-unauthenticated \
        --quiet

Replace the following:

  • SERVICE_NAME: the name of the service you want to deploy to. Service names must be 49 characters or less and must be unique pe
常见问题

服务和作业有什么区别?

服务以固定地址响应网页请求。作业则运行一项任务直到完成,可以按需运行,也可以按计划运行。

工作进程池用来做什么?

它用于持续运行的后台任务,例如拉取消息的队列消费者。

部署前我需要启用什么吗?

需要。你的项目中必须启用 Cloud Run Admin API 和 Cloud Build API。

详情

作者的原始描述

Manages Cloud Run services, jobs, and worker pools. Use when you need to deploy applications responding to HTTP requests (services), run event-triggered or scheduled tasks (jobs), or handle always-on pull-based background processing (worker pools).

repo: davila7/claude-code-templates

热度

GitHub 星标
33k
排名
#8738
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能