Hooks

Deployment hooks.

Hooks allow you to run a command or function before or after a CLI command is run. The config looks like:

const childProcess = require('child_process');

module.exports = {
  hooks: {
    hookName: {
      localCommand: 'command to run on computer',
      remoteCommand: 'command to run on every server'
    },
    'pre.deploy': {
      localCommand: 'npm prune --production'
    },
    'post.oreact.restart': {
      remoteCommand: 'docker logs --tail 50 app-name'
    },
    'pre.reconfig'(api) {
      // Same api as is given to plugin command handlers
      // If this runs asynchronous tasks, it needs to return a promise.
      const gitHash = childProcess.execSync('git rev-parse HEAD').toString().trim();

      api.getSettings();
      api.settings.GIT_HASH = gitHash;
    },
    'post.docker.setup'(api) {
      // Login to private Gitlab docker registry
      const config = api.getConfig();
      const registry = 'registry.gitlab.com';
      const username = process.env.REGISTRY_USERNAME;
      const password = process.env.REGISTRY_PASSWORD;

      if (!username || !password) {
        throw new Error(
          'You must provide registry login details'
        );
      }

      return api.runSSHCommand(
        config.servers.one,
        `docker login -u ${username} -p ${password} ${registry}`
      );
    }
  }
};

The hook name format is {pre or post}.topLevelCommand.subCommand. For example, if you want a command to run after orup deploy, the hook name would be post.deploy. Or if you want it to run after orup mongo restart, the hook name would be post.mongo.restart.

Some CLI commands run other CLI commands. For example, orup setup runs orup docker setup and orup mongo setup. To see all of the available hooks while a command runs, use the --show-hook-names option.