gcp-deploy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGCP Cloud Run Deploy
GCP Cloud Run 部署
Deploy from source to Cloud Run in a single command. Requires: a GCP project, a Dockerfile in the repo root, and an app that listens on a port (default 8080 / env var).
PORT通过单条命令将源码部署至Cloud Run。需满足以下条件:一个GCP项目、仓库根目录下的Dockerfile,以及监听端口(默认8080 / 环境变量)的应用。
PORTQuick Deploy
快速部署
bash
undefinedbash
undefined1. Auth (skip if already logged in)
1. 身份验证(已登录可跳过)
gcloud auth login
gcloud config set project PROJECT_ID
gcloud auth login
gcloud config set project PROJECT_ID
2. Enable APIs (first time only)
2. 启用API(首次部署时执行)
gcloud services enable run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com
gcloud services enable run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com
3. Deploy
3. 部署
gcloud run deploy SERVICE_NAME
--source .
--region australia-southeast1
--allow-unauthenticated
--source .
--region australia-southeast1
--allow-unauthenticated
This builds the container remotely via Cloud Build, pushes to Artifact Registry, deploys to Cloud Run, and returns a public HTTPS URL. Redeploy by re-running the same command.gcloud run deploy SERVICE_NAME
--source .
--region australia-southeast1
--allow-unauthenticated
--source .
--region australia-southeast1
--allow-unauthenticated
此命令通过Cloud Build远程构建容器,推送至Artifact Registry,部署到Cloud Run,并返回一个公开的HTTPS URL。重新部署只需再次运行相同命令。Deploy Script
部署脚本
Copy into the project root for one-command deploys:
scripts/deploy.shbash
chmod +x deploy.sh
./deploy.sh my-serviceEnv vars: (default ), (default: current gcloud project).
GCP_REGIONaustralia-southeast1GCP_PROJECT将复制到项目根目录,即可实现一键部署:
scripts/deploy.shbash
chmod +x deploy.sh
./deploy.sh my-service环境变量:(默认值)、(默认值:当前gcloud项目)。
GCP_REGIONaustralia-southeast1GCP_PROJECTCommon Flags
常用参数
| Flag | Purpose | Example |
|---|---|---|
| Deployment region | |
| Public access | |
| Container port (if not 8080) | |
| Env vars | |
| Secret Manager secrets | |
| Memory | |
| CPU | |
| Min instances (0 = scale to zero) | |
| Max instances | |
| Request timeout (max 3600) | |
| 参数 | 用途 | 示例 |
|---|---|---|
| 部署区域 | |
| 允许公开访问 | |
| 容器端口(若不是8080) | |
| 设置环境变量 | |
| 设置Secret Manager密钥 | |
| 内存配置 | |
| CPU配置 | |
| 最小实例数(0表示缩容至零) | |
| 最大实例数 | |
| 请求超时时间(最大3600秒) | |
Environment Variables and Secrets
环境变量与密钥
Inline env vars:
bash
gcloud run deploy SERVICE --source . --region REGION --allow-unauthenticated \
--set-env-vars "DATABASE_URL=postgres://...,API_KEY=abc123"Secret Manager (recommended for sensitive values):
bash
undefined内联设置环境变量:
bash
gcloud run deploy SERVICE --source . --region REGION --allow-unauthenticated \
--set-env-vars "DATABASE_URL=postgres://...,API_KEY=abc123"使用Secret Manager(敏感值推荐方式):
bash
undefinedCreate secret
创建密钥
echo -n "secret-value" | gcloud secrets create MY_SECRET --data-file=-
echo -n "secret-value" | gcloud secrets create MY_SECRET --data-file=-
Grant access to default compute SA
为默认计算服务账号授予访问权限
gcloud secrets add-iam-policy-binding MY_SECRET
--member="serviceAccount:$(gcloud iam service-accounts list --format='value(email)' --filter='displayName:Compute Engine default')"
--role="roles/secretmanager.secretAccessor"
--member="serviceAccount:$(gcloud iam service-accounts list --format='value(email)' --filter='displayName:Compute Engine default')"
--role="roles/secretmanager.secretAccessor"
gcloud secrets add-iam-policy-binding MY_SECRET
--member="serviceAccount:$(gcloud iam service-accounts list --format='value(email)' --filter='displayName:Compute Engine default')"
--role="roles/secretmanager.secretAccessor"
--member="serviceAccount:$(gcloud iam service-accounts list --format='value(email)' --filter='displayName:Compute Engine default')"
--role="roles/secretmanager.secretAccessor"
Deploy with secret
携带密钥部署
gcloud run deploy SERVICE --source . --region REGION --allow-unauthenticated
--set-secrets "MY_SECRET=MY_SECRET:latest"
--set-secrets "MY_SECRET=MY_SECRET:latest"
undefinedgcloud run deploy SERVICE --source . --region REGION --allow-unauthenticated
--set-secrets "MY_SECRET=MY_SECRET:latest"
--set-secrets "MY_SECRET=MY_SECRET:latest"
undefinedUseful Commands
实用命令
bash
undefinedbash
undefinedStream logs
流式查看日志
gcloud run services logs tail SERVICE --region REGION
gcloud run services logs tail SERVICE --region REGION
List services
列出所有服务
gcloud run services list --region REGION
gcloud run services list --region REGION
Get service URL
获取服务URL
gcloud run services describe SERVICE --region REGION --format "value(status.url)"
gcloud run services describe SERVICE --region REGION --format "value(status.url)"
Delete service
删除服务
gcloud run services delete SERVICE --region REGION
undefinedgcloud run services delete SERVICE --region REGION
undefinedCI/Headless Auth
CI/无界面身份验证
bash
gcloud auth activate-service-account --key-file=key.json
gcloud config set project PROJECT_IDbash
gcloud auth activate-service-account --key-file=key.json
gcloud config set project PROJECT_IDTroubleshooting
故障排查
| Issue | Fix |
|---|---|
| Build fails | Check Dockerfile locally: |
| 403 on deploy | Need |
| App crashes on start | Check logs: |
| Port mismatch | Set |
| Cold start slow | Set |
| Timeout on long requests | Increase with |
| 问题 | 解决方法 |
|---|---|
| 构建失败 | 本地检查Dockerfile: |
| 部署时出现403错误 | 需要 |
| 应用启动时崩溃 | 查看日志: |
| 端口不匹配 | 设置 |
| 冷启动缓慢 | 设置 |
| 长请求超时 | 增加超时时间: |