Skip to main content

ctx.hook

The ctx.hook value tells a script which lifecycle hook triggered it and for which event, so one script can serve several hooks.

Usage​

ctx.hook.name
ctx.hook.event

Returns​

A value with two string attributes, or None when the script is not running as a hook:

name
The hook's name from the stack manifest.
event
The canonical event, such as before.terraform.plan or after.terraform.apply.

The value is set in kind: step and kind: steps hooks, and in type: test hooks that run script steps. It is available inside parallel tasks and loaded functions. Outside hooks it is None.

Examples​

Act only on one event​

hooks:
check:
events: [before.terraform.plan, after.terraform.apply]
kind: step
type: script
with:
interpreter: starlark
script: !include scripts/check.star
scripts/check.star
if ctx.hook.event == "before.terraform.plan":
ui.info("Planning " + ctx.component.name)
else:
ui.success("Applied " + ctx.component.name)

Log which hook ran​

log.debug("hook started", hook = ctx.hook.name, event = ctx.hook.event)

Support standalone testing​

hook_name = ctx.hook.name if ctx.hook != None else "(not a hook)"
print(hook_name)