Cloudrun development

From infometa/workbuddyskills · View interactive version

Cloudrun development is an AI agent skill from the GitHub repository infometa/workbuddyskills. CloudBase Run backend development rules (Function mode/Container mode). Use this skill when deploying backend services that require long connections, multi-language support, custom environments, AI agent development, or migrating existing/GitHub apps that need VPC access to MySQL/PostgreSQL/Redis. A

How to use this skill

Skill file

Sibling skills (local only)

Sibling CloudBase skills ship beside this skill. Use local relative paths such as ../auth-tool-cloudbase/SKILL.md.

If a referenced sibling skill file is missing from this environment, ask the user to install the full CloudBase plugin (or the missing skill). Do not HTTP-fetch remote skill or protocol markdown into the agent context.

Cross-cutting protocols (required before writing HTTP handlers or deploying images):

CloudBase Run Development

Activation Contract

Use this first when

Read before writing code if

Then also read

Do NOT use for

Common mistakes / gotchas

Minimal checklist

Overview

Use CloudBase Run when the task needs a deployed backend service rather than a short-lived serverless function.

云托管 vs HTTP 云函数(按需求选,不按文件选)

核心原则:HTTP 云函数优先。只有需求真正需要云托管时才用云托管;有 Dockerfile 不等于必须上云托管。

HTTP 云函数更合适(优先):

云托管才需要(只有以下之一才选云托管):

语言不是上云托管的条件:非标准语言(Java / Rust / C++ / PHP / .NET 等)或需要自定义系统依赖时,只要服务仍是「请求进来 → 响应」且无长连接,打成镜像用 Custom Image HTTP Function,就还是函数;只有再叠加常驻 / 长连接 / VPC 才上云托管。

决策示例: 一个带 Dockerfile 的 Go/Python HTTP API,无长连接、无自定义系统依赖、不碰 VPC 数据库 → 选 HTTP 云函数而不是云托管;同一份代码若有 WebSocket 长连接 → 才选云托管。

数据库访问方式决策门(部署前必答:SDK 优先,TCP 直连兜底)

核心原则:能用 CloudBase SDK/网关访问的数据,一律优先 SDK 路径。 TCP 直连会引入 VPC、安全组、数据库账号密码三件套,全是部署后才暴露的问题(ETIMEDOUT、安全组拦截、密码注入),能不碰就不碰。

优先:SDK / 网关路径(无需 VpcConf、无需数据库账号密码)

仅当以下情况才走 TCP 直连(须完成 references/vpc-and-database.md 全流程):

决策动作: 扫描到 DATABASE_URL / DB 依赖信号时,先停下来回答「这个数据访问能不能换成 SDK/网关」,再决定是否进入 VPC checklist——不要默认按 TCP 直连方案往下走。

计算资源访问 CloudBase 的凭证决策门(部署前必答)

核心原则:SDK 路径免掉的是数据库账号密码,不是 CloudBase 资源访问凭证。 服务代码要调 CloudBase 资源(PG app.rdb() / NoSQL / storage / functions)时,先把凭证来源定下来,再写代码、再部署。

三件事必须先答:

  1. 谁签发 — CloudBase 服务端 API Key:manageAppAuth(action="createApiKey", keyType="api_key", keyName="<service>-<env>"),或 CLI tcb env apikey create my-key -e env-xxx。不要从本地客户端登录态(auth.json / .cloudbase/)里取一把来用。
  2. 怎么注入 — 经 serverConfig.EnvParams 注入 CLOUDBASE_APIKEY(@cloudbase/node-sdk 自动读取该变量;显式字段是 accessKey)。变量名以官方为准,不要自造。改环境变量时保留已有键值,不要整份覆盖。
  3. 怎么吊销 — 每个服务一把专用 key,记录 keyName 与轮换负责人;下线或轮换时 manageAppAuth(action="deleteApiKey", keyId=...) 并重新部署。轮换后旧实例里残留的副本不会报错,只会静默失效。

不要假设云托管容器已自动带上可用的 CloudBase 凭证 —— 部署后用一次真实的 SDK 读取验证(验证两次以上,不要只看进程起没起来)。完整步骤、Manager SDK 的腾讯云密钥对路径、环境变量合并的安全写法见 ../cloud-functions/references/http-function-credentials.md。

api_key 是环境级凭证:可绕过 RLS,单环境签发数量有限。不要给每个服务灌同一把 —— 任一实例失陷即整环境失陷。

When CloudRun is a better fit

Mode selection

| Dimension | Function mode | Container mode |

| --- | --- | --- |

| Best for | Fast start, Node.js service patterns, built-in framework, Agent flows | Existing containers, arbitrary runtimes, custom system dependencies |

| Port model | The function framework binds the platform port itself — your code must not call app.listen() | App must listen on the injected PORT |

| Dockerfile | Not required | Required — but a Dockerfile alone does not mean CloudRun; first check whether the service needs long connections / custom runtime. Stateless HTTP services with a Dockerfile may fit HTTP cloud functions better. |

| Local run through tools | Supported | Not supported |

| Typical use | Streaming APIs, low-latency backend, Agent service | Custom language stack, migrated container app |

How to use this skill (for a coding agent)

  1. Choose mode first
  1. Follow mandatory runtime rules
  1. Use the correct tools
  1. Follow the deployment sequence

Tool routing

Read operations

Log query SOP(构建日志 vs 运行日志)

部署失败排查时必须区分两类日志,不要只用 getDeployLog:

  1. 云端源码构建(传 targetPath、走 CODING 构建)
  1. 已有镜像部署(传 imageUrl、DeployType=image)
{
  "action": "getProcessLog",
  "detailServerName": "my-svc",
  "runId": "<from latestDeploy.RunId>"
}

Deploy-task SOP(撞到「已有部署发布任务运行中」时)

manageCloudRun(action="deploy") 报「已有部署发布任务运行中」/ already has a deploy task running 时,不要盲目重试,也不要反复改 Dockerfile / serverConfig —— 先确认任务真实状态:

  1. queryCloudRun(action="getManageTask", detailServerName=...) → 读 taskId / taskStatus / latestDeployStatus
  2. queryCloudRun(action="getProcessLog") → 隔 20–40 秒对比两次拉取,确认部署阶段步骤是否还在推进
  3. 按结果分支:

| 观察 | 动作 |

| --- | --- |

| taskStatus 仍是运行态,部署步骤有推进 | 继续等,不要重发 deploy |

| 任务已结束(非运行态),而 deploy 仍被拒 | 才考虑重试 |

| 任务长时间停在非终态,版本也停在 creating 不动 | 记录 taskId / latestDeployStatus / 时间窗作为证据,不要空转重试 |

force=true 不解决这个问题:它只跳过本工具的确认提示,不会取消或覆盖服务端已有的发布任务 —— 把它当「强制覆盖」用只会白撞一次。

日志侧的排查顺序(构建日志 vs 运行日志、先日志后配置)见上面的 Log query SOP 与 Container deploy failure SOP。

Write operations

Deploying an existing image (imageUrl)

已有一个现成镜像(本地构建好、或第三方发布)时,不需要本地源码目录,直接 manageCloudRun(action="deploy") 传入 imageUrl 即可,走 DeployType="image"(容器型)部署,targetPath 可省略。若用户明确提到使用某个镜像或无需重新构建代码,必须传 imageUrl,不要仅因本地有源码目录就回退到源码构建。

决策路径(直填 vs 本地中转):

  1. 公网匿名可拉取(如 ccr.ccs.tencentyun.com/...、公开 Docker Hub 镜像)→ 直填 imageUrl:manageCloudRun(action="deploy", serverName=..., imageUrl="ccr.ccs.tencentyun.com/ns/img:v1", serverConfig={...})。CloudBase 会直接拉取该 registry 地址构建部署。若 docker.io / Docker Hub 在节点上反复拉取失败,不要空转重试:改用 Dockerfile FROM <public-image> + targetPath 源码构建(CODING 拉公网镜像,产物进 CCR 内网拉取)。见下方 SOP 第 4 步。
  2. 私有 / 需登录的 registry(ghcr.io、私有 ECR/Harbor 等)→ 本地中转到 CCR:

