Skip to content

Lifecycle-Hook

Introduction

A LifecycleHook triggers an action based on a conditional expression or on completion of a step or template. It is configured either at the workflow-level or template-level, for instance as a function of the workflow.status or steps.status, respectively. A LifecycleHook executes during execution time, at most once for the workflow, step or task it is attached to. An expanded step or DAG task (one with withItems, withParam or withSequence) runs its hooks once for each item. The hook runs in parallel to its step or template once the expression is satisfied.

In other words, a LifecycleHook functions like an exit handler with a conditional expression. You must not name a LifecycleHook exit or it becomes an exit handler; otherwise the hook name has no relevance.

A step's or task's exit hook starts only after any of its lifecycle hooks that started have finished, and the steps or tasks that depend on it wait for all of its hooks. A step's or task's hook that ends Error, for example because it could not be started or it timed out while still Pending, fails its DAG or Steps template, and continueOn does not cover it. A hook that ends Failed is ignored. The "DAG and Steps templates run on one engine" section of the v4.2 upgrade notes describes how these rules differ from earlier versions.

Workflow-level LifecycleHook: Executes the template when a configured expression is met during the workflow.

Template-level Lifecycle-Hook: Executes the template when a configured expression is met during the step in which it is defined.

Supported conditions

Hook arguments and expressions that reference outputs of a skipped or omitted step resolve according to Outputs of Skipped and Omitted Nodes.

Unsupported conditions

  • outputs are not usable since LifecycleHook executes during execution time and outputs are not produced until the step is completed. You can use outputs from previous steps, just not the one you're hooking into. If you'd like to use outputs create an exit handler instead - all the status variable are available there so you can still conditionally decide what to do.

Notification use case

A LifecycleHook can be used to configure a notification depending on a workflow status change or template status change, like the example below:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
 generateName: lifecycle-hook-
spec:
 entrypoint: main
 hooks:
   exit:
     template: http
   running:
     expression: workflow.status == "Running"
     template: http
 templates:
   - name: main
     steps:
       - - name: step1
           template: heads

   - name: heads
     container:
       image: alpine:3.23
       command: [sh, -c]
       args: ["echo \"it was heads\""]

   - name: http
     http:
       url: http://dummy.restapiexample.com/api/v1/employees

Put differently, an exit handler is like a workflow-level LifecycleHook with an expression of workflow.status == "Succeeded" or workflow.status == "Failed" or workflow.status == "Error".


Have a question?

Search on GitHub Discussions and Slack.