Controlling the order of execution in a workflow

This page shows you how to use jumps 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 a certain criteria.

The example on this page uses a sample API that returns the day of the week.

Before you begin

  1. 登录您的 Google 帐号。

    如果您还没有 Google 帐号,请注册一个新帐号

  2. 在 Google Cloud Console 的项目选择器页面上,选择或创建一个 Google Cloud 项目。

    转到项目选择器页面

  3. 确保您的 Cloud 项目已启用结算功能。 了解如何确认您的项目是否已启用结算功能

  4. Have an existing workflow where you want to change the order of execution. To learn how to create and deploy a workflow, see Creating and updating a workflow

Using 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 the Workflows page

  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:

    - step_a:
        ...
        next: STEP_NAME
    - step_b:
        ...
    - next_step:
        ...
    

    Replace STEP_NAME 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:

    - step_a:
        ...
        next: STEP_NAME
    - step_b:
        ...
    - next_step:
        ...
    

    Replace STEP_NAME 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 beta 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, take the following workflow, which has its steps out of order:

- 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}

Add next fields for the steps get_time and get_daylight_savings_bool so that the workflow will execute in the correct order:

- 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: ${daylightSavings}
- get_daylight_savings_bool:
   assign:
       - daylightSavings: ${currentTime.body.isDayLightSavingsTime}
   next: return_daylight_savings_bool

Conditional jumps

You can use conditional jumps to determine which step to run next in a workflow. Conditional jumps use switch blocks, which jump to a specified step if a particular condition is met. Each switch block can include a maximum of 10 conditions.

Console

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

  2. Select the name of the workflow to which you want to add a switch block.

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

  4. On the Define workflow page, your workflow's current definition is displayed. To edit the workflow so that it executes a certain step based on a conditional statement, add a step that uses a switch block:

    - SWITCH_STEP_NAME:
       switch:
           - condition: ${EXPRESSION_ONE}
             next: STEP_A
           - condition: ${EXPRESSION_TWO}
             next: STEP_B
           ...
       next: STEP_C
    - STEP_A
       ...
    - STEP_B
       ...
    - STEP_C
       ...

    Replace the following:

    • SWITCH_STEP_NAME: The name of the step containing the switch block.

    • EXPRESSION_ONE: The first expression to evaluate. If this expression evaluates to true, the workflow executes STEP_A next. If this expression evaluates to false, the workflow evaluates the next expression.

    • EXPRESSION_TWO: The second expression to evaluate. If this expression evaluates to true, the workflow executes STEP_B next. If this expression evaluates to false, the workflow executes STEP_C.

    • STEP_A, STEP_B, STEP_C: Placeholder step names.

  5. Select Deploy.

gcloud

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

  2. To edit the workflow so that it executes a certain step based on a conditional statement, add a step that uses a switch block:

    - SWITCH_STEP_NAME:
       switch:
           - condition: ${EXPRESSION_ONE}
             next: STEP_A
           - condition: ${EXPRESSION_TWO}
             next: STEP_B
           ...
       next: STEP_C
    - STEP_A
       ...
    - STEP_B
       ...
    - STEP_C
       ...

    Replace the following:

    • SWITCH_STEP_NAME: The name of the step containing the switch block.

    • EXPRESSION_ONE: The first expression to evaluate. If this expression evaluates to true, the workflow executes STEP_A next. If this expression evaluates to false, the workflow evaluates the next expression.

    • EXPRESSION_TWO: The second expression to evaluate. If this expression evaluates to true, the workflow executes STEP_B next. If this expression evaluates to false, the workflow executes STEP_C.

    • STEP_A, STEP_B, STEP_C: Placeholder step names.

    The switch statement can have as many conditions as necessary.

  3. Save the workflow file.

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

    gcloud beta 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, this workflow uses a switch block to control the workflow's order of execution:

- getCurrentTime:
    call: http.get
    args:
        url:  https://us-central1-workflowsample.cloudfunctions.net/datetime
    result: currentTime
- conditionalSwitch:
    switch:
        - condition: ${currentTime.body.dayOfTheWeek == "Friday"}
          next: friday
        - condition: ${currentTime.body.dayOfTheWeek == "Saturday" OR currentTime.body.dayOfTheWeek == "Sunday"}
          next: weekend
    next: workWeek
- friday:
    return: "It's Friday! Almost the weekend!"
- weekend:
    return: "It's the weekend!"
- workWeek:
    return: "It's the work week."

In this example, the switch block has two conditions. The parser evaluates each condition in order, and if the result of the condition's expression is true, that condition's next step is called. If none of the conditions are met, the workflow calls the step specified by the next field outside of the switch block, which, in this example, is the step workWeek.

For example, if the day of the week is "Saturday", then the workflow jumps to the step weekend and returns the message "It's the weekend!". The workflow doesn't execute the step friday because the conditional jump skips over it, and the workflow doesn't execute the step workWeek because return stops the execution of the workflow at the end of the weekend step.

What's next