```

docker pull ghcr.io/example/app:latest

docker tag ghcr.io/example/app:latest ccr.ccs.tencentyun.com/<ns>/app:latest

docker login ccr.ccs.tencentyun.com

docker push ccr.ccs.tencentyun.com/<ns>/app:latest

```

然后把 ccr.ccs.tencentyun.com/<ns>/app:latest 作为 imageUrl 传入。中转只解决拉取,不能替代镜像文档里的启动命令 / 环境变量 / 数据目录。

与 initEnv 联动: 镜像部署同样要求环境已开通云托管。新环境首次部署前先 manageCloudRun(action="initEnv", envId=...),并用 queryCloudRun(action="envStatus") 轮询到 Status=normal;未开通时 deploy 会被拦截并引导先 initEnv。

示例:

{
  "action": "deploy",
  "serverName": "my-image-svc",
  "imageUrl": "ccr.ccs.tencentyun.com/ns/app:latest",
  "serverConfig": {
    "OpenAccessTypes": ["PUBLIC"],
    "Cpu": 0.5,
    "Mem": 1,
    "MinNum": 1,
    "MaxNum": 3,
    "Port": 8080,
    "Cmd": ["node", "server.js"],
    "EnvParams": "{\"PORT\":\"8080\",\"BIND_HOST\":\"0.0.0.0\"}"
  }
}

Port / Cmd / EnvParams 必须来自镜像官方文档的五要素清单,不要套用 3000 或省略启动命令。第三方镜像的完整对照见 references/image-deploy-troubleshooting.md 附录。

部署后:manageCloudRun(deploy) 对镜像返回的 next_step 默认指向 getProcessLog(或先 getDeployRecords 取 RunId),不要改去调 getDeployLog。也可用 queryCloudRun(action="detail") 查看 imageInfo(镜像地址与部署类型)。镜像部署失败排查走下方 SOP。

Container deploy failure SOP

顺序:先查镜像官方文档 → 再查运行日志 → 最后才动配置。禁止一看到 probe failed / deploy_failed 就调 InitialDelaySeconds。

详情与案例:references/image-deploy-troubleshooting.md。

1. 部署前:从镜像官方文档确认五要素

不要靠 Docker Hub tag 或「常见默认值」猜。部署前必须确认:

  1. 启动命令 EntryPoint / Cmd(进程如何前台常驻)→ serverConfig.EntryPoint / Cmd
  2. 服务端口(进程真正 bind 的端口;不要假设 80/3000,也不要假设镜像尊重 PORT)→ serverConfig.Port
  3. 对外监听环境变量(必须 0.0.0.0 而不是 127.0.0.1、功能开关默认关闭等)→ EnvParams
  4. 数据目录挂载 → serverConfig.VolumesConf
  5. 健康端点(CloudRun readiness 探的是服务端口,不是任意 HTTP path)

缺任何一项再部署,失败看起来都会像「健康检查失败」。

2. 部署失败:用 `getProcessLog` 定性

镜像部署(imageUrl)跳过 getDeployLog(那是云端源码构建的构建日志)。从 detail / getDeployRecords 取 RunId,再 queryCloudRun(action="getProcessLog")。

启动日志存在 ≠ 服务正常运行。 banner、s6/tini 行、sidecar "listening" 都不能证明探针目标已起来。

两次日志对比判活: 隔 20–40 秒再拉一次 getProcessLog。

| 观察 | 定性 |

| --- | --- |

| 只有调度/创建步骤(create_eks_*),没有容器 stdout | Pod 调度中 / 镜像拉取 |

| 同一段启动 banner / PID 1 行重复出现(时间戳在走、内容几乎一样) | 容器启动即退出 / 重启循环 |

