在部署前后运行钩子
本快速入门介绍如何运行部署钩子,即在您使用 Cloud Deploy 进行部署之前或之后运行的任意程序。
在本快速入门中,您将执行以下操作:
创建一个 GKE 集群或一个 Cloud Run 服务。
您也可以使用 GKE 附加集群,但本快速入门仅使用 GKE 和 Cloud Run。
定义 Cloud Deploy 交付流水线和部署目标。
交付流水线配置包含部署钩子定义,如
tasks。此流水线仅包含一个阶段,并且仅使用一个目标。
创建 Skaffold 配置以及 Kubernetes 清单或 Cloud Run 服务定义。
在 Skaffold 配置文件中,您可以标识要使用 Cloud Deploy 部署的清单。
创建版本,该版本会自动部署到目标。
其中一个钩子在应用部署之前运行,另一个钩子在应用部署之后运行。
使用控制台中的 Cloud Deploy Rollout details (发布详情)页面,在 Cloud Build 日志中查看部署前和部署后钩子的结果。Google Cloud
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建一个账号,以评估我们的产品在 实际场景中的表现。新客户还可以获得 300 美元的免费抵用金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Deploy, Cloud Build, GKE, Cloud Run, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Deploy, Cloud Build, GKE, Cloud Run, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - 确保默认 Compute Engine 服务账号具有足够的权限。
服务帐号可能已经拥有必要的权限。针对默认服务账号停用自动角色授予的项目包含这些步骤。
- 首先,添加
clouddeploy.jobRunner角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:$(gcloud projects describe PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --role="roles/clouddeploy.jobRunner" - 为您的特定运行时添加开发者角色。
- 对于 GKE:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:$(gcloud projects describe PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --role="roles/container.developer" - 对于 Cloud Run:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:$(gcloud projects describe PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --role="roles/run.developer" -
添加
iam.serviceAccountUser角色,该角色可提供用于部署到运行时的actAs权限:gcloud iam service-accounts add-iam-policy-binding $(gcloud projects describe PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --member=serviceAccount:$(gcloud projects describe PROJECT_ID \ --format="value(projectNumber)")-compute@developer.gserviceaccount.com \ --role="roles/iam.serviceAccountUser" \ --project=PROJECT_ID
如果您已安装 CLI,请确保您运行的是最新版本:
gcloud components update
创建运行时环境
如果您要部署到 Cloud Run,可以跳过此命令。
对于 GKE,请创建一个集群:deploy-hooks-cluster,并使用默认设置。集群的 Kubernetes API 端点必须可通过公共互联网访问。默认情况下,GKE 集群可从外部访问。
gcloud container clusters create-auto deploy-hooks-cluster \
--project=PROJECT_ID \
--region=us-central1
创建交付流水线和目标
您可以在一个文件中定义流水线和目标,也可以在单独的文件中定义。在本快速入门中,您将创建一个文件。
打开一个终端窗口。
创建一个新目录,并导航到此目录。
GKE
mkdir deploy-hooks-gke-quickstart cd deploy-hooks-gke-quickstartCloud Run
mkdir deploy-hooks-run-quickstart cd deploy-hooks-run-quickstart创建交付流水线和目标定义:
GKE
在
deploy-hooks-gke-quickstart目录中,创建包含以下内容的新文件clouddeploy.yaml:apiVersion: deploy.cloud.google.com/v1 kind: DeliveryPipeline metadata: name: deploy-hooks-demo-app-gke-1 description: main application pipeline serialPipeline: stages: - targetId: hooks-staging profiles: [] strategy: standard: predeploy: tasks: - type: container image: ubuntu command: ["/bin/sh"] args: ["-c", 'echo "this is a predeploy action"' ] postdeploy: tasks: - type: container image: ubuntu command: ["/bin/sh"] args: ["-c", 'echo "this is a postdeploy action"' ] --- apiVersion: deploy.cloud.google.com/v1 kind: Target metadata: name: hooks-staging description: hooks staging cluster gke: cluster: projects/PROJECT_ID/locations/us-central1/clusters/deploy-hooks-clusterCloud Run
在
deploy-hooks-run-quickstart目录中,创建包含以下内容的新文件clouddeploy.yaml:apiVersion: deploy.cloud.google.com/v1 kind: DeliveryPipeline metadata: name: deploy-hooks-demo-app-run-1 description: main application pipeline serialPipeline: stages: - targetId: hooks-staging profiles: [] strategy: standard: predeploy: tasks: - type: container image: