List workflow revisions (v1)

List all revisions of a workflow.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Go API reference documentation.

To authenticate to Workflows, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import (
	"context"
	"fmt"
	"io"

	workflows "cloud.google.com/go/workflows/apiv1"
	workflowspb "cloud.google.com/go/workflows/apiv1/workflowspb"
	"google.golang.org/api/iterator"
)

// listWorkflowRevisions lists all revisions of a workflow.
func listWorkflowRevisions(ctx context.Context, w io.Writer, projectID string, locationID string, workflowID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/workflows/%s", projectID, locationID, workflowID)

	client, err := workflows.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("workflows.NewClient: %w", err)
	}
	defer client.Close()

	req := &workflowspb.ListWorkflowRevisionsRequest{
		Name: name,
	}

	it := client.ListWorkflowRevisions(ctx, req)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListWorkflowRevisions: %w", err)
		}
		fmt.Fprintf(w, "Workflow revision: %s\n", resp.GetName())
	}

	return nil
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Node.js API reference documentation.

To authenticate to Workflows, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {WorkflowsClient} = require('@google-cloud/workflows');
const {status} = require('@grpc/grpc-js');

const client = new WorkflowsClient();

/**
 * Use the listWorkflowRevisions method
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 * @param {string} workflowId - ID of the Workflow (for example, 'example-workflow-id')
 */
async function listWorkflowRevisions(projectId, locationId, workflowId) {
  try {
    const name = `projects/${projectId}/locations/${locationId}/workflows/${workflowId}`;
    const request = {
      name,
    };

    const [revisions] = await client.listWorkflowRevisions(request);

    for (const revision of revisions) {
      console.log(`Revision: ${revision.name}`);
      console.log(`Revision ID: ${revision.revisionId}`);
      console.log(`State: ${revision.state}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code !== undefined) {
      console.error(`Google Cloud API error (${err.code}): ${err.message}`);
    } else {
      console.error(`Unexpected error: ${err.message}`);
    }
  }
}

module.exports = { listWorkflowRevisions };

Python

Before trying this sample, follow the Python setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Python API reference documentation.

To authenticate to Workflows, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import google.api_core.exceptions
from google.cloud import workflows_v1

client = workflows_v1.WorkflowsClient()


def list_workflow_revisions(
    project_id: str, location_id: str, workflow_id: str
) -> None:
    """Lists revisions for a specific workflow.

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id')
        location_id: The Google Cloud region where the service is located (for example, 'us-central1')
        workflow_id: ID of the Workflow (for example, 'example-workflow-id')
    """
    name = client.workflow_path(project_id, location_id, workflow_id)

    request = workflows_v1.ListWorkflowRevisionsRequest(name=name)

    try:
        response = client.list_workflow_revisions(request=request)
        for workflow in response:
            print(f"Revision ID: {workflow.revision_id}")
            print(f"Workflow Name: {workflow.name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to list workflow revisions: {error}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.