Stay organized with collections Save and categorize content based on your preferences.

Create a workflow using the gcloud CLI

This quickstart shows you how to create, deploy, and execute your first workflow using the Google Cloud CLI. 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 CLI commands, see the Workflows gcloud CLI reference page.

Before you begin

Some of the steps in this document might not work correctly if your organization applies constraints to your Google Cloud environment. In that case, you might not be able to complete tasks like creating public IP addresses or service account keys. If you make a request that returns an error about constraints, see how to Develop applications in a constrained Google Cloud environment.

  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. Install and initialize the Google Cloud CLI.
  3. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  4. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  5. Install and initialize the Google Cloud CLI.
  6. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  7. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  8. Enable the Workflows API.

    gcloud services enable workflows.googleapis.com
  9. Create the service account and give it a name; for example, sa-name.
    gcloud iam service-accounts create sa-name
  10. 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:sa-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.

Create, deploy, and execute a workflow

  1. In your home directory, create a new file called myFirstWorkflow.yaml or myFirstWorkflow.json.

  2. Copy and paste the following workflow into the new file, then save it:

    YAML

    main:
      params: [input]
      steps:
        - checkInputExists:
            switch:
              - condition: ${input != null}
                next: checkSearchTermInInput
            next: getCurrentTime
        - checkSearchTermInInput:
            switch:
              - condition: ${"searchTerm" in input}
                assign:
                  - searchTerm: ${input.searchTerm}
                next: readWikipedia
        - getCurrentTime:
            call: http.get
            args:
              url: https://us-central1-workflowsample.cloudfunctions.net/datetime
            result: currentDateTime
        - setFromCallResult:
            assign:
              - searchTerm: ${currentDateTime.body.dayOfTheWeek}
        - readWikipedia:
            call: http.get
            args:
              url: https://en.wikipedia.org/w/api.php
              query:
                action: opensearch
                search: ${searchTerm}
            result: wikiResult
        - returnOutput:
            return: ${wikiResult.body[1]}

    JSON

    {
      "main": {
        "params": [
          "input"
        ],
        "steps": [
          {
            "checkInputExists": {
              "switch": [
                {
                  "condition": "${input != null}",
                  "next": "checkSearchTermInInput"
                }
              ],
              "next": "getCurrentTime"
            }
          },
          {
            "checkSearchTermInInput": {
              "switch": [
                {
                  "condition": "${\"searchTerm\" in input}",
                  "assign": [
                    {
                      "searchTerm": "${input.searchTerm}"
                    }
                  ],
                  "next": "readWikipedia"
                }
              ]
            }
          },
          {
            "getCurrentTime": {
              "call": "http.get",
              "args": {
                "url": "https://us-central1-workflowsample.cloudfunctions.net/datetime"
              },
              "result": "currentDateTime"
            }
          },
          {
            "setFromCallResult": {
              "assign": [
                {
                  "searchTerm": "${currentDateTime.body.dayOfTheWeek}"
                }
              ]
            }
          },
          {
            "readWikipedia": {
              "call": "http.get",
              "args": {
                "url": "https://en.wikipedia.org/w/api.php",
                "query": {
                  "action": "opensearch",
                  "search": "${searchTerm}"
                }
              },
              "result": "wikiResult"
            }
          },
          {
            "returnOutput": {
              "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.

  3. Deploy the workflow and associate it with the specified service account:

    gcloud workflows deploy myFirstWorkflow --source=myFirstWorkflow.EXTENSION \
        --service-account=sa-name@PROJECT_ID.iam.gserviceaccount.com
    

    Replace the following:

    • EXTENSION: the file extension for your workflow; use yaml for the YAML version or use json for the JSON version
    • PROJECT_ID: your project ID
  4. Execute the workflow:

    gcloud workflows run myFirstWorkflow \
        --data='DAY_OF_WEEK'
    

    Replace DAY_OF_WEEK with your search term; for example, {"searchTerm":"Tuesday"}. If you enter {}, the current day of the week (in GMT) is retrieved.

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

    argument: '{"searchTerm":"Tuesday"}'
    endTime: '2022-07-19T13:50:00.452572474Z'
    name: projects/1051295516635/locations/us-central1/workflows/myFirstWorkflow/executions/9714c400-c8ce-4495-bdf8-fec3d1c4aaf7
    result: '["Tuesday","Tuesday Weld","Tuesday Night Music Club","Tuesday (iLoveMakonnen
      song)","Tuesdays with Morrie","Tuesday Knight","Tuesday Night Baseball","Tuesday
      Vargas","Tuesday Morning Quarterback","Tuesdays with Morrie (film)"]'
    startTime: '2022-07-19T13:49:59.901548541Z'
    state: SUCCEEDED
    status:
      currentSteps:
      - routine: main
        step: returnOutput
    workflowRevisionId: 000001-ac2
    

You've deployed and executed your first workflow!

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Cloud project with the resources.

  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