Skip to content

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 ​

bash
# 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=main

You can also enable it permanently in your config file:

ts
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: true is also supported. If your config file contains pr: { draft: true } and you then pass --pr on the command line, it is automatically normalized to --pr.enabled and 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 = false
  • npm.publish = false
  • github.release = false
  • gitlab.release = false

The complete execution flow is as follows:

  1. Platform preflight check (pr.init): identifies the target platform (GitHub or GitLab) up front from the git remote and github.host / gitlab.origin, so that an unrecognizable platform is not discovered only after the branch has been pushed.
  2. Version bump and changelog update (bump / beforeRelease): updates the package.json version and writes CHANGELOG.md.
  3. Prepare the release branch (git.beforeRelease):
    • Verifies that you are on a named branch (detached HEAD is not supported) and that pr.branch is not the same as pr.base.
    • Records the current original branch (originalBranch) and the previous state of the target release branch, then cuts the release branch with git checkout -B \<prBranch\> and stages the changes.
  4. 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-lease is added when pr.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\>.
  5. 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).
  6. 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:

  1. GitHub REST API (Octokit): when github.web: true is not set and GITHUB_TOKEN exists (or the variable specified by github.tokenRef), the API is called directly to find / create / update the PR (supports automatic retries, proxies, and the Enterprise github.host).
  2. gh CLI fallback: when no token is configured and github.web: true is not set, gh auth status is checked automatically on the local machine; if you are logged in, gh pr list / gh pr create / gh pr edit are used to complete the operation.
  3. Browser web fallback: when neither of the above is available, or github.web: true is set, the GitHub Compare new-PR page opens automatically in the default browser with the title and the changelog body pre-filled (under --ci or --dry-run mode, only the link is printed).

GitLab ​

Selected in the following order of priority:

  1. GitLab REST API v4: when GITLAB_TOKEN exists (or gitlab.tokenRef), projects/:id/merge_requests is called to query / create / update the MR (with pr.draft: true, the Draft: prefix is added to the title automatically; supports self-hosted gitlab.origin, gitlab.repoId, CA certificates, and proxies).
  2. 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 --ci or --dry-run mode, 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 prBranch is 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:init
  • before:pr:bump / after:pr:bump
  • before: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:

ts
export default {
  pr: {
    enabled: true
  },
  hooks: {
    'after:pr:release': 'gh pr edit "${prUrl}" --add-label release'
  }
}

Options ​

OptionTypeDefaultDescription
pr.enabledbooleanfalseWhether to enable Release-via-PR mode (the shorthand pr: true also works at the top level)
pr.branchstring'release/v${version}'Release branch name template
pr.basestring''Target branch to merge into (supports template variables; defaults to the current branch when empty)
pr.titlestring'chore: release v${version}'PR / MR title template
pr.bodystring | Function | nullnullPR / MR body template or function (receives the context; defaults to the changelog for this release when empty)
pr.draftbooleanfalseWhether to create the PR / MR as a draft
pr.forcePushbooleantrueWhether to update an existing remote release branch of the same name with --force-with-lease

Complete Default Configuration ​

ts
{
  pr: {
    enabled: false,
    branch: 'release/v${version}',
    base: '',
    title: 'chore: release v${version}',
    body: null,
    draft: false,
    forcePush: true
  }
}

Limitation: --pr cannot be enabled together with --canary or --snapshot.