> ## Documentation Index
> Fetch the complete documentation index at: https://aspect.build/llms.txt
> Use this file to discover all available pages before exploring further.

# Run AXL before and after any task

> ctx.hooks.pre_task and ctx.hooks.post_task run AXL around every Aspect CLI task, and post-task hooks see how the task ended, even on exit or error.

[← Shipped](https://aspect.build/shipped)

# Run AXL before and after any task

***

![Pre-task and post-task hooks around a task body](https://mintlify.s3.us-west-1.amazonaws.com/aspectbuild/images/marketing/illustrations/shipped-axl-task-hooks.svg)

`ctx.defer` runs on every ending, but it never learns how the task ended. A notification, a status check, or a cleanup that depends on pass or fail had nowhere to read the outcome.

[`ctx.hooks`](https://aspect.build/docs/cli/guides/task-hooks) adds two seams to every task the Aspect CLI runs. `ctx.hooks.pre_task(fn)` runs right before the task body. `ctx.hooks.post_task(fn)` runs right after it with the task's `TaskConclusion`, whether the body returned, called `ctx.std.process.exit`, or raised an error.

```python .aspect/config.axl theme={null}
def _require_clean_tree(ctx: TaskContext) -> None:
    git = ctx.std.process.command("git").args(["status", "--porcelain"]).stdout("piped")
    if git.spawn().wait_with_output().stdout.strip():
        ctx.std.process.exit(1, "Commit or stash your changes before running CI tasks.")

def config(ctx: ConfigContext):
    ctx.hooks.pre_task(_require_clean_tree)
```

A pre-task hook that exits stands in for the body: the task never runs, and post-task hooks still see that conclusion.

## Where to register them

* **`config.axl`**, for policy that applies to every task in the repository.
* **A feature**, to reuse the behavior across repositories, such as posting each task's verdict to a webhook.
* **The task body**, post-task only, for cleanup that depends on the outcome, such as keeping a scratch directory when the task failed.

The built-in `build` and `test` tasks use a post-task hook too: a task that ends early closes its GitHub check, Buildkite annotation, or GitLab commit status with the real verdict and message.

[AXL](https://aspect.build/platform/axl) ships with the Aspect CLI, free. [Run code before and after a task](https://aspect.build/docs/cli/guides/task-hooks.md) has full examples and the order hooks run in, and [`TaskHooks`](https://aspect.build/docs/axl/types/task_hooks) is the reference.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.