您可以使用 Workflows 執行 Cloud Run 工作,做為工作流程的一部分,以便進行更複雜的資料處理作業,或以系統化的方式自動調度管理現有工作。
本教學課程說明如何使用 Workflows 執行 Cloud Run 工作,處理儲存在 Cloud Storage bucket 中的事件資料。將事件酬載儲存在 Cloud Storage 值區中,可讓您使用客戶管理的加密金鑰加密資料,但如果將事件資料做為環境變數傳遞至 Cloud Run 作業,則無法加密資料。
下圖提供概要總覽:
目標
教學課程內容:
- 建立 Cloud Run 工作,處理儲存在 Cloud Storage bucket 中的事件資料。
- 部署可執行下列作業的工作流程:
- 以引數形式接收事件。
- 將事件酬載資料寫入 Cloud Storage bucket。
- 使用 Cloud Run Admin API 連接器執行 Cloud Run 工作。
- 建立 Pub/Sub 主題,以便將訊息發布至該主題。本教學課程以 Pub/Sub 事件為例,說明如何使用 Workflows 路由事件,並將事件儲存至 Cloud Storage,以便 Cloud Run 工作處理事件資料。
- 建立 Eventarc 觸發程序,在訊息寫入 Pub/Sub 主題時執行工作流程。
- 將訊息寫入 Pub/Sub 主題,藉此觸發工作流程。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。
開始本教學課程之前,請務必啟用特定 API,並建立使用者管理的服務帳戶。您必須授予服務帳戶必要角色和權限,才能使用 Eventarc 觸發條件傳送事件,並執行工作流程。
請注意,如果您使用 Cloud Shell 試用本教學課程,可能需要為 Compute Engine 預設服務帳戶授予其他角色。詳情請參閱本文的「建立 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run, Cloud Storage, Compute Engine, Eventarc, and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run Admin, Eventarc Event Receiver, Logs Writer, Storage Object Creator, Workflows Invoker.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run, Cloud Storage, Compute Engine, Eventarc, and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run Admin, Eventarc Event Receiver, Logs Writer, Storage Object Creator, Workflows Invoker.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
- 如果您是在 2021 年 4 月 8 日當天或之前啟用 Cloud Pub/Sub 服務代理,請將服務帳戶權杖建立者角色 (
roles/iam.serviceAccountTokenCreator
) 授予服務代理,以支援已驗證的 Pub/Sub 推送要求。否則,系統會預設授予這個角色:- 前往 Google Cloud 控制台的「IAM」IAM頁面。
- 選取「包含 Google 提供的角色授權」核取方塊。
- 在「Name」(名稱) 欄中找到「Cloud Pub/Sub Service Account」(Cloud Pub/Sub 服務帳戶),然後按一下對應資料列中的 「Edit principal」(編輯主體)。
- 按一下 「新增角色」或 「新增其他角色」。
- 在「Select a role」(選取角色) 清單中,篩選「Service Account Token Creator」(服務帳戶憑證建立者),然後選取該角色。
- 按一下 [儲存]。
- 前往 Google Cloud 控制台的「IAM」IAM頁面。
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Cloud Shell 支援本教學課程中的指令列步驟。
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run, Cloud Storage, Compute Engine, Eventarc, and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: roles/eventarc.eventReceiver, roles/logging.logWriter, roles/run.admin, roles/storage.objectCreator, roles/workflows.invoker .
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run, Cloud Storage, Compute Engine, Eventarc, and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: roles/eventarc.eventReceiver, roles/logging.logWriter, roles/run.admin, roles/storage.objectCreator, roles/workflows.invoker .
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
- 如果您是在 2021 年 4 月 8 日當天或之前啟用 Cloud Pub/Sub 服務代理,請將服務帳戶權杖建立者角色 (
roles/iam.serviceAccountTokenCreator
) 授予服務代理,以支援已驗證的 Pub/Sub 推送要求。否則,系統會預設授予這個角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \ --role=roles/iam.serviceAccountTokenCreator
gcloud
將 PROJECT_NUMBER
替換為專案編號。 Google Cloud您可以在 Google Cloud 控制台的「歡迎」頁面找到專案編號,也可以執行下列指令:
gcloud projects describe PROJECT_ID --format='value(projectNumber)'
建立 Cloud Run 工作
本教學課程使用 GitHub 上的範例程式碼。部署指令碼會建構容器映像檔,以建立 Cloud Run 工作。這個指令碼也會建立 Cloud Storage bucket。Cloud Run 工作會讀取儲存在 Cloud Storage bucket 中的任何事件資料,然後列印事件資料。
如果您在 Cloud Shell 中執行部署指令碼,且Compute Engine 預設服務帳戶沒有編輯者角色,請將下列專案角色授予 Compute Engine 預設服務帳戶。(否則,您可以略過這個步驟,並在下一個步驟中繼續複製範例應用程式存放區)。
授予Artifact Registry 寫入者角色 (
roles/artifactregistry.writer
):gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/artifactregistry.writer
將
PROJECT_NUMBER
替換為專案編號。 Google Cloud您可以在 Google Cloud 控制台的「歡迎」頁面找到專案編號,也可以執行下列指令:gcloud projects describe PROJECT_ID --format='value(projectNumber)'
授予 Storage 物件使用者角色 (
roles/storage.objectUser
):gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/storage.objectUser
授予記錄檔記錄寫入者角色 (
roles/logging.logWriter
):gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/logging.logWriter
將範例應用程式存放區複製到本機電腦,即可取得程式碼範例:
git clone https://github.com/GoogleCloudPlatform/workflows-demos.git
或者,您也可以下載 ZIP 格式的範例檔案,
變更為包含範例程式碼的目錄:
cd workflows-demos/cloud-run-jobs-payload-gcs/message-payload-job
執行部署指令碼,建立 Cloud Run 工作:
./deploy-job.sh
這個指令碼會建立名為 message-payload-PROJECT_ID
的 Cloud Storage 值區,其中 PROJECT_ID
是 Google Cloud 專案的 ID。系統也會建立名為 message-payload-job
的 Cloud Run 工作。
部署執行 Cloud Run 工作的工作流程
定義及部署工作流程,執行您剛建立的 Cloud Run 工作。工作流程定義是由一系列步驟組成,這些步驟會使用 Workflows 語法描述。
這項工作流程會接收事件、將事件資料儲存至 Cloud Storage bucket,然後執行 Cloud Run 工作來處理事件資料。
控制台
前往 Google Cloud 控制台的「Workflows」頁面:
按一下「建立」。
輸入新工作流程的名稱,例如
message-payload-workflow
。選擇適當的區域,例如
us-central1
。在「服務帳戶」欄位中,選取您先前建立的服務帳戶。
服務帳戶會做為工作流程的身分。您應該已將下列角色授予服務帳戶:
- Cloud Run 管理員:執行 Cloud Run 工作
- 記錄寫入者:寫入記錄項目
- Storage 物件建立者:在 Cloud Storage 中建立物件
點選「下一步」。
在工作流程編輯器中,輸入下列工作流程定義:
按一下 [Deploy] (部署)。
gcloud
為工作流程建立原始碼檔案:
touch message-payload-workflow.yaml
將下列工作流程定義複製到
message-payload-workflow.yaml
:輸入下列指令來部署工作流程:
gcloud workflows deploy message-payload-workflow \ --location=us-central1 \ --source=message-payload-workflow.yaml \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
更改下列內容:
SERVICE_ACCOUNT_NAME
:您先前建立的服務帳戶名稱PROJECT_ID
:您的Google Cloud 專案 ID
服務帳戶會做為工作流程的身分。您應該已將下列角色授予服務帳戶:
roles/logging.logWriter
:用於寫入記錄項目roles/run.admin
:執行 Cloud Run 工作roles/storage.objectCreator
:在 Cloud Storage 中建立物件
工作流程會執行下列作業:
init
步驟:接受事件做為引數,並設定必要變數。log_event
步驟:使用函式 sys.log 在 Cloud Logging 中建立記錄項目。write_payload_to_gcs
步驟:發出 HTTPPOST
要求,並將事件酬載資料寫入 Cloud Storage 值區檔案。run_job_to_process_payload
步驟:使用 Cloud Run Admin API 連接器方法googleapis.run.v1.namespaces.jobs.run
執行作業。Cloud Storage bucket 和資料檔案名稱會從工作流程以覆寫變數的形式傳遞至工作。finish
步驟:傳回工作執行資訊,做為工作流程的結果。
建立 Pub/Sub 主題
建立 Pub/Sub 主題,以便將訊息發布至該主題。本範例使用 Pub/Sub 事件,說明如何使用 Workflows 轉送事件,並將事件儲存至 Cloud Storage,以便 Cloud Run 工作處理事件資料。
控制台
前往 Google Cloud 控制台的「主題」頁面。
按一下
「建立主題」。在「Topic ID」(主題 ID) 欄位中,輸入主題的 ID,例如
message-payload-topic
。保留「新增預設訂閱項目」選項。
請勿選取其他選項。
點選「建立」。
gcloud
如要使用 ID message-payload-topic
建立主題,請執行 gcloud pubsub topics create
指令:
gcloud pubsub topics create message-payload-topic
建立 Eventarc 觸發條件,將事件轉送至工作流程
如要自動執行工作流程,進而執行 Cloud Run 工作,請建立 Eventarc 觸發條件,回應 Pub/Sub 事件,並將事件傳送至工作流程。每當訊息寫入 Pub/Sub 主題,事件就會觸發工作流程的執行作業。
控制台
前往 Google Cloud 控制台的「Workflows」頁面:
按一下工作流程名稱,例如
message-payload-workflow
。在「工作流程詳細資料」頁面中,按一下
「編輯」。在「Edit workflow」(編輯工作流程) 頁面的「Triggers」(觸發條件) 區段中,按一下「Add new trigger」(新增觸發條件) >「Eventarc」。
「Eventarc trigger」(Eventarc 觸發條件) 窗格隨即開啟。
在「觸發條件名稱」欄位中,輸入觸發條件的名稱,例如
message-payload-trigger
。在「Event provider」(事件提供者) 清單中,選取「Cloud Pub/Sub」。
在「Event」(事件) 清單中,選取「google.cloud.pubsub.topic.v1.messagePublished」。
從「Select a Cloud Pub/Sub topic」(選取 Cloud Pub/Sub 主題) 清單中,選取您先前建立的 Pub/Sub 主題。
在「服務帳戶」欄位中,選取您先前建立的服務帳戶。
服務帳戶會做為觸發程序的 ID。您應該已將下列角色授予服務帳戶:
- Eventarc 事件接收者:接收事件
- 工作流程叫用者:執行工作流程
按一下「儲存觸發條件」。
現在「Edit workflow」(編輯工作流程) 頁面的「Triggers」(觸發條件) 區段中,會顯示 Eventarc 觸發條件。
點選「下一步」。
按一下 [Deploy] (部署)。
gcloud
執行下列指令,建立 Eventarc 觸發條件:
gcloud eventarc triggers create message-payload-trigger \ --location=us-central1 \ --destination-workflow=message-payload-workflow \ --destination-workflow-location=us-central1 \ --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \ --transport-topic=projects/PROJECT_ID/topics/message-payload-topic \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
更改下列內容:
PROJECT_ID
:您的 Google Cloud 專案 IDSERVICE_ACCOUNT_NAME
:先前建立的服務帳戶名稱。
服務帳戶會做為觸發程序的 ID。您應該已將下列角色授予服務帳戶:
roles/eventarc.eventReceiver
:接收事件roles/workflows.invoker
:執行工作流程
觸發工作流程
將訊息發布至 Pub/Sub 主題並產生事件,測試端對端系統。詳情請參閱使用事件或 Pub/Sub 訊息觸發工作流程。
將訊息發布至 Pub/Sub 主題,產生事件:
gcloud pubsub topics publish message-payload-topic --message="Hello World"
系統會將事件傳送至工作流程,該工作流程會記錄事件訊息、將事件資料儲存至 Cloud Storage bucket,並執行 Cloud Run 工作來處理儲存在 Cloud Storage 中的資料。這可能需要一分鐘的時間。
查看工作執行作業,確認 Cloud Run 工作是否如預期執行:
gcloud run jobs executions list --job=message-payload-job
輸出內容中應會顯示新的工作執行作業。
如要查看觸發工作流程所建立的事件相關記錄項目,請執行下列指令:
gcloud logging read "resource.type=cloud_run_job AND textPayload:Payload"
尋找類似下列內容的記錄項目:
textPayload: "Payload: {'message': {'data': 'SGVsbG8gV29ybGQ=', 'messageId': '8834108235224238',\ \ 'publishTime': '2023-09-20T17:07:52.921Z'}, 'subscription': 'projects/MY_PROJECT/subscriptions/eventarc-us-central1-message-payload-trigger-sub-741'}" ... resource: labels: job_name: message-payload-job location: us-central1 project_id: MY_PROJECT type: cloud_run_job textPayload: Processing message payload gs://message-payload-MY_PROJECT/8254002311197919.data.json
您可以查看 Cloud Storage bucket 物件中的事件資料,確認結果是否符合預期。
擷取 bucket 名稱:
gcloud storage ls
輸出結果會與下列內容相似:
gs://message-payload-PROJECT_ID/
列出值區中的物件:
gcloud storage ls gs://message-payload-PROJECT_ID/** --recursive
畫面會顯示如下的輸出內容:
gs://message-payload-PROJECT_ID/OBJECT_ID.data.json
記下
OBJECT_ID
,以便在下一個步驟中使用。將 bucket 中的物件下載為檔案:
gcloud storage cp gs://message-payload-PROJECT_ID/OBJECT_ID.data.json message-event.txt
將
OBJECT_ID
替換為上一個步驟中傳回的 ID。在文字編輯器中開啟
message-event.txt
檔案,寫入檔案的事件主體應類似下列內容:{ "message": { "data": "SGVsbG8gV29ybGQ=", "messageId": "8254002311197919", "publishTime": "2023-09-20T16:54:29.312Z" }, "subscription": "projects/MY_PROJECT/subscriptions/eventarc-us-central1-message-payload-trigger-sub-741" }
請注意,如果從 Base64 格式解碼
SGVsbG8gV29ybGQ=
的資料值,系統會傳回「Hello World」。
清除所用資源
如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有專案,並想保留專案,但不要在本教學課程中新增的變更,請刪除為本教學課程建立的資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除教學課程資源
刪除您在本教學課程中建立的資源:
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-08 (世界標準時間)。