logo
0
0
WeChat Login
feat/create-gcloud-plugin

CNB Google Cloud Integration Plugin

CNB 与 Google Cloud 集成插件,提供 GKE 部署、GCR 镜像推送、GCS 文件同步等功能。

功能特性

  • GKE 部署 - 部署应用到 Google Kubernetes Engine
  • GCR 推送 - 推送 Docker 镜像到 Google Container Registry
  • GCS 同步 - 上传/下载文件到 Google Cloud Storage
  • 通用命令 - 执行任意 gcloud 命令

快速开始

基本配置

.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

参数说明

通用参数

参数类型必填说明
actionstring操作类型:gke-deploy, gcr-push, gcs-sync, gcloud
credentialsstringGoogle Cloud 服务账号密钥(JSON 格式)
projectstringGCP 项目 ID
regionstringGCP 区域,默认 us-central1

GKE 部署参数

参数类型必填说明
clusterstringGKE 集群名称
namespacestringKubernetes 命名空间,默认 default
kubectl_versionstringkubectl 版本,默认 latest
manifeststringKubernetes 清单文件路径
imagestring要部署的镜像地址
app_namestring应用名称

GCR 推送参数

参数类型必填说明
imagestring镜像名称,如 gcr.io/project/app
tagstring镜像标签,默认 latest
dockerfilestringDockerfile 路径,默认 ./Dockerfile
contextstring构建上下文路径,默认 .
build_argsarray构建参数

GCS 同步参数

参数类型必填说明
bucketstringGCS 存储桶名称
sourcestring源文件/目录路径
destinationstring目标路径(以 gs:// 开头)
sync_deleteboolean是否删除目标中不存在于源的文件

通用 gcloud 命令参数

参数类型必填说明
commandsstring/array要执行的 gcloud 命令

使用示例

示例 1:部署应用到 GKE

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

示例 2:推送镜像到 GCR

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

示例 3:同步文件到 GCS

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

示例 4:执行自定义 gcloud 命令

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

凭证配置

创建服务账号密钥

  1. 在 Google Cloud Console 中创建服务账号
  2. 授予适当的 IAM 角色(如 Kubernetes Engine Developer, Storage Admin
  3. 创建 JSON 格式的密钥文件

在 CNB 中配置凭证

将服务账号密钥内容存储为 CNB 密钥变量:

  1. 进入仓库设置 → 密钥管理
  2. 创建名为 GOOGLE_CREDENTIALS 的变量
  3. 将服务账号 JSON 密钥内容粘贴到值中

环境变量

插件执行时会注入以下环境变量:

环境变量说明
PLUGIN_ACTION操作类型
PLUGIN_CREDENTIALSGoogle Cloud 凭证
PLUGIN_PROJECTGCP 项目 ID
PLUGIN_REGIONGCP 区域
PLUGIN_CLUSTERGKE 集群名称
PLUGIN_NAMESPACEKubernetes 命名空间
PLUGIN_IMAGE镜像名称
PLUGIN_TAG镜像标签
PLUGIN_BUCKETGCS 存储桶名称
PLUGIN_SOURCE源路径
PLUGIN_DESTINATION目标路径
PLUGIN_COMMANDS自定义命令
PLUGIN_KUBECTL_VERSIONkubectl 版本

常见问题

Q: 如何处理多区域部署?

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
            # ...

Q: 如何使用 Artifact Registry 替代 GCR?

A: 指定完整的镜像地址即可,Artifact Registry 和 GCR 使用相同的认证方式:

settings:
  image: asia-east1-docker.pkg.dev/my-project/my-repo/my-app

许可证

MIT License

About

No description, topics, or website provided.
Language
Shell85.3%
Dockerfile14.7%