# 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

```python
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

```yaml
hooks:
  check:
    events: [before.terraform.plan, after.terraform.apply]
    kind: step
    type: script
    with:
      interpreter: starlark
      script: !include scripts/check.star
```

```python title="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

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

### Support standalone testing

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

## Related

- [`ctx.operation`](/functions/automation/ctx.operation) describes the operation the hook is attached to.
- [`ctx.component`](/functions/automation/ctx.component) is the hook's component.
- [`ctx`](/functions/automation/ctx) lists every context attribute.
- [Lifecycle hooks](/automation/lifecycle-hooks) and [stack hooks](/stacks/hooks)
