Menjalankan alur kerja menggunakan Library Klien Cloud
Panduan memulai ini menunjukkan cara menjalankan alur kerja dan melihat hasil eksekusi menggunakan Library Klien Cloud.
Untuk informasi selengkapnya tentang cara menginstal Library Klien Cloud dan menyiapkan lingkungan pengembangan, lihat Ringkasan library klien Alur Kerja.
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 Google Cloud yang terbatas.
- 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.
-
To initialize the gcloud CLI, run the following command:
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/owner
IAM 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/owner
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflows API:
gcloud services enable workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/owner
IAM 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/owner
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
- (Opsional) Untuk mengirim log ke Cloud Logging, berikan peran
roles/logging.logWriter
ke akun layanan.gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/logging.logWriter"
Untuk mempelajari peran dan izin akun layanan lebih lanjut, lihat Memberikan izin alur kerja untuk mengakses Google Cloud resource.
- Jika diperlukan, download dan instal alat pengelolaan kode sumber Git.
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.
Buat file teks dengan nama file
myFirstWorkflow.yaml
dengan konten berikut:Setelah membuat alur kerja, Anda dapat men-deploynya, 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_REGION
dengan lokasi yang didukung untuk alur kerja. Wilayah default yang digunakan dalam contoh kode adalahus-central1
.
Mendapatkan kode contoh
Anda dapat meng-clone kode contoh dari GitHub.
Clone repositori aplikasi contoh ke komputer lokal Anda:
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:
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 Alur Kerja.
- Menjalankan alur kerja.
- Melakukan polling terhadap eksekusi alur kerja (menggunakan backoff eksponensial) hingga eksekusi dihentikan.
- Mencetak hasil eksekusi.
Java
Node.js
Python
Menjalankan kode contoh
Anda dapat menjalankan kode contoh dan menjalankan alur kerja. Menjalankan alur kerja akan menjalankan definisi alur kerja yang di-deploy yang terkait dengan alur kerja.
Untuk menjalankan contoh, instal dependensi terlebih dahulu:
Java
mvn compile
Node.js
npm install -D tsx
Python
pip3 install -r requirements.txt
Jalankan skrip:
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
: Google Cloud nama project 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)"]
Meneruskan data dalam permintaan eksekusi
Bergantung pada bahasa library klien, Anda juga dapat meneruskan argumen runtime dalam permintaan eksekusi. Contoh:
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;
Untuk informasi selengkapnya tentang cara meneruskan argumen runtime, lihat Teruskan argumen runtime dalam permintaan eksekusi.
Pembersihan
Agar tidak menimbulkan biaya pada akun Google Cloud Anda untuk resource yang digunakan di halaman ini, hapus project Google Cloud yang berisi resource tersebut.
Hapus alur kerja yang Anda buat:
gcloud workflows delete myFirstWorkflow
Ketika ditanya apakah Anda ingin melanjutkan, tekan
y
.
Alur kerja akan dihapus.