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 .

  1. 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.
  2. Install the Google Cloud CLI.

  3. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  4. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • 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.

  6. 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.

  7. Verify that billing is enabled for your Google Cloud project.

  8. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable workflows.googleapis.com
  9. Install the Google Cloud CLI.

  10. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  11. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  12. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • 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.

  13. 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.

  14. Verify that billing is enabled for your Google Cloud project.

  15. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable workflows.googleapis.com
  16. Set up authentication:

    1. 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.
    2. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    3. Grant the roles/logging.logWriter 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/logging.logWriter

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
  17. 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

    1. Di direktori beranda, buat file baru bernama myFirstWorkflow.yaml atau myFirstWorkflow.json.

    2. Salin dan tempel alur kerja berikut ke dalam file baru, lalu simpan:

      YAML

      main:
        params: [input]
        steps:
          - checkSearchTermInInput:
              switch:
                - condition: '${"searchTerm" in input}'
                  assign:
                    - searchTerm: '${input.searchTerm}'
                  next: readWikipedia
          - getLocation:
              call: sys.get_env
              args:
                name: GOOGLE_CLOUD_LOCATION
              result: location
          - setFromCallResult:
              assign:
                - searchTerm: '${text.split(location, "-")[0]}'
          - readWikipedia:
              call: http.get
              args:
                url: 'https://en.wikipedia.org/w/api.php'
                query:
                  action: opensearch
                  search: '${searchTerm}'
              result: wikiResult
          - returnOutput:
              return: '${wikiResult.body[1]}'

      JSON

      {
        "main": {
          "params": [
            "input"
          ],
          "steps": [
            {
              "checkSearchTermInInput": {
                "switch": [
                  {
                    "condition": "${\"searchTerm\" in input}",
                    "assign": [
                      {
                        "searchTerm": "${input.searchTerm}"
                      }
                    ],
                    "next": "readWikipedia"
                  }
                ]
              }
            },
            {
              "getLocation": {
                "call": "sys.get_env",
                "args": {
                  "name": "GOOGLE_CLOUD_LOCATION"
                },
                "result": "location"
              }
            },
            {
              "setFromCallResult": {
                "assign": [
                  {
                    "searchTerm": "${text.split(location, \"-\")[0]}"
                  }
                ]
              }
            },
            {
              "readWikipedia": {
                "call": "http.get",
                "args": {
                  "url": "https://en.wikipedia.org/w/api.php",
                  "query": {
                    "action": "opensearch",
                    "search": "${searchTerm}"
                  }
                },
                "result": "wikiResult"
              }
            },
            {
              "returnOutput": {
                "return": "${wikiResult.body[1]}"
              }
            }
          ]
        }
      }
      

      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.

    3. 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.com
      

      Ganti kode berikut:

      • EXTENSION: ekstensi file untuk alur kerja Anda; gunakan yaml untuk versi YAML atau gunakan json untuk versi JSON
      • PROJECT_ID: project ID Anda
    4. Menjalankan alur kerja:

      gcloud workflows run myFirstWorkflow \
          --data='SEARCH_TERM'
      

      Ganti SEARCH_TERM dengan 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-ac2
      

    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.

    1. Hapus alur kerja yang Anda buat:

      gcloud workflows delete myFirstWorkflow
      
    2. Ketika ditanya apakah Anda ingin melanjutkan, tekan y.

    Alur kerja akan dihapus.

    Langkah berikutnya