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 mit der 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.
-
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
-
- Optional: Wenn Sie Logs an Cloud Logging senden möchten, gewähren Sie dem Dienstkonto die Rolle
roles/logging.logWriter
.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 Rollen und Berechtigungen für Dienstkonten finden Sie unter Workflow-Berechtigung zum Zugriff aufGoogle Cloud Ressourcen erteilen.
- Laden Sie das Git-Quellcodeverwaltungstool herunter und installieren Sie es, falls erforderlich.
Beispiel-Workflow 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.
Erstellen Sie eine Textdatei mit dem Dateinamen
myFirstWorkflow.yaml
und 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_REGION
durch einen unterstützten Speicherort für den Workflow. In den Codebeispielen wird die Standardregionus-central1
verwendet.
Beispielcode abrufen
Sie können den Beispielcode aus GitHub klonen.
Klonen Sie das Repository der Beispiel-App auf Ihren lokalen Computer:
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:
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 tut Folgendes:
- 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.
Java
Node.js
Python
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 mit dem Workflow verknüpft ist.
Installieren Sie zuerst die Abhängigkeiten, um das Beispiel auszuführen:
Java
mvn compile
Node.js
npm install -D tsx
Python
pip3 install -r requirements.txt
Führen Sie das Skript aus:
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 ProjektsCLOUD_REGION
: Der Speicherort Ihres Workflows (Standard:us-central1
)WORKFLOW_NAME
: Der Name des Workflows (Standard:myFirstWorkflow
)
Die Ausgabe sieht in 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)"]
Daten in einer Ausführungsanfrage übergeben
Je nach Sprache der Clientbibliothek können Sie auch ein Laufzeitargument in einer Ausführungsanfrage übergeben. Beispiel:
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;
Weitere Informationen zum Übergeben von Laufzeitargumenten finden Sie unter Laufzeitargumente in einer Ausführungsanfrage übergeben.
Bereinigen
Löschen Sie das Google Cloud-Projekt mit den Ressourcen, damit Ihrem Google Cloud -Konto die auf dieser Seite verwendeten Ressourcen nicht in Rechnung gestellt werden.
Löschen Sie den erstellten Workflow:
gcloud workflows delete myFirstWorkflow
Wenn Sie gefragt werden, ob Sie fortfahren möchten, geben Sie
y
ein.
Der Workflow wurde gelöscht.