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 sample 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 returns the second 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. Replace [NAME] with a name for the service account.

    gcloud iam service-accounts create [NAME]
  7. Grant the roles/logging.logWriter role to the service account.

    gcloud projects add-iam-policy-binding [PROJECT_ID] --member "serviceAccount:[NAME]@[PROJECT_ID].iam.gserviceaccount.com" --role "roles/logging.logWriter"

    Replace the following:
    • [PROJECT_ID]: Your project's ID.
    • [NAME]: The name of the service account you just created.
    Note: The Role field authorizes your service account to access resources. To send logs to Cloud Logging, assign the roles/logging.logWriter role. If you are developing a production app, always grant the least permissive roles possible. For more information, see granting roles to service accounts. For a list of Workflows roles, see the Access control reference.

Create, deploy, and execute a workflow

  1. Create a new file called myFirstWorkflow.yaml or myFirstWorkflow.json.

  2. 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 to get the current date. It takes the day of the week from that response and passes it to the Wikipedia API, where it searches for relevant articles on Wikipedia about the current day of the week.

  3. Open a terminal.

  4. Deploy the workflow by entering the following command:

    gcloud workflows deploy myFirstWorkflow --source=myFirstWorkflow.yaml \
    --service-account=[NAME]@[PROJECT_ID].iam.gserviceaccount.com

    Replace the following:

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

    • [NAME]: The name of the service account you created earlier

    • [PROJECT_ID]: Your project ID.

    This command deploys the workflow and associates it with the specified service account.

  5. Execute the workflow by entering the following command:

    gcloud workflows execute myFirstWorkflow
    

    This returns a workflows executions describe command with the unique ID of the workflow execution attempt. The output is similar to the following:

    Created [cf1b1883-e271-4371-916a-10337bc1b87a].
    To view the workflow status, you can use following command:
    
    gcloud workflows executions describe cf1b1883-e271-4371-916a-10337bc1b87a --workflow myFirstWorkflow
    
  6. To view the status of the execution, enter the command returned by the previous step. This returns data about the workflow execution, including the result:

    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'
    

You 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 by entering the following command:

    gcloud workflows delete myFirstWorkflow
    
  2. When asked if you want to continue, enter y.

The workflow will be deleted.

What's next