Execute a workflow using the Cloud Client Libraries
This quickstart shows you how to execute a workflow and view the execution results using the Cloud Client Libraries.
For more information about installing the Cloud Client Libraries and setting up your development environment, see the Workflows client libraries overview.
You can complete the following steps by using the Google Cloud CLI in either your terminal or Cloud Shell.
Before you begin
Security constraints defined by your organization might prevent you from completing the following steps. For troubleshooting information, see Develop applications in a constrained Google Cloud environment.
- 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) To send logs to Cloud Logging, grant the
roles/logging.logWriter
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"
To learn more about service account roles and permissions, see Grant a workflow permission to access Google Cloud resources.
- If required, download and install the Git source code management tool.
Deploy a sample workflow
After defining a workflow, you deploy it to make it available for execution. The deploy step also validates that the source file can be executed.
The following workflow sends a request to a public API and then returns the API's response.
Create a text file with the filename
myFirstWorkflow.yaml
with the following content:After creating the workflow, you can deploy it, but don't execute the workflow:
gcloud workflows deploy myFirstWorkflow \ --source=myFirstWorkflow.yaml \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --location=CLOUD_REGION
Replace
CLOUD_REGION
with a supported location for the workflow. The default region used in the code samples isus-central1
.
Get the sample code
You can clone the sample code from GitHub.
Clone the sample app repository to your local machine:
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Workflows sample code:
Java
cd java-docs-samples/workflows/cloud-client/
Node.js
cd nodejs-docs-samples/workflows/quickstart/
Python
cd python-docs-samples/workflows/cloud-client/
Take a look at the sample code. Each sample app does the following:
- Sets up the Cloud Client Libraries for Workflows.
- Executes a workflow.
- Polls the workflow's execution (using exponential backoff) until the execution terminates.
- Prints the execution results.
Java
Node.js
Python
Run the sample code
You can run the sample code and execute your workflow. Executing a workflow runs the deployed workflow definition associated with the workflow.
To run the sample, first install dependencies:
Java
mvn compile
Node.js
npm install -D tsx
Python
pip3 install -r requirements.txt
Run the script:
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
Replace the following:
PROJECT_ID
: your Google Cloud project nameCLOUD_REGION
: the location of your workflow (default:us-central1
)WORKFLOW_NAME
: your workflow name (default:myFirstWorkflow
)
The output is similar to the following:
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)"]
Pass data in an execution request
Depending on the client library language, you can also pass a runtime argument in an execution request. For example:
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;
For more information about passing runtime arguments, see Pass runtime arguments in an execution request.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.
Delete the workflow you created:
gcloud workflows delete myFirstWorkflow
When asked if you want to continue, enter
y
.
The workflow is deleted.