Workflow mit den Cloud-Clientbibliotheken ausführen
In dieser Kurzanleitung erfahren Sie, wie Sie einen Workflow ausführen und die Ausführungsergebnisse mit den Cloud-Clientbibliotheken aufrufen.
Weitere Informationen zum Installieren der Cloud-Clientbibliotheken und zum Einrichten der Entwicklungsumgebung finden Sie in der Übersicht zu Workflow-Clientbibliotheken.
Sie können die folgenden Schritte über das Google Cloud CLI entweder in Ihrem Terminal oder in Cloud Shell ausführen.
Hinweise
Von Ihrer Organisation definierte Sicherheitsbeschränkungen verhindern möglicherweise, dass die folgenden Schritte ausgeführt werden. Informationen zur Fehlerbehebung finden Sie unter Anwendungen in einer eingeschränkten Google Cloud -Umgebung entwickeln.
- 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. 
- 
          Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden. 
- 
        Führen Sie folgenden Befehl aus, um die gcloud CLI zu initialisieren: 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
      (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.
 
- 
  
    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). 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/ownerIAM 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 account
- PROJECT_ID: the project ID where you created the service account
 
 
- 
    Ensure that you have the Create Service Accounts IAM role
    (
- 
      Install the Google Cloud CLI. 
- 
          Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden. 
- 
        Führen Sie folgenden Befehl aus, um die gcloud CLI zu initialisieren: 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
      (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.
 
- 
  
    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). 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/ownerIAM 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 account
- PROJECT_ID: the project ID where you created the service account
 
 
- 
    Ensure that you have the Create Service Accounts IAM role
    (
- Optional: Wenn Sie Logs an Cloud Logging senden möchten, weisen Sie dem Dienstkonto die Rolle roles/logging.logWriterzu.gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/logging.logWriter" Weitere Informationen zu Dienstkontorollen und -berechtigungen finden Sie unter Arbeitsablaufberechtigung für den Zugriff aufGoogle Cloud -Ressourcen erteilen. 
- Laden Sie das Git-Quellcodeverwaltungstool herunter und installieren Sie es, falls erforderlich.
- Erstellen Sie eine Textdatei mit dem Dateinamen - myFirstWorkflow.yamlund folgendem Inhalt:
- Nachdem Sie den Workflow erstellt haben, können Sie ihn bereitstellen, aber nicht den Workflow ausführen: - gcloud workflows deploy myFirstWorkflow \ --source=myFirstWorkflow.yaml \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --location=CLOUD_REGION - Ersetzen Sie - CLOUD_REGIONdurch einen unterstützten Speicherort für den Workflow. Die Standardregion, die in den Codebeispielen verwendet wird, ist- us-central1.
- Klonen Sie das Repository der Beispiel-App auf Ihren lokalen Computer: - C#- git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git - Sie können auch das Beispiel als ZIP-Datei herunterladen und extrahieren. - Go- git clone https://github.com/GoogleCloudPlatform/golang-samples.git - Sie können auch das Beispiel als ZIP-Datei herunterladen und extrahieren. - Java- git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git - Sie können auch das Beispiel als ZIP-Datei herunterladen und extrahieren. - Node.js- git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git - Sie können auch das Beispiel als ZIP-Datei herunterladen und extrahieren. - Python- git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git - Sie können auch das Beispiel als ZIP-Datei herunterladen und extrahieren. 
- Wechseln Sie in das Verzeichnis, das den Workflows-Beispielcode enthält: - 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/ 
- Sehen Sie sich den Beispielcode an: Jede Beispiel-App führt folgende Schritte aus: - Richtet die Cloud-Clientbibliotheken für Workflows ein.
- Führt einen Workflow aus.
- Fragt die Ausführung des Workflows (mit exponentiellem Backoff) ab, bis die Ausführung beendet ist.
- Druckt die Ausführungsergebnisse.
 - C#- Go- Java- Node.js- Python
- Installieren Sie zuerst die Abhängigkeiten, um das Beispiel auszuführen: - C#- dotnet restore - Go- go mod download - Java- mvn compile - Node.js- npm install -D tsx - Python- pip3 install -r requirements.txt 
- Führen Sie das Skript aus: - 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 - Ersetzen Sie Folgendes: - PROJECT_ID: Der Name Ihres Google Cloud -Projekts
- CLOUD_REGION: Der Speicherort Ihres Workflows (Standardeinstellung:- us-central1)
- WORKFLOW_NAME: Der Name Ihres Workflows (Standard:- myFirstWorkflow)
 - Die Ausgabe sieht etwa so aus: - 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)"]
- Löschen Sie den erstellten Workflow: - gcloud workflows delete myFirstWorkflow
- Wenn Sie gefragt werden, ob Sie fortfahren möchten, geben Sie - yein.
Beispielworkflow bereitstellen
Nachdem Sie einen Workflow definiert haben, stellen Sie ihn bereit, um ihn für die Ausführung verfügbar zu machen. Der Bereitstellungsschritt überprüft auch, ob die Quelldatei ausgeführt werden kann.
Der folgende Workflow sendet eine Anfrage an eine öffentliche API und gibt dann die Antwort der API zurück.
Beispielcode abrufen
Sie können den Beispielcode aus GitHub klonen.
Beispielcode ausführen
Sie können den Beispielcode ausführen und Ihren Workflow ausführen. Bei der Ausführung eines Workflows wird die bereitgestellte Workflowdefinition ausgeführt, die dem Workflow zugeordnet ist.
Daten in einer Ausführungsanfrage übergeben
Je nach Sprache der Clientbibliothek können Sie auch ein Laufzeitargument in einer Ausführungsanfrage übergeben. Beispiel:
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
Weitere Informationen zum Übergeben von Laufzeitargumenten finden Sie unter Laufzeitargumente in einer Ausführungsanfrage übergeben.
Bereinigen
Löschen Sie das Google Cloud -Projekt zusammen mit den Ressourcen, damit Ihrem Google Cloud -Konto die auf dieser Seite verwendeten Ressourcen nicht in Rechnung gestellt werden.
Der Workflow wurde gelöscht.