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

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

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

  1. 建立 Skaffold 設定

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

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

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

  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, if any are not already enabled.

    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. Install the Google Cloud CLI.

  7. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  8. To initialize the gcloud CLI, run the following command:

    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, if any are not already enabled.

    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. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    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 服務。顧名思義,這是您的dev服務,對應於推送管道進展中的第一個目標。deploy-run-service-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 集區。如名稱 deploy-run-worker-pool-dev 所示,這是您的 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 集區。顧名思義,這是您的� worker 集區,對應於傳送管道進展中的第二個目標。�deploy-run-worker-pool-prodprod

這些檔案是 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:
      - targetId: run-qsdev
        profiles: [dev]
      - targetId: run-qsprod
        profiles: [prod]
    ---
    
    apiVersion: deploy.cloud.google.com/v1
    kind: Target
    metadata:
      name: run-qsdev
    description: Cloud Run development service
    run:
      location: projects/PROJECT_ID/locations/us-central1
    ---
    
    apiVersion: deploy.cloud.google.com/v1
    kind: Target
    metadata:
      name: run-qsprod
    description: Cloud Run production service
    run:
      location: projects/PROJECT_ID/locations/us-central1
    
  2. 向 Cloud Deploy 服務註冊管道和目標:

    gcloud deploy apply --file=clouddeploy.yaml --region=us-central1 --project=PROJECT_ID
    

    您現在已擁有管道和目標,可將應用程式部署至第一個目標。

  3. 確認管道和目標:

    在 Google Cloud 控制台中,前往 Cloud Deploy 的「Delivery pipelines」(推送管道) 頁面,查看可用推送管道的清單。

    開啟「推送管道」頁面

    系統會顯示您剛建立的推送管道,且兩個目標會列在「目標」欄中。

     Google Cloud 控制台顯示的推送管道

建立發布版本

版本是代表要部署變更的核心 Cloud Deploy 資源。發布管道會定義該版本的生命週期。如要進一步瞭解該生命週期,請參閱「Cloud Deploy 服務架構」。

從 deploy-run-quickstart 目錄執行下列指令,建立代表要部署容器映像檔的 release 資源:

服務

 gcloud deploy releases create test-release-001 \
   --project=PROJECT_ID \
   --region=us-central1 \
   --delivery-pipeline=my-run-demo-app-1 \
   --images=my-app-image=us-docker.pkg.dev/cloudrun/container/hello@sha256:95ade4b17adcd07623b0a0c68359e344fe54e65d0cb01b989e24c39f2fcd296a

工作

 gcloud deploy releases create test-release-001 \
   --project=PROJECT_ID \
   --region=us-central1 \
   --delivery-pipeline=my-run-demo-app-1 \
   --images=my-app-image=us-docker.pkg.dev/cloudrun/container/job@sha256:8eb3f5e72586de6375abe95aa67511c57c61d35fb37d5670e4d68624a68ef916

工作站集區

 gcloud deploy releases create test-release-001 \
   --project=PROJECT_ID \
   --region=us-central1 \
   --delivery-pipeline=my-run-demo-app-1 \
   --skaffold-version=2.17.0 \
   --images=my-app-image=us-docker.pkg.dev/cloudrun/container/hello@sha256:95ade4b17adcd07623b0a0c68359e344fe54e65d0cb01b989e24c39f2fcd296a

請注意 --images= 旗標,您可以使用這個旗標,將服務、工作或工作團隊集區定義中的預留位置 (my-app-image) 換成符合 SHA 資格的特定映像檔。Google 建議您以這種方式將服務和工作定義範本化,並在建立發布版本時使用 SHA 合格的圖片名稱。

--skaffold-version= 旗標會指定用於部署作業的 Skaffold 版本。如要支援 Cloud Run 工作人員集區,請務必使用 Skaffold 2.17 以上版本。

與所有版本一樣 (除非包含 --disable-initial-rollout),Cloud Deploy 也會自動建立 rollout 資源。應用程式會自動部署至程序中的第一個目標。

推送版本

  1. 在「Delivery pipelines」(推送 pipeline) 頁面中,按一下 my-run-demo-app-1 pipeline。

    開啟「推送管道」頁面

    「推送管道詳細資料」頁面會以圖表呈現推送管道的進度。在這個範例中,版本已部署至 run-qsdev 目標。

     Google Cloud 控制台中的推送管道視覺化

  2. 在推送管道圖表的第一個目標中,點選「推送」。

    畫面上會顯示「推送版本」對話方塊,內容包含要推送目標的詳細資料。

  3. 點選「升級」。

    版本現已排入佇列,準備部署至 run-qsprod。部署完成後,推送管道圖表會顯示為已部署:

     Google Cloud 控制台中的推送管道圖表,顯示已部署至正式環境的版本。

在 Google Cloud 控制台中查看結果

  1. 在 Google Cloud 控制台中,前往 Cloud Deploy 的「Delivery pipelines」(推送管道) 頁面,查看 my-run-demo-app-1 推送管道。

    開啟「推送管道」頁面

  2. 點選推送管道名稱「my-run-demo-app-1」。

    管道圖表會顯示應用程式在管道中的進度。

     Google Cloud 控制台中的推送管道視覺化

    發行內容會列在「推送 pipeline 詳細資料」底下的「發行內容」分頁中。

  3. 按一下發行版本名稱 test-release-001。

    推出作業會顯示在「推出作業」下方。點選推出作業即可查看詳細資料,包括部署記錄。

     Google Cloud 控制台中的推出作業

存取 Cloud Run 服務

根據預設,您必須先完成驗證,才能存取新建立的 Cloud Run 服務或工作站集區。請參閱 Cloud Run 的驗證總覽,瞭解如何提供憑證,以及存取服務時需要哪些 Identity and Access Management 設定,才能免除驗證。這不適用於 Cloud Run 工作。

清除所用資源

為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。

  1. 刪除 deploy-qs-dev Cloud Run 服務、工作或工作站集區:

    服務

    gcloud run services delete deploy-run-service-dev --region=us-central1 --project=PROJECT_ID
    

    工作

    gcloud run jobs delete deploy-run-job-dev --region=us-central1 --project=PROJECT_ID
    

    工作站集區

    gcloud run worker-pools delete deploy-run-worker-pool-dev --region=us-central1 --project=PROJECT_ID
    
  2. 刪除 deploy-qs-prod 服務、工作或 worker 集區:

    服務

    gcloud run services delete deploy-run-service-prod --region=us-central1 --project=PROJECT_ID
    

    工作

    gcloud run jobs delete deploy-run-job-prod --region=us-central1 --project=PROJECT_ID
    

    工作站集區

    gcloud run worker-pools delete deploy-run-worker-pool-prod --region=us-central1 --project=PROJECT_ID
    
  3. 刪除推送管道、目標、發布版本和推出項目:

    gcloud deploy delete --file=clouddeploy.yaml --force --region=us-central1 --project=PROJECT_ID
    
  4. 刪除 Cloud Deploy 建立的 Cloud Storage bucket。

    一個結尾為 _clouddeploy,另一個則是 [region].deploy-artifacts.[project].appspot.com。

    開啟 Cloud Storage 瀏覽器頁面

大功告成,您已經完成本快速入門導覽課程!

後續步驟