快速入門:使用 gcloud CLI 部署 Cloud Run 函式
本頁面說明如何使用 gcloud CLI 部署 HTTP Cloud Run 函式。
事前準備
- 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.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging 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.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging 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.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com - 如要為 Cloud Run 服務設定預設專案:
將 PROJECT_ID 替換為您為本快速入門導覽課程建立的專案名稱。gcloud config set project PROJECT_ID
- 查看 Cloud Run 定價,或使用 Pricing Calculator 估算費用。
-
專案的「Cloud Run 原始碼開發人員」 (
roles/run.sourceDeveloper) -
服務帳戶使用者 (
roles/iam.serviceAccountUser) 服務身分 -
專案的記錄檢視器 (
roles/logging.viewer) 建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld在
helloworld目錄中建立package.json檔案,指定 Node.js 依附元件:在
helloworld目錄中建立index.js檔案,並加入下列 Node.js 範例:建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld在
helloworld目錄中建立requirements.txt檔案,指定 Python 依附元件:這會新增範例所需的套件。
在
helloworld目錄中建立main.py檔案,並加入下列 Python 範例:建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld建立
go.mod檔案來宣告 Go 模組:在
helloworld目錄中建立hello_http.go檔案,並加入下列 Go 程式碼範例:建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld建立下列專案結構,內含來源目錄和來源檔案:
mkdir -p ~/helloworld/src/main/java/functions touch ~/helloworld/src/main/java/functions/HelloWorld.java使用下列 Java 程式碼範例更新
HelloWorld.java檔案:在
helloworld目錄中建立pom.xml檔案,並新增下列 Java 依附元件:建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld建立名為
app.rb的檔案,將下列程式碼貼入其中:建立一個檔案並命名為
Gemfile,然後將下列內容複製到檔案中:如果尚未安裝 Bundler 2.0 以上版本,請安裝 Bundler。
執行下列指令來產生
Gemfile.lock檔案:bundle install建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld建立名為
index.php的檔案,將下列程式碼貼入其中:如果您未使用 Cloud Shell,請建立
composer.json檔案,並將下列程式碼貼入其中:安裝 .NET SDK。
在主控台中,使用 dotnet 指令建立新的空白 Web 專案。
dotnet new web -o helloworld-csharp將目錄變更為
helloworld-csharp:將專案檔案
helloworld-csharp.csproj中的程式碼範例替換成下列程式碼:將
Program.cs檔案中的程式碼範例替換為下列程式碼:在包含範例程式碼的目錄中執行下列指令,即可部署函式:
Node.js
gcloud run deploy nodejs-http-function \ --source . \ --function helloGET \ --base-image nodejs24 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。Python
gcloud run deploy python-http-function \ --source . \ --function hello_get \ --base-image python313 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。Go
gcloud run deploy go-http-function \ --source . \ --function HelloGet \ --base-image go125 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。Java
在包含
pom.xml檔案的目錄中執行下列指令:gcloud run deploy java-http-function \ --source . \ --function functions.HelloWorld \ --base-image java21 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。Ruby
gcloud run deploy ruby-http-function \ --source . \ --function hello_get \ --base-image ruby34 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。PHP
gcloud run deploy php-http-function \ --source . \ --function helloGet \ --base-image php84 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。.NET
gcloud run deploy csharp-http-function \ --source . \ --function HelloWorld.Function \ --base-image dotnet8 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。部署完成後,Google Cloud CLI 會顯示服務的執行網址。在瀏覽器中開啟該網址,即可查看函式的輸出內容。
前往 Google Cloud 控制台的 Cloud Run「服務」頁面:
在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。
按一下「Delete」(刪除)。如此便會刪除服務的所有修訂版本。
- SERVICE:服務名稱。
- REGION:服務的 Google Cloud 區域。
如要使用 Google Cloud 控制台將範例函式部署至 Cloud Run,請參閱快速入門導覽課程:使用 Google Cloud 控制台將函式部署至 Cloud Run。
如要使用 Google Cloud 控制台和 Google Cloud CLI 部署函式及建立觸發條件,請參閱「部署函式」。
如要查看及刪除現有函式,請參閱「管理服務修訂版本」。
如要在自己的工具鍊中建構函式容器,並將其部署到 Cloud Run,請參閱「建構函式」。
如要使用 Eventarc 建立觸發條件,請參閱「使用 Eventarc 建立觸發條件」。
必要的角色
如要取得完成本快速入門導覽課程所需的權限,請要求管理員授予您下列 IAM 角色:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
授予 Cloud Build 服務帳戶專案存取權
除非您覆寫這項行為,否則 Cloud Build 會自動使用 Compute Engine 預設服務帳戶做為預設的 Cloud Build 服務帳戶,建構您的原始碼和 Cloud Run 資源。
如要讓 Cloud Build 建構來源,請在專案中將 Cloud Run 建構者 (roles/run.builder) 角色授予 Cloud Build 服務帳戶:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \ --role=roles/run.builder
將 PROJECT_ID 替換為您的 Google Cloud專案 ID,並將 SERVICE_ACCOUNT_EMAIL_ADDRESS 替換為 Cloud Build 服務帳戶的電子郵件地址。如果您使用 Compute Engine 預設服務帳戶做為 Cloud Build 服務帳戶,請使用下列格式的服務帳戶電子郵件地址:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
將 PROJECT_NUMBER 替換為您的專案編號。 Google Cloud
如需如何找出專案 ID 和專案編號的詳細操作說明,請參閱「建立與管理專案」。
授予 Cloud Run 建構工具角色需要幾分鐘才能傳播。
編寫範例函式
如要撰寫申請書,請按照下列步驟操作:
Node.js
Python
Go
Java
Ruby
PHP
.NET
部署函式
如要部署 Cloud Run 函式,請按照下列步驟操作:
清除所用資源
為避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除您透過本快速入門導覽課程部署的所有資源。
刪除存放區
部署的服務閒置時,Cloud Run 不會向您收費。不過,您可能仍須支付在 Artifact Registry 中儲存容器映像檔的費用。如要刪除 Artifact Registry 存放區,請按照 Artifact Registry 說明文件中的「刪除存放區」一節操作。
刪除服務
Cloud Run 服務收到要求後才會開始計費。如要刪除 Cloud Run 服務,請按照下列步驟操作:
控制台
如要刪除服務:
gcloud
如要刪除服務,請執行下列指令:
gcloud run services delete SERVICE --region REGION
更改下列內容:
刪除測試專案
刪除專案後,系統就會停止對該專案中的所有資源收取費用。 Google Cloud 如要釋出專案中的所有 Google Cloud 資源,請按照下列步驟操作:
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID