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):
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 isecho "${branchName}":textRefused 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_VERSIONand${repo.owner}→$RELEASE_REPO_OWNER); quoting the variable name in the command reads the value verbatim:tshooks: { // 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:*).
- Triggered before the stage starts for all plugins (
- 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:
| Stage | When it is triggered |
|---|---|
init | Plugin initialization and preflight checks (before the version is determined) |
beforeBump | Preparation before the version is bumped (for example npm checking package.json for breaking changes) |
bump | The version bump (at this point ${version} has been determined) |
beforeRelease | Preparation before the release actions are performed (writing CHANGELOG.md, preparing the PR branch and staging changed files, preparing the release notes) |
release | Performing the release actions (npm publishing, Git commit/tag/push, creating a PR/MR, creating a GitHub/GitLab Release) |
afterRelease | After 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 examplenpm.release()whennpm.publish: false, or when the user chooses to skip in an interactive confirmation), the plugin's correspondingafter:<plugin>:<hook>hook is skipped, but the globalafter:<hook>is not.
Complete Execution Order
Understanding the exact execution order of plugins and hooks helps you place each command where it fits best:
initstage (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
- Version and changelog resolution (
hooksare not triggered;getName,getLatestVersion,getIncrement,getIncrementedVersionCI/getIncrementedVersion,getChangelogare called in turn) beforeBumpstage (plugin order: external plugins → built-in plugins; for examplenpmchecks for breaking changes here)before:beforeBump- For each plugin in turn:
before:<plugin>:beforeBump→plugin.beforeBump()→after:<plugin>:beforeBump after:beforeBump
bumpstage (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
beforeReleasestage (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:changelogwritesCHANGELOG.mdgitprepares the PR branch (--prmode) and stages changed files (git add)github/gitlabprepare the release notes and validate milestones
releasestage (plugin order reversed: built-in pluginschangelog→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
afterReleasestage (plugin order: built-in plugins → external plugins; printing the package link / Release link / PR link, closing network connections, restoringpackage.jsonwith--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
initstage 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:initno version or file has been changed yet, so nothing needs to be rolled back when a test fails.
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
versionfrompackage.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 inafter:bump(which runs beforegitstages files). - If the build output is listed in
.gitignore(for exampledist/) and is only used for publishing to npm or uploading as a Release asset, eitherafter:bumporbefore:npm:releaseworks; placing it inafter:bumpensures thatnpmsees the latest build result when it runs thecheckFilesartifact scan in thereleasestage.
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:
hooks: {
'after:pr:release': 'echo "Release PR ready: ${prUrl}"'
}4. Release completion notification → after:release or after:github:release
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-runmode; 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/stderroutput 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 callingpackage.jsonscripts (such aspnpm 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.