종료 핸들러 (Exit handlers)
종료 핸들러(exit handler)는 워크플로 맨 끝에서 성공 여부와 무관하게 항상 실행되는 템플릿입니다.
본문
종료 핸들러의 대표적인 사용 사례는 다음과 같습니다:
- 워크플로 실행 후 정리(cleanup)
- 워크플로 상태 알림 전송(예: 이메일/Slack)
- 웹훅 결과로 성공/실패 상태 게시(예: GitHub 빌드 결과)
- 워크플로 재제출 또는 다른 워크플로 제출
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: exit-handlers-
spec:
entrypoint: intentional-fail
onExit: exit-handler # invoke exit-handler template at end of the workflow
templates:
# primary workflow template
- name: intentional-fail
container:
image: alpine:3.23
command: [sh, -c]
args: ["echo intentional failure; exit 1"]
# Exit handler 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-handler
steps:
- - name: notify
template: send-email
- name: celebrate
template: celebrate
when: "{{workflow.status}} == Succeeded"
- name: cry
template: cry
when: "{{workflow.status}} != Succeeded"
- name: send-email
container:
image: alpine:3.23
command: [sh, -c]
args: ["echo send e-mail: {{workflow.name}} {{workflow.status}} {{workflow.duration}}"]
- name: celebrate
container:
image: alpine:3.23
command: [sh, -c]
args: ["echo hooray!"]
- name: cry
container:
image: alpine:3.23
command: [sh, -c]
args: ["echo boohoo!"]
더 알아보기 (Learn more)
spec.onExit에 템플릿 이름을 지정하면 워크플로 끝에서 항상 실행됩니다.{{workflow.status}}는 Succeeded, Failed, Error 중 하나이며when조건으로 분기할 수 있습니다.- 정리 작업이나 알림은 실패해도 반드시 실행되므로 exit handler에 두는 것이 안전합니다.