在部署前後執行掛鉤
本快速入門導覽課程說明如何執行部署掛鉤,也就是在您使用 Cloud Deploy 部署前後執行的任意程式。
在本快速入門導覽課程中,您將完成下列事項:
- 建立一個 GKE 叢集或一個 Cloud Run 服務。 - 您也可以使用 GKE Enterprise 叢集,但本快速入門導覽只會使用 GKE 和 Cloud Run。 
- 建立 Skaffold 設定,以及 Kubernetes 資訊清單或 Cloud Run 服務定義。 - 您可以在 Skaffold 設定檔中設定要執行的部署掛鉤。您可指定要在部署前執行的容器,以及要在部署後執行的容器。 
- 定義 Cloud Deploy 推送管道和部署目標。 - 在推送管道設定中,您會參照 - skaffold.yaml中定義的部署掛鉤,執行這些掛鉤。- 這個管道只包含一個階段,且只使用一個目標。 
- 建立版本,系統會自動部署至目標。 - 其中一個 Hook 會在部署應用程式前執行,另一個則會在部署後執行。 
- 在Google Cloud console 中,使用 Cloud Deploy 的「推出詳細資料」頁面,查看 Cloud Build 記錄中部署前和部署後掛鉤的結果。 
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- 
    
    
      
        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
      (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 Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
      Install the 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
      (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 Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
      Install the 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
- 開啟終端機視窗。 
- 建立新目錄並前往該目錄。 - GKE- mkdir deploy-hooks-gke-quickstart cd deploy-hooks-gke-quickstart- Cloud Run- mkdir deploy-hooks-run-quickstart cd deploy-hooks-run-quickstart
- 建立名為 - skaffold.yaml的檔案,並在當中加入下列內容:- GKE- apiVersion: skaffold/v4beta7 kind: Config manifests: rawYaml: - k8s-pod.yaml deploy: kubectl: {} customActions: - name: predeploy-action containers: - name: predeploy-echo image: ubuntu command: ["/bin/sh"] args: ["-c", 'echo "this is a predeploy action"' ] - name: postdeploy-action containers: - name: postdeploy-echo image: ubuntu command: ["/bin/sh"] args: ["-c", 'echo "this is a postdeploy action"' ]- Cloud Run- apiVersion: skaffold/v4beta7 kind: Config manifests: rawYaml: - service.yaml deploy: cloudrun: {} customActions: - name: predeploy-action containers: - name: predeploy-echo image: ubuntu command: ["/bin/sh"] args: ["-c", 'echo "this is a predeploy action"' ] - name: postdeploy-action containers: - name: postdeploy-echo image: ubuntu command: ["/bin/sh"] args: ["-c", 'echo "this is a postdeploy action"' ]- 這個檔案包含 - customActions:節。這會定義要在部署前後執行的容器 (即掛鉤)。- 如要進一步瞭解這個設定檔,請參閱 - skaffold.yaml參考資料。
- 建立應用程式的定義,也就是 Cloud Run 的服務定義或 GKE 的 Kubernetes 資訊清單。 - GKE- 建立名為 - k8s-pod.yaml的檔案,並在當中加入下列內容:- apiVersion: v1 kind: Pod metadata: name: my-hooks-pod spec: containers: - name: nginx image: my-app-image- 這個檔案是基本的 Kubernetes 資訊清單,會套用至叢集以部署應用程式。要部署的容器映像檔在此設為預留位置 - my-app-image,您建立版本時,會將其替換為特定映像檔。- Cloud Run- 建立名為 - service.yaml的檔案,並在當中加入下列內容:- apiVersion: serving.knative.dev/v1 kind: Service metadata: name: my-hooks-run-service spec: template: spec: containers: - image: my-app-image- 這個檔案是簡單的 Cloud Run 服務定義,用於部署應用程式。要部署的容器映像檔在此設定為預留位置 - my-app-image,您建立版本時,會將其替換為特定映像檔。
- 建立推送管道和目標定義: - 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: actions: ["predeploy-action"] postdeploy: actions: ["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-cluster- Cloud 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: actions: ["predeploy-action"] postdeploy: actions: ["postdeploy-action"] --- apiVersion: deploy.cloud.google.com/v1 kind: Target metadata: name: hooks-staging description: staging Run service run: location: projects/PROJECT_ID/locations/us-central1
- 向 Cloud Deploy 服務註冊管道和目標: - gcloud deploy apply --file=clouddeploy.yaml --region=us-central1 --project=PROJECT_ID - 您現在有一個推送管道和一個目標,可以部署應用程式,並執行預先部署和後期部署工作。 
- 確認管道和目標: - 在 Google Cloud 控制台中,前往 Cloud Deploy 的「Delivery pipelines」(推送管道) 頁面,查看可用推送管道的清單。 - 您剛建立的推送管道會顯示在「目標」欄中,並列出一個目標。  
- 在 Google Cloud 控制台中,前往 Cloud Deploy 的「Delivery pipelines」(推送管道) 頁面,查看推送管道 (「deploy-hooks-demo-app-gke-1」或「deploy-hooks-demo-app-run-1」)。 
- 按一下推送管道的名稱 (「deploy-hooks-demo-app-gke-1」或「deploy-hooks-demo-app-run-1」)。 - 管道視覺化畫面會顯示應用程式的部署狀態。由於管道中只有一個階段,因此視覺化內容只會顯示一個節點。  - 發行內容會列在「推送 pipeline 詳細資料」下方的「發行內容」分頁中。 
- 按一下「發布」分頁標籤,位於「發布管道詳細資料」下方。 
- 按一下推出名稱,即可查看推出詳細資料。  - 「Predeploy」(預先部署) 和「Postdeploy」(後期部署) 會列為工作。 
- 按一下「預先部署」 - 系統隨即會顯示工作執行記錄。 
- 在記錄項目清單中向下捲動,找出 - predeploy-echo並點按。 - 請注意 - textPayload。該字串是在Skaffold 設定的- predeploy-action中設定。
- 按一下「Postdeploy」工作,然後找出 - postdeploy-echo記錄項目。 
- 刪除 GKE 叢集或 Cloud Run 服務: - GKE- gcloud container clusters delete deploy-hooks-cluster --region=us-central1 --project=PROJECT_ID- Cloud Run- gcloud run services delete my-hooks-run-service --region=us-central1 --project=PROJECT_ID
- 刪除推送管道、目標、版本和推出項目: - gcloud deploy delete --file=clouddeploy.yaml --force --region=us-central1 --project=PROJECT_ID
- 刪除 Cloud Deploy 建立的 Cloud Storage bucket。 - 一個結尾是 - _clouddeploy,另一個是- [region].deploy-artifacts.[project].appspot.com。
如果已安裝 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
準備 Skaffold 設定和應用程式資訊清單
Cloud Deploy 會使用 Skaffold 提供詳細資料,說明要部署的內容,以及如何部署至目標。
在本快速入門導覽課程中,您將建立 skaffold.yaml 檔案,其中會識別用於部署範例應用程式的資訊清單,以及部署前後要執行的容器 (部署掛鉤)。
建立推送管道和目標
您可以在一個檔案或不同檔案中定義管道和目標。在本快速入門導覽課程中,您將建立單一檔案。
建立發布版本
「版本」是 Cloud Deploy 的核心資源,代表要部署的變更。推送 pipeline 會定義該版本的生命週期。如要進一步瞭解該生命週期,請參閱 Cloud Deploy 服務架構。
GKE
從 deploy-hooks-gke-quickstart 目錄執行下列指令,建立代表要部署的容器映像檔的 release 資源:
 gcloud deploy releases create test-release-001 \
   --project=PROJECT_ID \
   --region=us-central1 \
   --delivery-pipeline=deploy-hooks-demo-app-gke-1 \
   --images=my-app-image=gcr.io/google-containers/nginx@sha256:f49a843c290594dcf4d193535d1f4ba8af7d56cea2cf79d1e9554f077f1e7aaa
請注意 --images= 旗標,您可以使用這個旗標,將資訊清單中的預留位置 (my-app-image) 換成符合 SHA 資格的特定圖片。Google 建議您以這種方式將資訊清單範本化,並在建立發布版本時使用 SHA 合格的映像檔名稱。
Cloud Run
從 deploy-hooks-run-quickstart 目錄執行下列指令,建立代表要部署的容器映像檔的 release 資源:
 gcloud deploy releases create test-release-001 \
   --project=PROJECT_ID \
   --region=us-central1 \
   --delivery-pipeline=deploy-hooks-demo-app-run-1 \
   --images=my-app-image=us-docker.pkg.dev/cloudrun/container/hello@sha256:95ade4b17adcd07623b0a0c68359e344fe54e65d0cb01b989e24c39f2fcd296a
請注意 --images= 標記,您可以使用這個標記,將服務定義中的預留位置 (my-app-image) 替換為符合 SHA 資格的特定映像檔。Google 建議您以這種方式將服務和工作定義範本化,並在建立版本時使用 SHA 合格的映像檔名稱。
與所有版本一樣 (除非包含 --disable-initial-rollout),Cloud Deploy 也會自動建立推出資源。應用程式會自動部署到為這個發布管道設定的目標。
此外,部署前工作會在應用程式部署前執行,部署後工作則會在部署後執行。
在 Google Cloud 控制台中查看結果
幾分鐘後,版本就會部署至目標執行階段。
我們設定的部署前和部署後掛鉤 (僅為示範用途) 會將字串列印至 Cloud Build 記錄。我們可以查看這些記錄,確認掛鉤是否正常運作。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
大功告成,您已經完成本快速入門導覽課程!