# An exit handler is a template reference that executes at the end of the workflow# irrespective of the success, failure, or error of the primary workflow. To specify# an exit handler, reference the name of a template in 'spec.onExit'.# Some common use cases of exit handlers are:# - sending notifications of workflow status (e.g. e-mail/slack)# - posting the pass/fail status to a webhook result (e.g. github build result)# - cleaning up workflow artifacts# - resubmitting or submitting another workflowapiVersion:argoproj.io/v1alpha1kind:Workflowmetadata:generateName:exit-handlers-spec:entrypoint:intentional-failonExit:exit-handlertemplates:# primary workflow template- name:intentional-failcontainer:image:alpine:latestcommand:[sh, -c]args:["echo intentional failure; exit 1"]# exit handler related templates# After the completion of the entrypoint template, the status of the# workflow is made available in the global variable {{workflow.status}}.# {{workflow.status}} will be one of: Succeeded, Failed, Error- name:exit-handlersteps:- - name:notifytemplate:send-email- name:celebratetemplate:celebratewhen:"{{workflow.status}} == Succeeded"- name:crytemplate:crywhen:"{{workflow.status}} != Succeeded"- name:send-emailcontainer:image:alpine:latestcommand:[sh, -c]# Tip: {{workflow.failures}} is a JSON list. If you're using bash to read it, we recommend using jq to manipulate# it. For example:## echo "{{workflow.failures}}" | jq -r '.[] | "Failed Step: \(.displayName)\tMessage: \(.message)"'## Will print a list of all the failed steps and their messages. For more info look up the jq docs.# Note: jq is not installed by default on the "alpine:latest" image, however it can be installed with "apk add jq"args:["echo send e-mail: {{workflow.name}} {{workflow.status}} {{workflow.duration}}. Failed steps {{workflow.failures}}"]- name:celebratecontainer:image:alpine:latestcommand:[sh, -c]args:["echo hooray!"]- name:crycontainer:image:alpine:latestcommand:[sh, -c]args:["echo boohoo!"]
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".
apiVersion:argoproj.io/v1alpha1kind:Workflowmetadata:generateName:lifecycle-hook-tmpl-level-spec:entrypoint:maintemplates:- name:mainsteps:- - name:step-1hooks:running:# Name of hook does not matter# Expr will not support `-` on variable name. Variable should wrap with `[]`expression:steps["step-1"].status == "Running"template:httpsuccess:expression:steps["step-1"].status == "Succeeded"template:httptemplate:echo- - name:step2hooks:running:expression:steps.step2.status == "Running"template:httpsuccess:expression:steps.step2.status == "Succeeded"template:httptemplate:echo- name:echocontainer:image:alpine:3.6command:[sh, -c]args:["echo \"it was heads\""]- name:httphttp:# url: http://dummy.restapiexample.com/api/v1/employeesurl:"https://raw.githubusercontent.com/argoproj/argo-workflows/4e450e250168e6b4d51a126b784e90b11a0162bc/pkg/apis/workflow/v1alpha1/generated.swagger.json"
apiVersion:argoproj.io/v1alpha1kind:Workflowmetadata:generateName:lifecycle-hook-spec:entrypoint:mainhooks:exit:# if named exit, it'a an Exit handlertemplate:httptemplates:- name:mainsteps:- - name:step1template:heads- name:httphttp:# url: http://dummy.restapiexample.com/api/v1/employeesurl:"https://raw.githubusercontent.com/argoproj/argo-workflows/4e450e250168e6b4d51a126b784e90b11a0162bc/pkg/apis/workflow/v1alpha1/generated.swagger.json"