快速入門:從 Git 存放區部署至 Cloud Run

本頁說明如何從 Git 存放區持續部署至 Cloud Run。


如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


如需這項程序的完整操作說明,請參閱「透過 Cloud Build 從 Git 持續部署」。

事前準備

  1. 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.
  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. 如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。

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

  5. 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

  6. 如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。

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

  8. Enable the Cloud Build API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  9. 如果您受到網域限制組織政策限制,專案無法進行未經驗證的呼叫,則必須按照「測試私人服務」一節的說明存取已部署的服務。

  10. 查看 Cloud Run 定價,或使用 Pricing Calculator 估算費用。
  11. 必要的角色

    如要取得完成本快速入門導覽課程所需的權限,請要求管理員授予您下列 IAM 角色:

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

    您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

    使用範本建立存放區

    如要建立存放區,請按照下列步驟操作:

    1. 前往範本存放區:

    2. 按一下「在 Google Cloud 上執行」

    3. 選取「建立新的存放區」

    4. 在表單中,輸入存放區名稱

    5. 按一下「Create repository from template」(透過範本建立存放區)

    從存放區部署

    如要從存放區部署:

    1. 前往 Google Cloud 控制台的 Cloud Run 頁面:

      前往 Cloud Run

    2. 從選單中選取「服務」,然後按一下「部署容器」

    3. 在「建立服務」中,確認已選取「從存放區持續部署」選項。

    4. 在表單中,按一下「Set up Cloud Build」(設定 Cloud Build)

    5. 在右側面板中:

      1. 在「Repository」下方,選取新建立的存放區。如果尚未完成驗證,系統可能會要求您驗證。請使用「Repository Provider」(存放區供應商) 欄位下方的「authenticate」(驗證) 連結。

      2. 確認您同意 GitHub 和 Google Cloud 互動功能。

      3. 點選「下一步」

      4. 在「Build Type」(建構類型) 下方,選取「Buildpacks」(建構套件)Google Cloud

      5. 按一下 [儲存]

    6. 在「建立服務」表單中:

      1. 確認服務名稱。系統會自動填入存放區名稱。

      2. 在「Region」(區域) 下拉式選單中,選取要使用服務的區域

      3. 在「Authentication」(驗證) 下方,選取「Allow public access」(允許公開存取)。 如果您沒有權限 (Cloud Run 管理員角色) 可選取這個選項,服務會部署並要求驗證。

      4. 按一下「建立」,將範例存放區部署到 Cloud Run,然後等待部署作業完成。

    7. 按一下顯示的網址連結來執行部署好的容器。

    Cloud Run 位置

    Cloud Run 具有「地區性」,這表示執行 Cloud Run 服務的基礎架構位於特定地區,並由 Google 代管,可為該地區內所有區域提供備援功能。

    選擇 Cloud Run 服務的執行地區時,請將延遲時間、可用性或耐用性需求做為主要考量。一般而言,您可以選擇最靠近使用者的地區,但您應考量 Cloud Run 服務所使用的其他 Google Cloud 產品位置。使用分散在不同位置的 Google Cloud 產品,可能會影響服務的延遲時間和費用。

    Cloud Run 可在下列地區使用:

    採用級別 1 定價

    採用級別 2 定價

    • africa-south1 (約翰尼斯堡)
    • asia-east2 (香港)
    • asia-northeast3 (韓國首爾)
    • asia-southeast1 (新加坡)
    • asia-southeast2 (雅加達)
    • asia-south2 (印度德里)
    • australia-southeast1 (雪梨)
    • australia-southeast2 (墨爾本)
    • europe-central2 (波蘭華沙)
    • europe-west10 (柏林)
    • europe-west12 (杜林)
    • europe-west2 (英國倫敦) 節能綠葉圖示 二氧化碳排放量低
    • europe-west3 (德國法蘭克福)
    • europe-west6 (瑞士蘇黎世) 節能綠葉圖示 二氧化碳排放量低
    • me-central1 (杜哈)
    • me-central2 (達曼)
    • northamerica-northeast1 (蒙特婁) 節能綠葉圖示 二氧化碳排放量低
    • northamerica-northeast2 (多倫多) 節能綠葉圖示 二氧化碳排放量低
    • southamerica-east1 (巴西聖保羅) 節能綠葉圖示 二氧化碳排放量低
    • southamerica-west1 (智利聖地牙哥) 節能綠葉圖示 二氧化碳排放量低
    • us-west2 (洛杉磯)
    • us-west3 (鹽湖城)
    • us-west4 (拉斯維加斯)

    如果您已建立 Cloud Run 服務,即可在 Google Cloud 控制台的 Cloud Run 資訊主頁中查看地區。

    更新服務

    如要變更服務:

    1. 將修訂內容推送至存放區的主要分支版本。

    2. 重新整理 Cloud Run 服務,查看更新狀態。

    Cloud Run 會根據需求自動調度容器執行個體,您只需要支付處理要求期間使用的 CPU、記憶體和網路費用。

    清除所用資源

    為避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除您透過本快速入門導覽課程部署的所有資源。

    刪除存放區

    部署的服務閒置時,Cloud Run 不會向您收費。不過,您可能仍須支付在 Artifact Registry 中儲存容器映像檔的費用。如要刪除 Artifact Registry 存放區,請按照 Artifact Registry 說明文件中的「刪除存放區」一節操作。

    刪除服務

    Cloud Run 服務收到要求後才會開始計費。如要刪除 Cloud Run 服務,請按照下列步驟操作:

    控制台

    如要刪除服務:

    1. 前往 Google Cloud 控制台的 Cloud Run「服務」頁面:

      前往 Cloud Run

    2. 在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。

    3. 按一下「Delete」(刪除)。如此便會刪除服務的所有修訂版本。

    gcloud

    如要刪除服務,請執行下列指令:

    gcloud run services delete SERVICE --region REGION

    更改下列內容:

    • SERVICE:服務名稱。
    • REGION:服務的 Google Cloud 區域。

    刪除測試專案

    刪除專案後,系統就會停止對該專案中的所有資源收取費用。 Google Cloud 如要釋出專案中的所有 Google Cloud 資源,請按照下列步驟操作:

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    後續步驟