| 进程还在,但 listen 在 127.0.0.1 或端口 ≠ serverConfig.Port | 端口 / 绑定地址问题 |

| 两次拉取是同一条启动过程在往后打日志,banner 不重复 | 才可能是启动慢 |

3. Readiness probe 真实机制(严禁先调延迟)

部署步骤完成后:先等 N 秒(InitialDelaySeconds),再大约 每 5 秒 探一次服务端口,连续约 30 次全失败 才判本次部署失败。窗口 ≈ N+150s。不是「N 秒后立即失败」。

4. 公网镜像(`docker.io`)反复失败 → Dockerfile 源码构建

节点直连 Docker Hub 反复失败时,不要空转 imageUrl。写:

FROM docker.io/example/app:latest

用 targetPath 走云端源码构建:CODING 构建机拉公网镜像,产物进 CCR,云托管节点内网拉取。这只解决拉取拓扑,不替代第 1 步的 Cmd / 环境变量 / 卷。

5. Supervisor 镜像(s6 / tini / supervisord)启动即退出

PID 1 往往是监督进程,不是 HTTP 应用。用两次日志找子进程重启风暴。若镜像 issue 记录了 PID 1 / pgrep -f 误匹配,按文档 workaround(绝对路径 Cmd、关闭 supervise),不要调探针延迟。示例见 reference 附录。

Access guidance

Quick examples

Initialize

{ "action": "init", "serverName": "my-svc", "targetPath": "/abs/ws/my-svc" }

Local run (Function mode)

{ "action": "run", "serverName": "my-svc", "targetPath": "/abs/ws/my-svc", "runOptions": { "port": 3000 } }

Deploy (no VPC-private dependencies)

{
  "action": "deploy",
  "serverName": "my-svc",
  "targetPath": "/abs/ws/my-svc",
  "serverConfig": {
    "OpenAccessTypes": ["PUBLIC"],
    "Cpu": 0.5,
    "Mem": 1,
    "MinNum": 1,
    "MaxNum": 5
  }
}

Deploy (existing app that connects to MySQL / PostgreSQL / Redis over TCP)

{
  "action": "deploy",
  "serverName": "my-existing-app",
  "targetPath": "/abs/ws/my-existing-app",
  "serverConfig": {
    "OpenAccessTypes": ["PUBLIC"],
    "Cpu": 0.5,
    "Mem": 1,
    "MinNum": 1,
    "MaxNum": 5,
    "EnvParams": "{\"DATABASE_URL\":\"postgres://user:pass@10.x.x.x:5432/app\"}",
    "VpcConf": {
      "VpcId": "vpc-xxxxxxxx",
      "SubnetId": "subnet-xxxxxxxx"
    }
  }
}

Valid OpenAccessTypes values: OA (办公网访问), PUBLIC (公网访问), MINIAPP (小程序访问), VPC (VPC访问). Use PUBLIC for web applications that need public HTTPS access.

MinNum: 1 is the recommended default when you want to reduce cold-start latency. If the user explicitly prefers lower cost and accepts more cold starts, explain the tradeoff and let them reduce MinNum to 0.

Best practices

  1. Prefer PRIVATE/VPC or mini-program internal ingress when possible.
  2. For TCP database access, always pair private DB URLs with VpcConf in the same VPC/region as the database.
  3. Use environment variables for secrets and per-environment configuration — read them server-side only; never return them in HTTP responses. CloudBase API Keys come from manageAppAuth(action="createApiKey"), not from a local client login file.
  4. Verify configuration before and after deployment with queryCloudRun(action="detail").
  5. Keep startup work small to reduce cold-start impact.
  6. For Agent scenarios, use the Agent SDK skill for protocol and adapter details instead of duplicating them here.
  7. For smoke tests, return a fixed { "ok": true } / health payload — never deploy httpbin or any service that reflects request headers.

Troubleshooting hints

Reference index

All packaged reference files (required for skill lint reachability):

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.