Release-via-PR
When a repository has branch protection enabled on a default branch such as main (Branch Protection: every change must go through a Pull Request / Merge Request with reviews and status checks before it can be merged, and pushing to the default branch directly from a local clone is not allowed), you can use the built-in pr plugin to prepare the release through Release-via-PR mode.
For a complete end-to-end setup (including a GitHub Actions pipeline that automatically tags, publishes to npm, and creates the GitHub Release after the PR is merged), see Protected Branch PR Pipeline.
Quick Start
# Cut a release/v<version> branch, commit the version and CHANGELOG, push, and create/update the PR or MR
release --pr
# Create a draft PR / MR
release --pr --pr.draft
# Specify the increment type and the target base branch
release minor --pr --pr.base=mainYou can also enable it permanently in your config file:
import { defineConfig } from '@neosjs/release'
export default defineConfig({
pr: {
enabled: true,
branch: 'release/v${version}',
base: 'main',
title: 'chore: release v${version}',
draft: false,
forcePush: true
}
})The boolean shorthand
pr: trueis also supported. If your config file containspr: { draft: true }and you then pass--pron the command line, it is automatically normalized to--pr.enabledand deep-merged with the local object configuration.
Execution Flow and Automatic Isolation
When Release-via-PR mode is enabled, @neosjs/release automatically turns off the following local direct-release actions and leaves the subsequent publishing to CI once the PR is merged:
git.tag = falsenpm.publish = falsegithub.release = falsegitlab.release = false
The complete execution flow is as follows:
- Platform preflight check (
pr.init): identifies the target platform (GitHub or GitLab) up front from thegit remoteandgithub.host/gitlab.origin, so that an unrecognizable platform is not discovered only after the branch has been pushed. - Version bump and changelog update (
bump/beforeRelease): updates thepackage.jsonversion and writesCHANGELOG.md. - Prepare the release branch (
git.beforeRelease):- Verifies that you are on a named branch (detached HEAD is not supported) and that
pr.branchis not the same aspr.base. - Records the current original branch (
originalBranch) and the previous state of the target release branch, then cuts the release branch withgit checkout -B \<prBranch\>and stages the changes.
- Verifies that you are on a named branch (detached HEAD is not supported) and that
- Commit and push the release branch (
git.release):- Creates the Git commit on the release branch.
- Runs
git push [--force-with-lease] --set-upstream \<remote\> \<prBranch\>(--force-with-leaseis added whenpr.forcePush !== false, which supports re-running to update the same release branch). - After a successful push, automatically switches back to the original branch with
git checkout \<originalBranch\>.
- Create or update the PR / MR (
pr.release):- Automatically detects whether an open PR / MR already exists from
prBranch -> baseBranch: if so, its title and body are updated; otherwise a new one is created. - Writes the result to the context (
prUrl,prAction,prMethod).
- Automatically detects whether an open PR / MR already exists from
- Print the link (
pr.afterRelease): prints the URL of the created / updated PR / MR to the terminal.
Platform Authentication and Automatic Fallback
GitHub
The method for creating / updating is selected automatically in the following order of priority:
- GitHub REST API (Octokit): when
github.web: trueis not set andGITHUB_TOKENexists (or the variable specified bygithub.tokenRef), the API is called directly to find / create / update the PR (supports automatic retries, proxies, and the Enterprisegithub.host). ghCLI fallback: when no token is configured andgithub.web: trueis not set,gh auth statusis checked automatically on the local machine; if you are logged in,gh pr list/gh pr create/gh pr editare used to complete the operation.- Browser web fallback: when neither of the above is available, or
github.web: trueis set, the GitHub Compare new-PR page opens automatically in the default browser with the title and the changelog body pre-filled (under--cior--dry-runmode, only the link is printed).
GitLab
Selected in the following order of priority:
- GitLab REST API v4: when
GITLAB_TOKENexists (orgitlab.tokenRef),projects/:id/merge_requestsis called to query / create / update the MR (withpr.draft: true, theDraft:prefix is added to the title automatically; supports self-hostedgitlab.origin,gitlab.repoId, CA certificates, and proxies). - Browser web fallback: when there is no token, the GitLab new-MR page opens automatically in the browser with the source branch, target branch, title, and description pre-filled (under
--cior--dry-runmode, only the link is printed).
Which Host the Token Is Sent To
The same rule as when publishing a Release: a token from an environment variable is only sent to the official domains (github.com / gitlab.com), the host explicitly specified in the configuration (github.host / gitlab.origin), or the address that the CI platform declares for itself (GITHUB_SERVER_URL / CI_SERVER_URL). When the git remote points to a self-hosted instance or another domain, the token is not sent merely because the host name contains github / gitlab: if a token is present in the environment but the host is not declared with github.host / gitlab.origin, pr.init fails immediately instead of falling back to the gh CLI or the browser (--dry-run prints a notice as well). See GitHub Releases / GitLab Releases for details.
Automatic Rollback on Failure or Cancellation
When git.requireCleanWorkingDir: true (the default), if an exception occurs while committing, pushing, or in the PR flow, or if the user presses Ctrl+C to cancel:
- the working tree changes are reset (
git reset --hard HEAD); - the original branch (
originalBranch) is checked out again automatically; - if
prBranchis a local branch created during this run, it is deleted automatically (git branch -D \<prBranch\>); if a local branch of the same name already existed before, it is restored to the commit it pointed to before the operation (git branch -f \<prBranch\> \<previousSha\>).
If the user deliberately skips the "push the release branch" step in interactive mode, the tool also switches back to the original branch automatically.
Dedicated Lifecycle Hooks
pr is a separate built-in plugin and supports the full set of plugin-level hooks (see Lifecycle Hooks):
before:pr:init/after:pr:initbefore:pr:bump/after:pr:bumpbefore:pr:release/after:pr:release
In after:pr:release you can reference variables such as ${prUrl}, ${prBranch}, ${baseBranch}, and ${version} directly, for example to add a label to the PR or send a notification automatically:
export default {
pr: {
enabled: true
},
hooks: {
'after:pr:release': 'gh pr edit "${prUrl}" --add-label release'
}
}Options
| Option | Type | Default | Description |
|---|---|---|---|
pr.enabled | boolean | false | Whether to enable Release-via-PR mode (the shorthand pr: true also works at the top level) |
pr.branch | string | 'release/v${version}' | Release branch name template |
pr.base | string | '' | Target branch to merge into (supports template variables; defaults to the current branch when empty) |
pr.title | string | 'chore: release v${version}' | PR / MR title template |
pr.body | string | Function | null | null | PR / MR body template or function (receives the context; defaults to the changelog for this release when empty) |
pr.draft | boolean | false | Whether to create the PR / MR as a draft |
pr.forcePush | boolean | true | Whether to update an existing remote release branch of the same name with --force-with-lease |
Complete Default Configuration
{
pr: {
enabled: false,
branch: 'release/v${version}',
base: '',
title: 'chore: release v${version}',
body: null,
draft: false,
forcePush: true
}
}Limitation:
--prcannot be enabled together with--canaryor--snapshot.