Hooks
Introduction
- Hooks allow you to execute a custom command or script before or after specified command is executed.
- Example use cases:
- build, validate or lint your application before deployment
- run unit tests on your application before deployment
- execute database migration after deployment
- cleanup database before delete
Usage
Within hook, you can reference scripts specified in scripts section of your config.
Scripts can be referenced in a hook, but they can also be executed manually. Refer to scripts docs to learn more.
During execution the script will get the following environment variables:
STP_HOOK_TYPE
-before
orafter
STP_COMMAND
- Stacktape command used
This is in addition to the environment variables specified on the script definition and the environment variables injected when using connectTo.
NamedScriptLifecycleHook API reference
Parent:Hooks
scriptName
Required
skipOnCI
skipOnLocal
Copy
scripts:buildWeb:type: local-scriptproperties:executeCommand: npx gatsby buildhooks:beforeDeploy:- scriptName: buildWeb
Using script in the hook
Hookable events
Hooks API reference
beforeDeploy
afterDeploy
beforeDelete
afterDelete
beforeBucketSync
afterBucketSync
beforeDev
afterDev
Before command hooks
- Executed before the command, but AFTER the configuration has been successfully loaded.
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.
- Most common use case for using after deploy hooks is migration/seeding of the database, or API/endpoint testing.
Copy
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
Stacktape config using the migration script with tunneling