Membuat alur kerja menggunakan gcloud CLI
Panduan memulai ini menunjukkan cara membuat, men-deploy, dan menjalankan alur kerja pertama Anda menggunakan Google Cloud CLI. Alur kerja contoh mengirim permintaan ke API publik lalu menampilkan respons API.
Untuk mengetahui daftar semua perintah gcloud CLI Workflows, lihat halaman referensi gcloud CLI Workflows.
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
(
-
Service Account User (
roles/iam.serviceAccountUser) -
Workflows Admin (
roles/workflows.admin) Di direktori beranda, buat file baru bernama
myFirstWorkflow.yamlataumyFirstWorkflow.json.Salin dan tempel alur kerja berikut ke dalam file baru, lalu simpan:
YAML
JSON
Kecuali jika Anda memasukkan istilah penelusuran sendiri, alur kerja ini menggunakan Google Cloud lokasi Anda untuk membuat istilah penelusuran, yang diteruskan ke Wikipedia API. Daftar artikel Wikipedia terkait akan ditampilkan.
Deploy alur kerja dan kaitkan dengan akun layanan yang ditentukan:
gcloud workflows deploy myFirstWorkflow --source=myFirstWorkflow.EXTENSION \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.comGanti kode berikut:
EXTENSION: ekstensi file untuk alur kerja Anda; gunakanyamluntuk versi YAML atau gunakanjsonuntuk versi JSONPROJECT_ID: project ID Anda
Menjalankan alur kerja:
gcloud workflows run myFirstWorkflow \ --data='SEARCH_TERM'Ganti
SEARCH_TERMdengan istilah penelusuran Anda; misalnya,{"searchTerm":"North"}. Jika Anda memasukkan{}, lokasi Google Cloud Anda akan digunakan untuk membuat istilah penelusuran.Tindakan ini akan menampilkan hasil upaya eksekusi. Outputnya mirip dengan hal berikut ini:
argument: '{"searchTerm":"North"}' duration: 0.210616856s endTime: '2023-05-10T21:56:39.465899376Z' name: projects/734581694262/locations/us-central1/workflows/workflow-1/executions/eae31f11-a5c3-47e2-8014-05b400820a79 result: '["North","North America","Northern Ireland","North Korea","North Macedonia","North Carolina","Northrop Grumman B-2 Spirit","Northrop F-5","Northern Cyprus","North Dakota"]' startTime: '2023-05-10T21:56:39.255282520Z' state: SUCCEEDED status: currentSteps: - routine: main step: returnOutput workflowRevisionId: 000001-ac2Hapus 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.
Membuat, men-deploy, dan menjalankan alur kerja
Anda telah men-deploy dan menjalankan alur kerja pertama Anda.
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.