Hooks
Hooks allow you to execute a custom command or script before or after a specified Stacktape command.
Example use cases:
- Building, validating, or linting your application before deployment.
- Running unit tests before deployment.
- Executing database migrations after deployment.
- Cleaning up a database before deletion.
How to use hooks
You can reference scripts defined in the scripts
section of your stacktape.yml
file within
a hook. Scripts can also be executed manually. For more information, see the
scripts documentation.
When a script is executed, it receives the following environment variables:
STP_HOOK_TYPE
:before
orafter
STP_COMMAND
: The Stacktape command that was used
These are in addition to the environment variables specified in the script definition and those injected when using
connectTo
.
scripts:buildWeb:type: local-scriptproperties:executeCommand: npx gatsby buildhooks:beforeDeploy:- scriptName: buildWeb
Hookable events
Before-command hooks
These hooks are executed before the specified command, but after the configuration has been successfully loaded.
A before-command hook is only executed if the configuration is loaded successfully and the hooks are registered. The hook will not be executed if there is an error in your CLI arguments or in your configuration file.
After-command hooks
These hooks are executed after the specified command has finished successfully. Common use cases for after-command hooks include database migration or seeding, and API or endpoint testing.
scripts:migrateDb:type: local-script-with-bastion-tunnelingproperties:executeScript: migrate.tsconnectTo:- myDatabasehooks:afterDeploy:- scriptName: migrateDbresources:myBastion:type: bastionmyDatabase:type: relational-databaseproperties:# database is only accessible from withing VPCaccessibility:accessibilityMode: vpcengine:type: postgresproperties:version: '16.2'primaryInstance:instanceSize: db.t3.microcredentials:masterUserPassword: my_secret_pass