快速入門:使用 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.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with 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_ID
with 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:
gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/run.sourceDeveloper, roles/run.admin, roles/logging.viewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: your project ID.USER_IDENTIFIER
: the identifier for your user account—for example,myemail@example.com
.ROLE
: the IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with 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_ID
with 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:
gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/run.sourceDeveloper, roles/run.admin, roles/logging.viewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: your project ID.USER_IDENTIFIER
: the identifier for your user account—for example,myemail@example.com
.ROLE
: the IAM role that you grant to your user account.
- 如要為 Cloud Run 服務設定預設專案:
將 PROJECT_ID 替換為您為本快速入門導覽課程建立的專案名稱。gcloud config set project PROJECT_ID
-
請確認您已獲得服務身分的服務帳戶使用者角色。根據預設,服務身分是 Compute Engine 預設服務帳戶。
授予角色
如要授予服務身分資源的存取權,請使用
gcloud iam service-accounts add-iam-policy-binding
指令,並將醒目顯示的變數替換為適當的值:gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \ --member="PRINCIPAL" \ --role="roles/iam.serviceAccountUser"
更改下列內容:
- SERVICE_ACCOUNT_EMAIL:您用來做為服務身分的服務帳戶電子郵件地址,例如:
- Compute Engine 預設服務帳戶:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
- 您建立的服務帳戶:
SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
- Compute Engine 預設服務帳戶:
- PRINCIPAL:使用者 ID。這通常是 Google 帳戶的電子郵件地址。
- SERVICE_ACCOUNT_EMAIL:您用來做為服務身分的服務帳戶電子郵件地址,例如:
- 將下列 IAM 角色授予 Cloud Build 服務帳戶。
<0x
按一下即可查看 Cloud Build 服務帳戶的必要角色
除非您覆寫這項行為,否則 Cloud Build 會自動使用 Compute Engine 預設服務帳戶做為預設的 Cloud Build 服務帳戶,建構您的原始碼和 Cloud Run 資源。如要讓 Cloud Build 建構來源,請要求管理員將 Cloud Run Builder (
roles/run.builder
) 授予專案中的 Compute Engine 預設服務帳戶:gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/run.builder
請將
PROJECT_NUMBER
替換為專案編號,並將PROJECT_ID
替換為專案 ID。 Google CloudGoogle Cloud如需如何找出專案 ID 和專案編號的詳細操作說明,請參閱「建立及管理專案」。將 Cloud Run 建構者角色授予 Compute Engine 預設服務帳戶後,需要幾分鐘才能傳播。
- 查看 Cloud Run 定價,或使用 Pricing Calculator 估算費用。
建立一個新目錄並命名為
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 nodejs22 \ --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 建立觸發條件」。
編寫範例函式
如要撰寫申請書,請按照下列步驟操作:
Node.js
Python
Go
Java
Ruby
PHP
.NET
部署函式
重要事項:本快速入門導覽課程假設您在用於快速入門導覽課程的專案中,具備擁有者或編輯者角色。否則,請參閱 Cloud Run Source Developer 角色,瞭解從來源部署 Cloud Run 資源所需的權限。
如要部署 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