使用部署政策限制推出作業

本快速入門導覽課程說明如何防止 Cloud Deploy 在指定時間內將推出作業部署至目標,以及如何覆寫這項限制。

在本快速入門導覽課程中,您將執行下列操作:

  1. 建立 Skaffold 設定和 Kubernetes 資訊清單或 Cloud Run 服務定義,指定要部署的 (預先建構) 容器映像檔。

  2. 定義 Cloud Deploy 推送管道和一個部署目標,指向一個 GKE 叢集或 Cloud Run 服務。

    這個管道只包含一個階段,適用於一個目標。

  3. 為目標設定部署政策。

    這項政策會定義日期範圍,禁止在該範圍內向目標推出版本。

  4. 建立版本。

    通常建立版本時,Cloud Deploy 會為推送管道進度中的第一個目標建立推出作業。在本例中,由於政策禁止部署至目標,因此系統不會為該目標建立推出作業。

  5. 在 Google Cloud 控制台中查看結果。

    由於這項政策,您不會看到版本的推出作業,且推送管道的視覺化內容中不會有待處理的動作。

  6. 覆寫部署政策。

    這項覆寫作業會導致 Cloud Deploy 現在為目標建立推出作業。

  7. 在 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, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    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, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the APIs

  11. 安裝 Google Cloud CLI。

  12. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  13. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  14. 如果您已安裝 Google Cloud CLI,請確認您使用的是最新版本:

    gcloud components update
    
  15. 請確認預設的 Compute Engine 服務帳戶具備足夠的權限。

    服務帳戶可能已具備必要權限。如果專案已停用為預設服務帳戶自動授予角色的功能,則適用這些步驟。

    1. 首先,請新增 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"
      
    2. 為特定執行階段新增開發人員角色。
      • 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"
        

    3. 新增 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
      

建立執行階段環境

如果您要部署至 Cloud Run,可以略過這個指令

如果是 GKE,請建立一個叢集:quickstart-cluster-qsprod。 叢集的 Kubernetes API 端點必須可從公開網際網路連上網路。根據預設,GKE 叢集可從外部存取。

gcloud container clusters create-auto quickstart-cluster-qsprod \
                 --project=PROJECT_ID \
                 --region=us-central1

準備 Skaffold 設定和應用程式資訊清單