CNB 与 Google Cloud 集成插件,提供 GKE 部署、GCR 镜像推送、GCS 文件同步等功能。
在 .cnb.yml 中使用本插件:
main:
push:
- stages:
- name: deploy to GKE
image: cnbcool/gcloud
settings:
action: gke-deploy
credentials: $GOOGLE_CREDENTIALS
project: my-project
region: us-central1
cluster: my-cluster
namespace: production
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
action | string | 是 | 操作类型:gke-deploy, gcr-push, gcs-sync, gcloud |
credentials | string | 是 | Google Cloud 服务账号密钥(JSON 格式) |
project | string | 是 | GCP 项目 ID |
region | string | 否 | GCP 区域,默认 us-central1 |
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
cluster | string | 是 | GKE 集群名称 |
namespace | string | 否 | Kubernetes 命名空间,默认 default |
kubectl_version | string | 否 | kubectl 版本,默认 latest |
manifest | string | 否 | Kubernetes 清单文件路径 |
image | string | 否 | 要部署的镜像地址 |
app_name | string | 否 | 应用名称 |
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
image | string | 是 | 镜像名称,如 gcr.io/project/app |
tag | string | 否 | 镜像标签,默认 latest |
dockerfile | string | 否 | Dockerfile 路径,默认 ./Dockerfile |
context | string | 否 | 构建上下文路径,默认 . |
build_args | array | 否 | 构建参数 |
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
bucket | string | 是 | GCS 存储桶名称 |
source | string | 是 | 源文件/目录路径 |
destination | string | 是 | 目标路径(以 gs:// 开头) |
sync_delete | boolean | 否 | 是否删除目标中不存在于源的文件 |
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
commands | string/array | 是 | 要执行的 gcloud 命令 |
main:
push:
- stages:
- name: build image
script: docker build -t gcr.io/my-project/my-app:${CNB_COMMIT_SHA} .
- name: deploy to GKE
image: cnbcool/gcloud
settings:
action: gke-deploy
credentials: $GOOGLE_CREDENTIALS
project: my-project
region: us-central1
cluster: my-cluster
namespace: production
manifest: ./k8s/deployment.yaml
main:
push:
- services:
- docker
stages:
- name: build and push to GCR
image: cnbcool/gcloud
settings:
action: gcr-push
credentials: $GOOGLE_CREDENTIALS
project: my-project
image: gcr.io/my-project/my-app
tag: ${CNB_COMMIT_SHA}
dockerfile: ./Dockerfile
main:
push:
- stages:
- name: sync to GCS
image: cnbcool/gcloud
settings:
action: gcs-sync
credentials: $GOOGLE_CREDENTIALS
project: my-project
bucket: my-bucket
source: ./dist
destination: gs://my-bucket/static
sync_delete: true
main:
push:
- stages:
- name: list SQL instances
image: cnbcool/gcloud
settings:
action: gcloud
credentials: $GOOGLE_CREDENTIALS
project: my-project
commands: |
gcloud sql instances list
gcloud compute instances list
Kubernetes Engine Developer, Storage Admin)将服务账号密钥内容存储为 CNB 密钥变量:
GOOGLE_CREDENTIALS 的变量插件执行时会注入以下环境变量:
| 环境变量 | 说明 |
|---|---|
PLUGIN_ACTION | 操作类型 |
PLUGIN_CREDENTIALS | Google Cloud 凭证 |
PLUGIN_PROJECT | GCP 项目 ID |
PLUGIN_REGION | GCP 区域 |
PLUGIN_CLUSTER | GKE 集群名称 |
PLUGIN_NAMESPACE | Kubernetes 命名空间 |
PLUGIN_IMAGE | 镜像名称 |
PLUGIN_TAG | 镜像标签 |
PLUGIN_BUCKET | GCS 存储桶名称 |
PLUGIN_SOURCE | 源路径 |
PLUGIN_DESTINATION | 目标路径 |
PLUGIN_COMMANDS | 自定义命令 |
PLUGIN_KUBECTL_VERSION | kubectl 版本 |
A: 可以在流水线中使用多个 stage,分别指定不同的 region:
main:
push:
- stages:
- name: deploy to us-central1
image: cnbcool/gcloud
settings:
action: gke-deploy
region: us-central1
# ...
- name: deploy to asia-east1
image: cnbcool/gcloud
settings:
action: gke-deploy
region: asia-east1
# ...
A: 指定完整的镜像地址即可,Artifact Registry 和 GCR 使用相同的认证方式:
settings:
image: asia-east1-docker.pkg.dev/my-project/my-repo/my-app
MIT License