Skip to content

Actions

In this section, you will update the "environment template" to use an action. The action will be configured to control the logic defined within the Python code of a function workflow handler.


Step 1: Update Workflow Handler

In this step, you will update the existing Workflow Handler used in the Introductory Exercise. You will update the WorkflowHandler function code logic to handle different action types.

  • In your project, navigate to Environments -> Workflow Handlers
  • Click on the name of the previously created workflow handler
  • Navigate to Function
  • Replace the function source code with the below code
from typing import Dict, Any
from logging import Logger

def handle(logger: Logger, request: Dict[str, Any]) -> Dict[str, Any]:
    logger.info(f"Inside function handler, request: {request}", extra={"request": request})

    action = request.get("action")

    if action == "deploy":
        logger.info("hello world")
        return {
            "output": "hello world",
            "action": action
        }

    elif action == "retry":
        logger.info("hello again")
        return {
            "output": "hello again",
            "action": action
        }

    else:
        logger.info("unknown action")
        return {
            "output": "unknown action",
            "action": action
        }
  • Click Save

Update WFH


Step 2: Update Environment Template

In this step, you will update the existing Environment Template to include an Action. The Action will use the same workflow handler, however, now the logic will be controlled by the Action the user selects.

  • In your project, navigate to Environments -> Environment Templates
  • Click on the name of the environment template
  • Edit the template
  • Navigate to Actions
  • Click + Actions
  • Enter the name retry. Note, this name is what will be passed as a variable to the workflow handler to determine the logic used.
  • Select Workflows for the Type
  • Expand the Custom Providers section
  • Click + Task
  • Enter a name for the task
  • Select the previously modified workflow handler for the Driver Name
  • Click Save & Continue

Configure Task

  • Click Save

Configure Action

Next, we will create an input variable that will pass the action value to the workflow handler.

  • Navigate to Input Variables
  • Click + Variable
  • Enter a name for the variable
  • Select Not Allowed for the Override
  • Select Expressions for the Value Type
  • Enter the following code for the Default Value
## script begin ##
def eval(**ctx):
  action_name = ctx["trigger"]["payload"].get("action_name")
  if action_name:
    return action_name
  return ctx["trigger"]["payload"].get("action")
## script end ##
  • Click Save
  • Select No, Exit

Configure Input Variable

Note: In the case you want to use a selector to refine where this variable is passed to, you can use environment.action.. to pass the action name to the action

  • Click Save as Draft

Update Env Template


Step 3: Run Action

In this step, we will create a new environment using the recently updated environment template. Once the environment is created, we will run the retry action on the environment to see the output that should be displayed from this action.

  • In your project, navigate to Environments -> Environments
  • Click Launch on the environment template card
  • Enter a name for the environment
  • Click Save & Deploy

Environment

After the environment is created, you will see the results sections shows the "deploy" action and the output "hello world".

Environment

Next, you will run the previously created retry action on the environment to produce a different output.

  • Click Show in the Actions section
  • Select the previously created retry action
  • Click Open Action
  • Click Execute Action

Execute Action

Once the action has finished running:

  • Click Show in the Activities section
  • Expand the first section to see the action outputs

You will see the action is not "retry" and the output is "hello again". The output is different as the logic in the workflow handler was set to return this output value if the action was equal to "retry".

Environment


Recap

In this part, you created an action on your environment template. The action was configured to trigger different logic within the workflow handler.