Stacktape

Sign up

Stacktape

Sign up



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 or after
  • 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.

scriptName
Required
skipOnCI
skipOnLocal

Copy

scripts:
buildWeb:
type: local-script
properties:
executeCommand: npx gatsby build
hooks:
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-tunneling
properties:
executeScript: migrate.ts
connectTo:
- myDatabase
hooks:
afterDeploy:
- scriptName: migrateDb
resources:
myBastion:
type: bastion
myDatabase:
type: relational-database
properties:
# database is only accessible from withing VPC
accessibility:
accessibilityMode: vpc
engine:
type: postgres
properties:
version: '16.2'
primaryInstance:
instanceSize: db.t3.micro
credentials:
masterUserPassword: my_secret_pass

Stacktape config using the migration script with tunneling

Need help? Ask a question on Discord or info@stacktape.com.