快速入門:使用 gcloud CLI 部署 Cloud Run 函式
本頁說明如何使用 gcloud CLI 部署 HTTP Cloud Run 函式。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您在專案中獲派角色,即可選取該專案。
-
建立專案:如要建立專案,您需要「專案建立者」角色 (
roles/resourcemanager.projectCreator),其中包含resourcemanager.projects.create權限。瞭解如何授予角色。
-
建立 Google Cloud 專案:
gcloud projects create PROJECT_ID
將
PROJECT_ID替換為要建立的專案名稱。 Google Cloud -
選取您建立的 Google Cloud 專案:
gcloud config set project PROJECT_ID
將
PROJECT_ID替換為 Google Cloud 專案名稱。
-
如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。
如果尚未啟用 Artifact Registry、Cloud Build、Cloud Run Admin API 和 Cloud Logging API,請啟用這些 API:
啟用 API 時所需的角色
如要啟用 API,您必須具備
serviceusage.services.enable權限。如果您建立了專案,可能已透過「擁有者」角色 (roles/owner) 取得這項權限。否則,您可以透過「服務使用情形管理員」角色 (roles/serviceusage.serviceUsageAdmin) 取得這項權限。瞭解如何授予角色。gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您在專案中獲派角色,即可選取該專案。
-
建立專案:如要建立專案,您需要「專案建立者」角色 (
roles/resourcemanager.projectCreator),其中包含resourcemanager.projects.create權限。瞭解如何授予角色。
-
建立 Google Cloud 專案:
gcloud projects create PROJECT_ID
將
PROJECT_ID替換為要建立的專案名稱。 Google Cloud -
選取您建立的 Google Cloud 專案:
gcloud config set project PROJECT_ID
將
PROJECT_ID替換為 Google Cloud 專案名稱。
-
如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。
如果尚未啟用 Artifact Registry、Cloud Build、Cloud Run Admin API 和 Cloud Logging API,請啟用這些 API:
啟用 API 時所需的角色
如要啟用 API,您必須具備
serviceusage.services.enable權限。如果您建立了專案,可能已透過「擁有者」角色 (roles/owner) 取得這項權限。否則,您可以透過「服務使用情形管理員」角色 (roles/serviceusage.serviceUsageAdmin) 取得這項權限。瞭解如何授予角色。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 估算費用。
必要的角色
如要取得完成本快速入門導覽課程所需的權限,請要求管理員授予您下列 IAM 角色:
- 專案的 Cloud Run 管理員 (
roles/run.admin) - 專案的 Cloud Run 原始碼開發者 (
roles/run.sourceDeveloper) - 服務身分上的服務帳戶使用者 (
roles/iam.serviceAccountUser) - 專案的記錄檢視器 (
roles/logging.viewer)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
授予 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
建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld在
helloworld目錄中建立package.json檔案,指定 Node.js 依附元件:{ "name": "nodejs-docs-samples-functions-hello-world-get", "version": "0.0.1", "private": true, "license": "Apache-2.0", "author": "Google Inc.", "repository": { "type": "git", "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" }, "engines": { "node": ">=16.0.0" }, "scripts": { "test": "c8 mocha -p -j 2 test/*.test.js --timeout=6000 --exit" }, "dependencies": { "@google-cloud/functions-framework": "^3.1.0" }, "devDependencies": { "c8": "^10.0.0", "gaxios": "^6.0.0", "mocha": "^10.0.0", "wait-port": "^1.0.4" } }
在
helloworld目錄中建立index.js檔案,並加入下列 Node.js 範例:import { http } from '@google-cloud/functions-framework'; // Register an HTTP function with the Functions Framework that executes // when you make an HTTP request to the deployed function's endpoint. http('helloGET', (req, res) => { res.send('Hello World!'); });
Python
建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld在
helloworld目錄中建立requirements.txt檔案,指定 Python 依附元件:這會新增範例所需的套件。
在
helloworld目錄中建立main.py檔案,並加入下列 Python 範例:
Go
建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld建立
go.mod檔案來宣告 Go 模組:在
helloworld目錄中建立hello_http.go檔案,並加入以下 Go 程式碼範例:
Java
建立一個新目錄並命名為
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 依附元件:
Ruby
建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld建立名為
app.rb的檔案,然後將下列程式碼貼入該檔案:建立一個檔案並命名為
Gemfile,然後將下列內容複製到檔案中:如果未安裝 Bundler 2.0 以上版本,請安裝 Bundler。
執行下列指令,產生
Gemfile.lock檔案:bundle install
PHP
建立一個新目錄並命名為
helloworld,然後將目錄變更為該目錄:mkdir helloworld cd helloworld建立名為
index.php的檔案,然後將下列程式碼貼入該檔案:如果您未使用 Cloud Shell,請建立
composer.json檔案,然後將下列程式碼貼入該檔案:
.NET
安裝 .NET SDK。
使用 dotnet 指令,從控制台建立新的空白網頁專案。
dotnet new web -o helloworld-csharp將目錄變更為
helloworld-csharp:將專案檔案
helloworld-csharp.csproj中的程式碼範例替換成下列程式碼:將
Program.cs檔案中的程式碼範例替換為下列程式碼:
部署函式
如要部署 Cloud Run 函式,請按照下列步驟操作:
如要部署函式,請在包含程式碼範例的目錄中執行下列指令:
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 python314 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。Go
gcloud run deploy go-http-function \ --source . \ --function HelloGet \ --base-image go127 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。Java
在包含
pom.xml檔案的目錄中執行下列指令:gcloud run deploy java-http-function \ --source . \ --function functions.HelloWorld \ --base-image java25 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。Ruby
gcloud run deploy ruby-http-function \ --source . \ --function hello_get \ --base-image ruby40 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。PHP
gcloud run deploy php-http-function \ --source . \ --function helloGet \ --base-image php85 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。.NET
gcloud run deploy csharp-http-function \ --source . \ --function HelloWorld.Function \ --base-image dotnet10 \ --region REGION \ --allow-unauthenticated將 REGION 替換為要部署函式的服務 Google Cloud 區域。例如:
europe-west1。部署完成後,Google Cloud CLI 會顯示服務執行的網址。在瀏覽器中開啟網址,即可查看函式的輸出內容。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除您透過本快速入門導覽課程部署的所有資源。
刪除存放區
部署的服務閒置時,Cloud Run 不會向您收費。 不過,您可能仍須支付將容器映像檔儲存至 Artifact Registry 的費用。如要刪除 Artifact Registry 存放區,請按照 Artifact Registry 說明文件中的「刪除存放區」一節操作。
刪除服務
Cloud Run 服務收到要求後才會產生費用。 如要刪除 Cloud Run 服務,請按照下列其中一個步驟操作:
控制台
如要刪除服務:
前往 Google Cloud 控制台的 Cloud Run「Services」(服務) 頁面:
在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。
按一下「Delete」(刪除)。這會刪除服務的所有修訂版本。
gcloud
如要刪除服務,請執行下列指令:
gcloud run services delete SERVICE --region REGION
更改下列內容:
- SERVICE:服務名稱。
- REGION:服務的 Google Cloud 區域。
刪除測試專案
刪除 Google Cloud 專案後,系統會停止收取該專案中所有資源的費用。如要釋出專案中的所有 Google Cloud 資源,請按照下列步驟操作:
刪除 Google Cloud 專案:
gcloud projects delete PROJECT_ID
後續步驟
如要使用 Google Cloud 控制台將範例函式部署至 Cloud Run,請參閱快速入門:使用 Google Cloud 控制台將函式部署至 Cloud Run。
如要使用 Google Cloud 控制台和 Google Cloud CLI 部署函式及建立觸發條件,請參閱「部署函式」。
如要查看及刪除現有函式,請參閱「管理服務修訂版本」。
如要在自己的工具鍊中建構函式容器,並將其部署至 Cloud Run,請參閱「建構函式」。
如要使用 Eventarc 建立觸發條件,請參閱「使用 Eventarc 建立觸發條件」。