Using Hooks in Spacelift Modules
Last updated: November 10, 2024
Spacelift modules support hooks, allowing you to customize and automate actions at various stages of your workflow. Hooks can be configured directly in the module’s config.yml file or by attaching a context to the module. This guide explains how to set up and use hooks within Spacelift modules.
The configuration file must be named spacelift/config.yml. Spacelift will not recognize .yaml as a valid configuration file. Ensure the file is correctly named to enable hook functionality
Setting Up Hooks in a Module
Configure Hooks in
config.yml
Hooks can be set up in theconfig.ymlfile within your module configuration. This file allows you to define custom commands or scripts to run at specific stages, such as before or after the plan or apply phases.Example of a
.spacelift/config.ymlhook setup:version: "2" stack_defaults: runner_image: your/first:runner # Note that tflint is not installed by # default - this example assumes that your # runner image has this available. before_init: - echo "checking formatting" - terraform fmt -diff - tflintUse Contexts (optionally: with Auto-Attachment Labels)
You can also set up hooks through a context attached to the module. Contexts allow you to define reusable settings, including hooks, across multiple modules or stacks. By using an auto-attachment label, you can automatically apply the context to the module.