# Custom Hooks

You can provide custom hooks by placing .js files into the hooks directory.

# Custom Hook File

The hook file must be a plain javascript file and export following properties:

# type

  • Type: string
  • Requirements:
    • Must match regex /^[a-zA-Z]+[a-zA-Z0-9-_]*$/

Type of the hook.

# init

  • Type: function

A function that initializes the hook with properties given in a stack group or stack configuration file. The function can be either normal or async, and must return an instantiated hook object.

# Hook Object

Hook object must implement execute function that is invoked when the hook is executed. The function can be either normal or async, and must return an object with properties: message and success. The message is a string and can be used to return some human readable information from the hook. The success is a boolean determining if the hook was successful.

The hook function is invoked with a single argument containing the following properties:

# stage

  • Type: string
  • Possible values: before, after

Stage of the command during which the hook is executed.

# operation

  • Type: string
  • Possible values: create, update, delete

Operation of the command during which the hook is executed.

# status

  • Type: string
  • Possible values: success, failed, skipped, cancelled.

Result of the command during which the hook is executed.

# variables

  • Type: object

Mutable variables object containing command line and environment variables. The hook can modify existing variables and add new ones. After the hook is completed, the same variables object is passed to the subsequent hooks which can then access its contents. If the stack's template is .hbs file, the same variables are also usable there.

# ctx

  • Type: object

Command context.

# Examples

An example of a custom hook with type "my-special-hook" placed into hooks dir.

module.exports = {
  type: "my-special-hook",
  init: (props) => {
    console.log("Initialize my special hook");
    return {
      execute: (input) => {
        console.log("Execute special hook!");
        console.log(`Stage:     ${input.stage}`);
        console.log(`Operation: ${input.operation}`);
        console.log(`Status:    ${input.status}`);
        console.log(JSON.stringify(props, null, 2));
        
        return {
          message: "OK",
          success: true
        };
      }
    }
  }
};

This hook can then be used in a stack group or stack configuration files like so:

hooks:
  - name: my-hook
    type: my-special-hook
Last Updated: 5/4/2020, 3:54:44 PM