使用 Cloud Deploy 將應用程式部署至 Cloud Run

本頁面說明如何使用 Cloud Deploy,將名為 hello 的範例應用程式映像檔,依序交付給兩個 Cloud Run 服務、兩個 Cloud Run 工作,或兩個 Cloud Run 工作人員集區。

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

  1. 建立 Skaffold 設定

  2. 為兩項 Cloud Run 服務、兩項工作或兩個工作站集區建立設定檔。

    這些檔案會定義服務或工作,並指定要部署的 (預先建構) 容器映像檔。

  3. 定義 Cloud Deploy 推送管道和部署目標,這些目標會指向兩個服務、兩個工作或兩個工作站集區。

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

  5. 將版本推送至第二個目標。

  6. 在 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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Cloud Deploy, Cloud Build, 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

  6. 安裝 Google Cloud CLI。

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

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

    gcloud init
  9. 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

  10. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  12. Enable the Cloud Deploy, Cloud Build, 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

  13. 安裝 Google Cloud CLI。

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

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

    gcloud init

必要的角色

為確保 Cloud Deploy 服務帳戶具備執行 Cloud Deploy 作業及部署至 Cloud Run 的必要權限,請管理員在專案中將下列 IAM 角色授予 Cloud Deploy 服務帳戶:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

管理員或許也能透過自訂角色或其他預先定義的角色,授予 Cloud Deploy 服務帳戶必要權限。

準備 Skaffold 設定

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

在本快速入門導覽中,您會建立 skaffold.yaml 檔案,識別要用於部署範例應用程式的 Cloud Run 服務或工作定義。

  1. 開啟終端機視窗。

  2. 建立名為 deploy-run-quickstart 的新目錄,然後前往該目錄。

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

    服務

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

    工作

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

    工作站集區

    apiVersion: skaffold/v4beta7
    kind: Config
    metadata:
      name: deploy-run-quickstart
    profiles:
    - name: dev
      manifests:
        rawYaml:
        - run-worker-pool-dev.yaml
    - name: prod
      manifests:
        rawYaml:
        - run-worker-pool-prod.yaml
    deploy:
      cloudrun: {}
    

    這個檔案是基本的 Skaffold 設定,用於識別 Cloud Run 服務、工作或工作站集區。如要進一步瞭解這個檔案,請參閱 skaffold.yaml 參考資料

準備 Cloud Run 服務、工作或 worker 集區

在本快速入門導覽課程中,您將在同一個專案中建立兩個不同的 Cloud Run 服務、兩個 Cloud Run 工作,或兩個 Cloud Run worker 集區。Cloud Deploy 也支援跨多個專案部署。此外,我們使用 Skaffold 設定檔,讓您在同一個專案中擁有兩個服務、工作或工作站集區。使用不同專案時,您可能不需要使用 Skaffold 設定檔。

服務

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

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

    這個檔案定義了 Cloud Run 服務。如名稱 deploy-run-service-dev 所示,這是您的 dev 服務,對應於推送管道進度中的第一個目標。

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

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

    這個檔案定義了另一個 Cloud Run 服務,且如名稱 deploy-run-service-prod 所示,這是您的 prod 服務,對應於推送管道進度中的第二個目標。

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

工作

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

    apiVersion: run.googleapis.com/v1
    kind: Job
    metadata:
      name: deploy-run-job-dev
    spec:
      template:
        spec:
          template:
            spec:
              containers:
              - image: my-app-image
    

    這個檔案定義了 Cloud Run 工作。如名稱 deploy-run-job-dev 所示,這是您的 dev 工作,對應於推送管道進度中的第一個目標。

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

    apiVersion: run.googleapis.com/v1
    kind: Job
    metadata:
      name: deploy-run-job-prod
    spec:
      template:
        spec:
          template:
            spec:
              containers:
              - image: my-app-image
    

    這個檔案定義了另一個 Cloud Run 工作。顧名思義,這是您的prod工作,對應於推送管道階段中的第二個目標。deploy-run-job-prod

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

工作站集區

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

    apiVersion: run.googleapis.com/v1
    kind: WorkerPool
    metadata:
      name: deploy-run-worker-pool-dev
      annotations:
        run.googleapis.com/launch-stage: BETA
    spec:
      template:
        spec:
          containers:
          - image: my-app-image
    

    這個檔案定義了 Cloud Run worker 集區。顧名思義,這是您的 dev 工作站集區,對應於推送管道進度中的第一個目標。deploy-run-worker-pool-dev

  2. 建立名為 run-worker-pool-prod.yaml 的檔案,並在當中加入下列內容:

    apiVersion: run.googleapis.com/v1
    kind: WorkerPool
    metadata:
      name: deploy-run-worker-pool-prod
      annotations:
        run.googleapis.com/launch-stage: BETA
    spec:
      template:
        spec:
          containers:
          - image: my-app-image
    

    這個檔案定義了另一個 Cloud Run worker 集區。如名稱 deploy-run-worker-pool-prod 所示,這是您的prod工作站集區,對應於傳送管道進度中的第二個目標。

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

建立推送管道和目標

您可以在一個或多個檔案中定義管道和目標。在本快速入門導覽課程中,您將建立單一檔案。

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

    apiVersion: deploy.cloud.google.com/v1
    kind: DeliveryPipeline
    metadata:
      name: my-run-demo-app-1
    description: main application pipeline
    serialPipeline:
      stages: