Quickstart using the gcloud tool

This quickstart shows you how to create, deploy, and execute your first workflow using the gcloud command-line tool. The workflow sends a request to a sample API and then uses the response to create and send a request to a public API. The workflow then returns the public API's response.

For a list of all Workflows gcloud tool commands, see the Workflows gcloud tool reference page.

Before you begin

  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. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Install and initialize the Cloud SDK.
  5. Enable the Workflows API.

    gcloud services enable workflows.googleapis.com
  6. Create the service account and give it a name; for example, sa-name.
    gcloud iam service-accounts create sa-name
  7. Grant the roles/logging.logWriter role to the service account.

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member "serviceAccount:sa-name@PROJECT_ID.iam.gserviceaccount.com" \
        --role "roles/logging.logWriter"
    

    Replace PROJECT_ID with your project's ID.

Create, deploy, and execute a workflow

  1. Open a terminal or Cloud Shell.
  2. In your home directory, create a new file called myFirstWorkflow.yaml or myFirstWorkflow.json.
  3. Copy and paste one of the following into the new file, then save it:

    YAML

    - getCurrentTime:
        call: http.get
        args:
          url: https://us-central1-workflowsample.cloudfunctions.net/datetime
        result: currentTime
    - readWikipedia:
        call: http.get
        args:
          url: https://en.wikipedia.org/w/api.php
          query:
            action: opensearch
            search: ${currentTime.body.dayOfTheWeek}
        result: wikiResult
    - returnResult:
        return: ${wikiResult.body[1]}

    JSON

    [
      {
        "getCurrentTime": {
          "call": "http.get",
          "args": {
            "url": "https://us-central1-workflowsample.cloudfunctions.net/datetime"
          },
          "result": "currentTime"
        }
      },
      {
        "readWikipedia": {
          "call": "http.get",
          "args": {
            "url": "https://en.wikipedia.org/w/api.php",
            "query": {
              "action": "opensearch",
              "search": "${currentTime.body.dayOfTheWeek}"
            }
          },
          "result": "wikiResult"
        }
      },
      {
        "returnResult": {
          "return": "${wikiResult.body[1]}"
        }
      }
    ]
    This workflow makes a call to a sample API. The returned day of the week is passed to the Wikipedia API. Relevant articles on Wikipedia about the current day of the week are returned.
  4. Deploy the workflow and associate it with the specified service account:
    gcloud workflows deploy myFirstWorkflow --source=myFirstWorkflow.yaml \
    --service-account=sa-name@PROJECT_ID.iam.gserviceaccount.com

    Replace the following:

    • .yaml: Replace with .json if you copied the JSON version of the example workflow.

    • PROJECT_ID: Your project ID.

  5. Execute the workflow:
    gcloud workflows run myFirstWorkflow

    This returns the results of the execution attempt. The output is similar to the following:

    argument: 'null'
    endTime: '2020-06-23T16:21:35.266171131Z'
    name: projects/123456789012/locations/us-central1/workflows/myFirstWorkflow/executions/f72bc6d4-5ea0-4dfb-bb14-2dae82303120
    result: '["Tuesday","Tuesday Weld","Tuesday Night Music Club","Tuesday (ILoveMakonnen
      song)","Tuesdays with Morrie","Tuesday Group","Tuesday Knight","Tuesday (Burak Yeter
      song)","Tuesday Morning Quarterback","Tuesday Maybe"]'
    startTime: '2020-06-23T16:21:34.826993288Z'
    state: SUCCEEDED
    workflowVersionId: '1'
    
  6. You've deployed and executed your first workflow!

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.

  1. Delete the workflow you created:
    gcloud workflows delete myFirstWorkflow
  2. When asked if you want to continue, enter y.

The workflow is deleted.

What's next