Hooks
Introduction
- Hooks allow you to execute a custom command or script before specified command, after specified command or when a specified command fails.
- Example use cases:
- build, validate or lint your application before deployment
- execute database migration after deployment
- send a notification to a Slack channel after a deployment error
Defining script to execute
- The script is defined by its name. It must be specified in the
scripts
section of the configuration file. - Refer to scripts docs to learn how to define the script.
- The script will additionally get the following environment variables:
STP_HOOK_TYPE
-before
,after
oronError
STP_COMMAND
- Stacktape command used
Copy
hooks:- triggers: ["before:deploy"]scriptName: buildWebscripts:buildWeb:executeCommand: npx gatsby build
Hookable events
before:command hooks
- Executed before the command, but AFTER the configuration has been successfully loaded.
- Must have the format
before:[command]
.
Executed only if the configuration was successfully loaded and hooks have been registered. The hook won't be executed if there's an error with your CLI arguments or in your configuration.
after:command hooks
- Executed after the command has successfully finished.
- For convenience,
after:deploy
hook can reference parameters of your stack resources in its environment variables (using $ResourceParam() and $CfResourceParam() directives).
onError:command hooks
- Executed if the command has failed.
onError
hooks additionaly get the following environment variablesSTP_ERROR
containing data about the error:message
: human-readable messageerrorType
: type of the errorhints
: (for expected errors only) - list of hints that might help you solve the error.errorId
: (for unexpected errors only) - you can use this error id to report an issue
Executed only if the configuration was successfully loaded and hooks have been registered. The hook won't be executed if there's an error with your CLI arguments or in your configuration.
API reference
LifecycleHook API reference
triggers
Required
scriptName
Required