Menjalankan alur kerja menggunakan Library Klien Cloud
Panduan memulai ini menunjukkan cara menjalankan alur kerja dan melihat hasil eksekusi menggunakan Library Klien Cloud.
Untuk mengetahui informasi selengkapnya tentang cara menginstal Library Klien Cloud dan menyiapkan lingkungan pengembangan, lihat Ringkasan library klien Workflows.
Anda dapat menyelesaikan langkah-langkah berikut menggunakan Google Cloud CLI di terminal atau Cloud Shell.
Sebelum memulai
Batasan keamanan yang ditentukan oleh organisasi mungkin mencegah Anda menyelesaikan langkah-langkah berikut. Untuk mengetahui informasi pemecahan masalah, lihat Mengembangkan aplikasi di lingkungan yang terbatas Google Cloud .
- 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.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
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.
-
Jika Anda menggunakan project yang sudah ada untuk panduan ini, pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini. Jika berhasil membuat project baru, berarti Anda sudah memiliki izin yang diperlukan.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
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 workflows.googleapis.com
-
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
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.
-
Jika Anda menggunakan project yang sudah ada untuk panduan ini, pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini. Jika berhasil membuat project baru, berarti Anda sudah memiliki izin yang diperlukan.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
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 workflows.googleapis.com
-
Set up authentication:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAMEwith a name for the service account. -
Grant the
roles/logging.logWriterIAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/logging.logWriter
Replace the following:
SERVICE_ACCOUNT_NAME: the name of the service accountPROJECT_ID: the project ID where you created the service account
-
Ensure that you have the Create Service Accounts IAM role
(
- Jika diperlukan, download dan instal alat pengelolaan kode sumber Git.
-
Service Account User (
roles/iam.serviceAccountUser) -
Workflows Admin (
roles/workflows.admin) Buat file teks dengan nama file
myFirstWorkflow.yamldengan konten berikut:Setelah membuat alur kerja, Anda dapat men-deploy-nya, tetapi jangan jalankan alur kerja:
gcloud workflows deploy myFirstWorkflow \ --source=myFirstWorkflow.yaml \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --location=CLOUD_REGION
Ganti
CLOUD_REGIONdengan lokasi yang didukung untuk alur kerja. Wilayah default yang digunakan dalam contoh kode adalahus-central1.Clone repositori aplikasi contoh ke komputer lokal Anda:
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Ubah ke direktori yang berisi kode contoh Workflows:
C#
cd dotnet-docs-samples/workflows/api/Workflow.Samples/
Go
cd golang-samples/workflows/executions/
Java
cd java-docs-samples/workflows/cloud-client/
Node.js
cd nodejs-docs-samples/workflows/quickstart/
Python
cd python-docs-samples/workflows/cloud-client/
Lihat kode contoh: Setiap aplikasi contoh melakukan hal berikut:
- Menyiapkan Library Klien Cloud untuk Workflows.
- Menjalankan alur kerja.
- Melakukan polling eksekusi alur kerja (menggunakan backoff eksponensial) hingga eksekusi berakhir.
- Mencetak hasil eksekusi.
C#
Go
Java
Node.js
Python
Untuk menjalankan contoh, instal dependensi terlebih dahulu:
C#
dotnet restore
Go
go mod download
Java
mvn compile
Node.js
npm install -D tsx
Python
pip3 install -r requirements.txt
Jalankan skrip:
C#
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME dotnet run
Go
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME go run .
Java
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME mvn compile exec:java -Dexec.mainClass=com.example.workflows.WorkflowsQuickstart
Node.js
npx tsx index.js
Python
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME python3 main.py
Ganti kode berikut:
PROJECT_ID: nama project Google Cloud AndaCLOUD_REGION: lokasi alur kerja Anda (default:us-central1)WORKFLOW_NAME: nama alur kerja Anda (default:myFirstWorkflow)
Outputnya mirip dengan hal berikut ini:
Execution finished with state: SUCCEEDED Execution results: ["Thursday","Thursday Night Football","Thursday (band)","Thursday Island","Thursday (album)","Thursday Next","Thursday at the Square","Thursday's Child (David Bowie song)","Thursday Afternoon","Thursday (film)"]Hapus alur kerja yang Anda buat:
gcloud workflows delete myFirstWorkflowKetika ditanya apakah Anda ingin melanjutkan, tekan
y.
Untuk mempelajari lebih lanjut peran dan izin akun layanan, lihat Memberikan izin alur kerja untuk mengakses Google Cloud resource.
Peran yang diperlukan
Untuk mendapatkan izin yang Anda perlukan untuk menyelesaikan panduan memulai ini, minta administrator Anda untuk memberi Anda peran IAM berikut di project Anda:
Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.
Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.
Men-deploy alur kerja contoh
Setelah menentukan alur kerja, Anda men-deploy alur kerja tersebut agar tersedia untuk dieksekusi. Langkah deploy juga memvalidasi bahwa file sumber dapat dieksekusi.
Alur kerja berikut mengirim permintaan ke API publik, lalu menampilkan respons API.
Mendapatkan kode contoh
Anda dapat meng-clone kode contoh dari GitHub.
Menjalankan kode contoh
Anda dapat menjalankan kode contoh dan mengeksekusi alur kerja. Mengeksekusi alur kerja akan menjalankan definisi alur kerja yang di-deploy dan terkait dengan alur kerja.
Meneruskan data dalam permintaan eksekusi
Bergantung pada bahasa library klien, Anda juga dapat meneruskan argumen runtime dalam permintaan eksekusi. Contoh:
C#
Go
Java
// Creates the execution object
CreateExecutionRequest request =
CreateExecutionRequest.newBuilder()
.setParent(parent.toString())
.setExecution(Execution.newBuilder().setArgument("{\"searchTerm\":\"Friday\"}").build())
.build();
Node.js
// Execute workflow
try {
const createExecutionRes = await client.createExecution({
parent: client.workflowPath(projectId, location, workflow),
execution: {
argument: JSON.stringify({"searchTerm": "Friday"})
}
});
const executionName = createExecutionRes[0].name;
Python
Untuk mengetahui informasi selengkapnya tentang meneruskan argumen runtime, lihat Meneruskan argumen runtime dalam permintaan eksekusi.
Pembersihan
Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, hapus project Google Cloud yang berisi resource tersebut.
Alur kerja akan dihapus.