在 Cloud Deploy 中自動推送版本,推進推出程序

本頁面說明如何使用 Cloud Deploy 自動將版本推送至目標,並將推出作業推進至下一階段。

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

  1. 建立兩個 GKE 叢集或兩個 Cloud Run 服務。

  2. 建立 Skaffold 設定,以及 Kubernetes 資訊清單或 Cloud Run 服務定義。

  3. 定義 Cloud Deploy 推送管道和部署目標。

    管道會部署至兩個目標:devstaging。且staging目標使用初期測試部署策略

  4. 定義兩項自動化規則:

    • 自動化作業:在成功將版本推出至 dev 後,將版本推送至 staging 目標。

    • 自動化作業:在成功完成 canary-25` 階段後,將推出作業推進至 stable 階段。

  5. 建立版本來例項化推送管道,系統會自動部署至 dev 目標。

  6. 在 Google Cloud 控制台中查看推送管道和版本。

    由於系統會自動升級版本,因此這個版本會自動升級至 staging

    由於 staging 目標使用初期測試部署策略,且這是首次部署到該執行階段,因此系統會略過 canary-25 階段。請參閱「為什麼有時會略過階段」,進一步瞭解首次略過初期測試階段的原因。

    由於系統會自動推進階段,因此推出作業會推進至 stable 階段。

事前準備

  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, Cloud Run, 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, Cloud Run, 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. 如果您已安裝 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. 新增 clouddeploy.releaser 角色:
      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.releaser"
      
    3. 為特定執行階段新增開發人員角色。
      • 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"
        
    4. 新增 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
      
    5. 新增 iam.serviceAccountUser 角色,包括 actAs 權限,以便使用預設服務帳戶:
      gcloud iam service-accounts add-iam-policy-binding $(gcloud projects describe PROJECT_ID \
          --format="value(projectNumber)")-compute@developer.gserviceaccount.com \
          --member=user:YOUR_EMAIL_ADDRESS \
          --role="roles/iam.serviceAccountUser" \
          --project=PROJECT_ID
      

      在本例中,YOUR_EMAIL_ADDRESS 是您用來存取 Google Cloud 的電子郵件地址。

建立執行階段環境

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

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

gcloud container clusters create-auto automation-quickstart-cluster-dev \
                 --project=PROJECT_ID \
                 --region=us-central1 \
                 && gcloud container clusters create-auto automation-quickstart-cluster-staging \
                 --project=PROJECT_ID \
                 --region=us-central1

取得專案編號。

您需要專案編號來識別預設服務帳戶。這是設定自動化資源的必要條件。

  1. 執行下列指令,取得專案編號:

    gcloud projects describe PROJECT_ID --format="value(projectNumber)"
    
  2. 從指令列輸出內容複製專案編號,然後貼到這裡。

    您不需要以指令形式執行這項操作。將其貼到這裡,即可在本快速入門導覽課程稍後的自動化設定中填入服務帳戶參照。

    PROJECT_NUMBER
    

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

Cloud Deploy 會使用 Skaffold 提供詳細資料,說明要部署的內容,以及如何為個別目標正確部署。

在本快速入門導覽課程中,您將建立 skaffold.yaml 檔案,識別用於部署範例應用程式的應用程式資訊清單。

  1. 開啟終端機視窗。

  2. 建立新目錄並前往該目錄。

    GKE

    mkdir deploy-automation-gke-quickstart
    cd deploy-automation-gke-quickstart
    

    Cloud Run

    mkdir deploy-automation-run-quickstart
    cd deploy-automation-run-quickstart
    
  3. 建立名為 skaffold.yaml 的檔案,並在當中加入下列內容:

    GKE

    apiVersion: skaffold/v4beta7
    kind: Config
    metadata:
      name: gke-automation
    manifests:
      rawYaml:
      - k8s-deployment.yaml
    deploy:
      kubectl: {}
    

    Cloud Run

    apiVersion: skaffold/v4beta7
    kind: Config
    metadata:
      name: run-automation
    profiles:
    - name: dev
      manifests:
        rawYaml:
        - run-dev.yaml
    - name: staging
      manifests:
        rawYaml:
        - run-staging.yaml
    deploy:
      cloudrun: {}
    

    這個檔案是基本的 Skaffold 設定。在本快速入門導覽課程中,您會建立檔案。但您也可以讓 Cloud Deploy 為您建立一個,適用於簡單的非正式版應用程式。

    如要進一步瞭解這個檔案,請參閱 skaffold.yaml 參考資料

  4. 建立應用程式的定義,也就是 Cloud Run 的一對服務定義,或是 GKE 的 Kubernetes 資訊清單。

    GKE

    建立名為 k8s-deployment.yaml 的檔案,並在當中加入下列內容:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-deployment
      labels:
        app: my-app
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: my-app
      template:
        metadata:
          labels:
            app: my-app
        spec:
          containers:
          - name: nginx
            image: my-app-image
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
      namespace: default
    spec:
      selector:
        app: my-app
      ports:
        - protocol: TCP
          port: 80
    

    這個檔案是簡單的 Kubernetes 資訊清單,用於部署應用程式。要部署的容器映像檔會在此處設為預留位置 my-app-image,當您建立版本時,系統會將其替換為特定映像檔。

    Cloud Run

    1. 建立名為 run-dev.yaml 的檔案,並在當中加入下列內容:

      apiVersion: serving.knative.dev/v1
      kind: Service
      metadata:
        name: my-automation-run-service-dev
      spec:
        template:
          spec:
            containers:
            - image: my-app-image
      
    2. 建立名為 run-staging.yaml 的檔案,並在當中加入下列內容:

      apiVersion: serving.knative.dev/v1
      kind: Service
      metadata:
        name: my-automation-run-service-staging
      spec:
        template:
          spec:
            containers:
            - image: my-app-image
      

    這些檔案是簡單的 Cloud Run 服務定義,用於部署應用程式。要部署的容器映像檔在此處設為預留位置 my-app-image,當您建立版本時,系統會將其替換為特定映像檔。

建立推送管道、目標和自動化作業

您可以在一個檔案或多個檔案中定義發布管道和目標。您也可以在個別檔案中定義自動化動作。本快速入門導覽會使用一個檔案來處理管道、目標和自動化作業。

  1. 建立推送管道、目標定義和自動化動作:

    GKE

    deploy-automation-gke-quickstart 目錄中建立新檔案:clouddeploy.yaml,並加入下列內容:

    apiVersion: deploy.cloud.google.com/v1
    kind: DeliveryPipeline
    metadata:
      name: my-automation-demo-app-1
    description: Automation demonstration pipeline
    serialPipeline:
      stages:
      - targetId: automation-quickstart-dev
      - targetId: automation-quickstart-staging
        profiles: []
        strategy:
          canary:
            runtimeConfig:
              kubernetes:
                serviceNetworking:
                  service: "my-service"
                  deployment: "my-deployment"
            canaryDeployment:
              percentages: [25]
              verify: false
    ---
    
    apiVersion: deploy.cloud.google.com/v1
    kind: Target
    metadata:
      name: automation-quickstart-dev
    description: Dev cluster to demonstrate deploy automation
    gke:
      cluster: projects/PROJECT_ID/locations/us-central1/clusters/automation-quickstart-cluster-dev
    ---
    
    apiVersion: deploy.cloud.google.com/v1
    kind: Target
    metadata:
      name: automation-quickstart-staging
    description: Staging cluster to demonstrate deploy automation
    gke:
      cluster: projects/PROJECT_ID/locations/us-central1/clusters/automation-quickstart-cluster-staging
    ---
    
    apiVersion: deploy.cloud.google.com/v1
    kind: Automation
    metadata:
      name: my-automation-demo-app-1/promote
    description: promotes a release
    suspended: false
    serviceAccount: PROJECT_NUMBER-compute@developer.gserviceaccount.com
    selector:
      targets:
      - id: automation-quickstart-dev
    rules:
    - promoteReleaseRule:
        id: "promote-release"
        wait: 1m
        destinationTargetId: "@next"
    ---
    
    apiVersion: deploy.cloud.google.com/v1
    kind: Automation
    metadata:
      name: my-automation-demo-app-1/advance
    description: advances a rollout
    suspended: false