使用 Cloud Code for VS Code 部署 Cloud Run 服務

本文說明如何開始使用 Cloud Code。

包括從「Hello, world」應用程式範例建立服務、使用 Cloud Code 公開部署服務,以及查看有效服務資源的狀態。

事前準備

  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. Enable the Cloud Run 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

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

  7. 如要使用現有專案完成本指南,請確認您擁有完成本指南所需的權限。如果您已建立新專案,表示已具備必要權限。

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

  9. Enable the Cloud Run 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

  10. 安裝 Git。 需要使用 Git 才能將範例複製到電腦。
  11. 安裝 Docker。 您必須使用 Docker 建構範例程式碼。確認本機使用者帳戶可以建構容器。
  12. 如果尚未安裝 Cloud Code 外掛程式,請先安裝。
  13. 必要的角色

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

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

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

建立服務

如要建立範例服務應用程式,請按照下列步驟操作:

  1. 在 Cloud Code 狀態列中,按一下有效專案名稱。

    狀態列中的有效專案名稱

  2. 在隨即顯示的「快速挑選」選單中,選取「New Application」(新增應用程式)

  3. 選取「Cloud Run application」做為應用程式類型。

  4. 從範例應用程式清單中,選取您偏好語言的應用程式。

    舉例來說,選擇「Node.js: Cloud Run」會在 Node.js 中建立「Hello, world」服務應用程式。

  5. 確認服務原始碼的檔案系統位置 (或視需要修改),然後按一下「Create New Application」(建立新的應用程式)

    VS Code 會開啟所選服務的 README.md 檔案預覽畫面。

部署至 Cloud Run

如要將服務部署到 Cloud Run,請按照下列步驟操作:

  1. 開啟指令面板 (按下 Ctrl/Cmd+Shift+P 鍵,或依序點選「View」(瀏覽) >「Command Palette」(指令面板)),然後執行「Deploy to Cloud Run」(部署至 Cloud Run) 指令。

  2. 在「Deploy to Cloud Run」窗格中,設定 Google Cloud 專案。

  3. 接受其餘預設設定,然後按一下「部署」

    Cloud Code 會建立映像檔並推送至儲存庫,然後將服務部署到 Cloud Run。

  4. 如要查看執行中的服務,請開啟「部署至 Cloud Run」對話方塊頂端顯示的網址。

查看資源狀態

您可以使用 Cloud Run 服務瀏覽器查看 Cloud Run 服務的狀態:

  1. 在 VS Code 活動列中,按一下 「Cloud Code」
  2. 展開「Cloud Run」部分,即可查看 Cloud Run 服務。
  3. 按一下服務即可查看其屬性。

查看記錄

您也可以使用 Cloud Code 隨附的記錄檢視器,查看已部署服務的記錄。

如要查看記錄,請按照下列步驟操作:

  1. 在 VS Code 活動列中,按一下 「Cloud Code」
  2. 展開「Cloud Run」部分,即可查看 Cloud Run 服務。
  3. 在 Cloud Run 服務上按一下滑鼠右鍵,然後點選「查看記錄檔」

清除所用資源

如要刪除專案:

  1. 前往 Google Cloud 控制台的「Projects」(專案) 頁面:

    前往「Projects」(專案) 頁面

  2. 選取您在本快速入門導覽課程所建立的專案,然後按一下「Delete」(刪除)

  3. 輸入專案 ID 以確認,然後點選「Shut down」(關閉)

    接著,系統就會關閉專案並排定刪除時間。

如果您使用現有專案,並想刪除為本快速入門導覽課程建立的資源,請按照下列步驟操作:

  • 容器映像檔:雖然 Cloud Run 在服務未用時不會產生費用,但將容器映像檔儲存於 Artifact Registry 仍可能會產生費用。如需刪除圖片的步驟,請參閱「刪除圖片」一文。

  • Cloud Run 服務:如要刪除 Cloud Run 服務,請使用導覽選單前往Google Cloud 控制台中的 Cloud Run。選取要刪除的服務,然後按一下「刪除」

後續步驟