Skip to content

Lifecycle Hooks ​

Through the hooks configuration, you can automatically run arbitrary shell commands at key points of the release flow (for example running linting, unit tests or a project build, generating extra files, or sending a release notification).

Basic Usage ​

Add hook names and their commands to the hooks field of the configuration file (a single string or an array of strings is supported; commands in an array run one after another in order):

ts
import { defineConfig } from '@neosjs/release'

export default defineConfig({
  hooks: {
    'after:init': ['pnpm lint', 'pnpm typecheck', 'pnpm test'],
    'after:bump': 'pnpm build',
    'after:release': 'echo "🎉 Successfully released ${name} v${version} to ${repo.repository}"'
  }
})

${...} template variables are supported; see Configuration guide - Template interpolation for the complete list of variables.

Template Variable Values and the Shell ​

Hook commands are ultimately handed to the system shell, and some of the values of ${...} come from the repository itself (the branch name, the tag name, the changelog generated from commit messages, etc.). To prevent repository content from being executed as a command, follow these rules:

  • A value rendered into a command must not contain shell syntax: whitespace, quotes, $, `, ;, |, &, >, * and similar characters all count. A value consisting only of ordinary characters such as letters, digits and _ - . / : = , @ + is rendered as usual, for example ${version}, ${branchName}, ${repo.repository}.

  • When a value contains shell syntax, the whole command is not executed; the release fails and names the offending variable. For example, when the current branch name is feat/$(touch pwned) and the hook is echo "${branchName}":

    text
    Refused to run "echo "${branchName}"": the value of variable ${branchName} ("feat/$(touch pwned)") contains shell syntax and would be executed by the shell once rendered into the command. For multi-line text or content with special characters, use an environment variable instead (such as "$RELEASE_CHANGELOG")

    Wrapping the variable in quotes is refused just the same: a quote inside the value can end the quotation early.

  • Use environment variables for multi-line text or content with special characters. Template variables are also passed to hook commands as environment variables (with a RELEASE_ prefix and snake case naming, such as ${version} → $RELEASE_VERSION and ${repo.owner} → $RELEASE_REPO_OWNER); quoting the variable name in the command reads the value verbatim:

    ts
    hooks: {
      // The changelog is multi-line text containing backticks, & and other characters: use the environment variable, not ${changelog}
      'after:bump': 'printf "%s" "$RELEASE_CHANGELOG" > release-notes.md'
    }

    See Configuration guide - Template interpolation for the complete list of environment variables.

Hook Naming Rules ​

A hook name consists of 2 or 3 segments separated by colons ::

  • Global hooks: [before|after]:[hook]
    • Triggered before the stage starts for all plugins (before:*) or after the stage has finished for all plugins (after:*).
  • Plugin-level hooks: [before|after]:[plugin]:[hook]
    • Triggered before or after the given plugin runs the stage.
    • [plugin] can be the name of a built-in plugin (changelog, npm, git, pr, github, gitlab, version) or the namespace of an external plugin.

The standard [hook] stages are:

StageWhen it is triggered
initPlugin initialization and preflight checks (before the version is determined)
beforeBumpPreparation before the version is bumped (for example npm checking package.json for breaking changes)
bumpThe version bump (at this point ${version} has been determined)
beforeReleasePreparation before the release actions are performed (writing CHANGELOG.md, preparing the PR branch and staging changed files, preparing the release notes)
releasePerforming the release actions (npm publishing, Git commit/tag/push, creating a PR/MR, creating a GitHub/GitLab Release)
afterReleaseAfter all release actions have finished (printing the package link / Release link / PR link, cleanup and restoring)

If a plugin's lifecycle method explicitly returns false (for example npm.release() when npm.publish: false, or when the user chooses to skip in an interactive confirmation), the plugin's corresponding after:<plugin>:<hook> hook is skipped, but the global after:<hook> is not.

Complete Execution Order ​

Understanding the exact execution order of plugins and hooks helps you place each command where it fits best:

  1. init stage (plugin order: external plugins → changelog → npm → git → pr → github → gitlab → version)
    • before:init
    • For each plugin in turn: before:<plugin>:init → plugin.init() → after:<plugin>:init
    • after:init
  2. Version and changelog resolution (hooks are not triggered; getName, getLatestVersion, getIncrement, getIncrementedVersionCI / getIncrementedVersion, getChangelog are called in turn)
  3. beforeBump stage (plugin order: external plugins → built-in plugins; for example npm checks for breaking changes here)
    • before:beforeBump
    • For each plugin in turn: before:<plugin>:beforeBump → plugin.beforeBump() → after:<plugin>:beforeBump
    • after:beforeBump
  4. bump stage (plugin order: external plugins → changelog → npm → git → pr → github → gitlab → version)
    • before:bump
    • For each plugin in turn: before:<plugin>:bump → plugin.bump(version) → after:<plugin>:bump
    • after:bump
  5. beforeRelease stage (plugin order: external plugins → built-in plugins)
    • before:beforeRelease
    • For each plugin in turn: before:<plugin>:beforeRelease → plugin.beforeRelease() → after:<plugin>:beforeRelease
    • after:beforeRelease
    • What each plugin's beforeRelease() does:
      • changelog writes CHANGELOG.md
      • git prepares the PR branch (--pr mode) and stages changed files (git add)
      • github / gitlab prepare the release notes and validate milestones
  6. release stage (plugin order reversed: built-in plugins changelog → npm → git → pr → github → gitlab → version → external plugins)
    • before:release
    • For each plugin in turn: before:<plugin>:release → plugin.release() → after:<plugin>:release
    • after:release
  7. afterRelease stage (plugin order: built-in plugins → external plugins; printing the package link / Release link / PR link, closing network connections, restoring package.json with --canary)
    • before:afterRelease
    • For each plugin in turn: before:<plugin>:afterRelease → plugin.afterRelease() → after:<plugin>:afterRelease
    • after:afterRelease

Where to Place Common Hooks ​

1. Linting and tests → after:init ​

Time-consuming checks such as lint, typecheck and test are best placed in after:init rather than before:init:

  • The init stage first completes the clean working tree check, the branch check, the upstream sync check and the automatic npm login. If a precondition is not met, the release fails immediately instead of running the tests for nothing.
  • At after:init no version or file has been changed yet, so nothing needs to be rolled back when a test fails.
ts
hooks: {
  'after:init': ['pnpm lint', 'pnpm typecheck', 'pnpm test']
}

2. Build artifacts → after:bump or before:npm:release ​

  • If the build output depends on the new version number (for example when the build reads the version from package.json), or the output needs to be part of the release Git commit (for example building the docs or generating a manifest file), place it in after:bump (which runs before git stages files).
  • If the build output is listed in .gitignore (for example dist/) and is only used for publishing to npm or uploading as a Release asset, either after:bump or before:npm:release works; placing it in after:bump ensures that npm sees the latest build result when it runs the checkFiles artifact scan in the release stage.
ts
hooks: {
  'after:bump': 'pnpm build'
}

3. Release branch / PR follow-up automation → after:pr:release ​

In Release-via-PR mode, you can use the pr plugin's dedicated hook to add labels or notify reviewers automatically after a PR has been created or updated:

ts
hooks: {
  'after:pr:release': 'echo "Release PR ready: ${prUrl}"'
}

4. Release completion notification → after:release or after:github:release ​

ts
hooks: {
  'after:release': 'echo "Released ${name} v${version}"'
}

Hook Execution Details and Troubleshooting ​

  • Order and failing fast: multiple hook commands run serially in order. Any command that returns a non-zero exit code interrupts the release immediately and prints an error.
  • Dry run (--dry-run): all hook scripts are treated as write operations by default and are not actually executed in --dry-run mode; only the commands that would run are printed (prefixed with !).
  • Viewing command output: by default, hook execution is shown through a spinner. To see the stdout / stderr output of hook scripts in real time, add the -V (or --verbose) flag.
  • Shell differences: hook strings are executed in the system's default shell through Node.js child_process.exec, and support pipes, environment variable references and && chaining. For cross-platform projects, prefer calling package.json scripts (such as pnpm build) to reduce differences in shell syntax.
  • Mistyped templates: when a variable name is misspelled (such as ${verison}), the release fails and prints only the faulty template and the reason (Failed to render template "echo ${verison}": verison is not defined), not the context.