Control the order of execution in a workflow

This page shows you how to use jumps or for loops to control the order in which your workflow's steps run. Basic jumps allow you to define which step the workflow runs next. Conditional jumps build on basic jumps, allowing you to use conditional expressions to control the order of execution through a workflow. For example, you can run certain steps only when a variable or response from another workflow step meets specific criteria.

The examples on this page use a sample API that returns the day of the week.

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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. You should already have an existing workflow where you want to change the order of execution. To learn how to create and deploy a workflow, see Create and update a workflow.

Use jumps to change execution order

By default, all workflows are ordered lists where every step runs in the order you define in the workflow's source code. You can choose to override this default ordering by using jumps.

Basic jumps

You can specify which step to run next in a workflow by using basic jumps.

Console

  1. Open the Workflows page in the Google Cloud console:
    Go to Workflows

  2. Select the name of the workflow where you want to change the order of execution of the existing steps.

  3. On the Edit workflow page, select Next to go to the workflow editor.

  4. Add the next field at the end of a step to tell the workflow to jump to a particular step:

    YAML

         - step_a:
             ...
             next: STEP_NAME_TO_JUMP_TO
         - step_b:
             ...
         - next_step:
             ...

    JSON

        [
          {
            "step_a": {
              ...
              "next": "STEP_NAME_TO_JUMP_TO"
            }
          }
          {
            "step_b": {
              ...
            }
          }
          {
            "next_step": {
              ...
            }
          }
        ]

    Replace STEP_NAME_TO_JUMP_TO with the name of the step you want the workflow to execute next. For example, next_step.

  5. Select Deploy.

gcloud

  1. Open your workflow's definition file in the text editor of your choice.

  2. Add the next field at the end of a step to tell the workflow to jump to a particular step:

    YAML

         - step_a:
             ...
             next: STEP_NAME_TO_JUMP_TO
         - step_b:
             ...
         - next_step:
             ...

    JSON

      [
        {
          "step_a": {
            ...
            "next": "STEP_NAME_TO_JUMP_TO"
          }
        }
        {
          "step_b": {
            ...
          }
        }
        {
          "next_step": {
            ...
          }
        }
      ]

    Replace STEP_NAME_TO_JUMP_TO with the name of the step you want the workflow to execute next. For example, next_step.

  3. Save the workflow file.

  4. To deploy the workflow, enter the following command:

    gcloud workflows deploy WORKFLOW_NAME \
    --source=WORKFLOW_FILE.YAML

    Replace the following:

    • WORKFLOW_NAME: required. The name of your workflow.

    • WORKFLOW_FILE.YAML: required. The source file for the workflow.

Example

For example, the following workflow has its steps out of order:

YAML

  - get_time:
     call: http.get
     args:
         url:  https://us-central1-workflowsample.cloudfunctions.net/datetime
     result: currentTime
  - return_daylight_savings_bool:
     return: ${daylightSavings}
  - get_daylight_savings_bool:
     assign:
         - daylightSavings: ${currentTime.body.isDayLightSavingsTime}

JSON

    [
      {
        "get_time": {
          "call": "http.get",
          "args": {
            "url": "https://us-central1-workflowsample.cloudfunctions.net/datetime"
          },
          "result": "currentTime"
        }
      },
      {
        "return_daylight_savings_bool": {
          "return": "${daylightSavings}"
        }
      },
      {
        "get_daylight_savings_bool": {
          "assign": [
            {
              "daylightSavings": "${currentTime.body.isDayLightSavingsTime}"
            }
          ]
        }
      }
    ]

In this example, next fields have been added to the get_daylight_savings_bool and return_daylight_savings_bool steps so that the steps execute in the correct order:

YAML

  - get_time:
     call: http.get
     args:
         url:  https://us-central1-workflowsample.cloudfunctions.net/datetime
     result: currentTime
     next: get_daylight_savings_bool
  - return_daylight_savings_bool:
     return: