在 Google Cloud 控制台中创建流水线和版本

本页面介绍如何使用 Google Cloud 控制台创建 Cloud Deploy 交付流水线,然后为该流水线创建版本。

在本快速入门中,您将执行以下操作:

  1. 创建两个 GKE 集群或配置两个 Cloud Run 服务。

  2. 使用Google Cloud 控制台创建交付流水线 和两个目标

  3. 使用 Google Cloud 控制台创建版本,以实例化交付流水线。

    创建此版本后,应用会自动部署到目标。

  4. 在 Google Cloud 控制台中查看结果。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建账号,以便在 真实场景中评估我们产品的性能。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Deploy, Cloud Build, GKE, Artifact Registry, Cloud Run, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 安装 Google Cloud CLI。

  6. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  7. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  8. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Deploy, Cloud Build, GKE, Artifact Registry, Cloud Run, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. 安装 Google Cloud CLI。

  12. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  13. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  14. 如果您已安装 CLI,请确保您运行的是最新版本:

    gcloud components update
    

创建运行时环境

如果您要部署到 Cloud Run,则可以跳过此命令

对于 GKE,请使用默认设置创建两个集群:quickstart-cluster-for-console-stagingquickstart-cluster-for-console-prod。集群的 Kubernetes API 端点必须可通过公共互联网访问。默认情况下,GKE 集群可从外部访问。

gcloud container clusters create-auto quickstart-cluster-for-console-staging \
                 --project=PROJECT_ID \
                 --region=us-central1 && \
gcloud container clusters create-auto quickstart-cluster-for-console-prod \
                 --project=PROJECT_ID \
                 --region=us-central1

创建交付流水线和两个目标

您可以使用 Cloud Deploy 来 根据一个或多个 YAML 文件中指定的配置 创建交付流水线和目标。但您也可以 使用 Google Cloud 控制台创建交付流水线。

在本部分中,您将使用 Google Cloud 控制台创建交付流水线 和两个目标。使用 Google Cloud 控制台时,您无需创建 任何 YAML 文件;Cloud Deploy 会为您创建 skaffold.yaml 和清单。

  1. 在 Google Cloud 控制台中,前往 Cloud Deploy 主页面。

    打开“交付流水线”页面

  2. 点击创建

     Google Cloud 控制台中的 Cloud Deploy 主页面

    此时会显示创建交付流水线 表单。

  3. 流水线名称 字段中,将默认文本替换为 in-console-quickstart-pipeline

  4. (可选)输入此交付流水线的说明。

  5. 区域 下拉列表中,选择 us-central1

  6. 选择运行时。

    如果您要部署到 GKE,请选择 Google Kubernetes Engine 。否则,请选择 Cloud Run

  7. 创建目标:

    GKE

    1. 新目标 下的目标名称 字段中,将默认文本替换为 console-staging

    2. Kubernetes Engine 集群 下拉列表中,选择 quickstart-cluster-for-console-staging

    3. 点击完成

    4. 点击添加目标

      目标名称 的默认文本替换为 console-prod

    5. Kubernetes Engine 集群 下拉列表中,选择 quickstart-cluster-for-console-prod

    6. 选择需要获得批准才能发布

      在本快速入门中,我们需要获得第二个目标的批准 ,但不需要获得第一个目标的批准。

    7. 点击创建 以创建此交付流水线。

       Google Cloud 控制台中的 Cloud Deploy 主页面

    Cloud Run

    1. 新目标 下的目标名称 字段中,将默认文本替换为 console-staging

    2. 区域 下拉列表中,选择 us-central1

    3. 点击完成

    4. 点击添加目标

    5. 目标名称 的默认文本替换为 console-prod

    6. 为此目标选中需要获得批准才能发布 复选框。

      在本快速入门中,我们需要获得第二个目标的批准 ,但不需要获得第一个目标的批准。

    7. 点击创建 以创建此交付流水线。

       Google Cloud 控制台中的 Cloud Deploy 主页面

    现在,您已经有了一个包含两个目标的交付流水线,可以创建版本了。
    流水线的页面随即显示,其中显示了两个目标,但没有发布。

    在 Google Cloud 控制台中创建的新交付流水线

创建版本

现在,您已经有了一个包含两个目标的交付流水线,该流水线指向两个 GKE 集群或两个 Cloud Run 服务,您可以创建版本以将应用部署到第一个目标。

  1. 如果您尚未进入交付流水线页面(其中显示新的交付流水线 in-console-quickstart-pipeline),请立即前往该页面。

    流水线直观显示效果随即显示,但没有发布。

  2. 点击创建版本 按钮。

    此时会显示创建版本 对话框。大多数字段都已预先填充。保留这些默认值。

    **创建发布版本** 对话框

    您可以点击查看清单 按钮来查看自动生成的清单(适用于任一目标),还可以点击查看 Skaffold 文件 按钮来查看生成的 skaffold.yaml。您也可以对其进行修改,但对于本快速入门,请保留原样。

  3. (可选)在说明 字段中为此版本添加说明。

  4. 点击创建 以开始发布。

    此时会显示发布到第一个目标的发布详情页面,您可以查看此发布的进度。完成此过程需要几分钟时间。发布可能需要几秒钟才能开始。

  5. 第一个发布完成后,点击交付流水线名称以转到交付流水线页面。

    点击流水线名称以查看可视化图表

    流水线直观显示效果随即显示,其中显示已完成到第一个目标的发布。

    交付流水线直观显示,其中第一个目标已完成

  6. 点击提升 以开始发布到下一个目标。

    此时会显示提升 对话框。

  7. 保留默认值,根据需要添加发布说明 ,然后点击提升

    由于我们在创建第二个目标时选择了需要获得批准才能发布 ,因此此提升操作正在等待批准。

  8. 在交付流水线直观显示效果中,点击审核

    此时会显示审批页面。

    需要获得批准才能提升到第二个目标

  9. 再次点击审核 ,然后在审批对话框中点击批准

    系统会开始发布到第二个目标。您可以再次点击交付流水线名称,以在流水线直观显示效果中查看进度。

第二个发布完成后,应用会部署在第二个目标中,并且您的交付流水线已完成。

已完成的交付流水线。

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

  1. 删除 GKE 集群或 Cloud Run 服务:

    GKE

    gcloud container clusters delete quickstart-cluster-for-console-staging --region=us-central1 --project=PROJECT_ID \
    && gcloud container clusters delete quickstart-cluster-for-console-prod --region=us-central1 --project=PROJECT_ID
    

    Cloud Run

    gcloud run services delete in-console-quickstart-pipeline-target-1 --region=us-central1 --project=PROJECT_ID \
    && gcloud run services delete in-console-quickstart-pipeline-target-2 --region=us-central1 --project=PROJECT_ID
    
  2. 在交付流水线页面中,点击删除 以删除交付流水线、版本和发布。

    在提供的字段中输入流水线名称,然后点击确认 以完成资源删除。

    用于删除流水线和子资源的确认对话框。目标

  3. 删除两个目标:

    gcloud deploy targets delete console-staging --region=us-central1  && \
    gcloud deploy targets delete console-prod --region=us-central1
    
  4. 删除 Cloud Deploy 创建的 Cloud Storage 存储分区。

    一个以 _clouddeploy 结尾,另一个是 [region].deploy-artifacts.[project].appspot.com

    打开“Cloud Storage 浏览器”页面

恭喜,您已经完成快速入门!

后续步骤