Skip to content

Release-via-PR Pipeline for a Protected Default Branch ​

If the main branch of your repository has branch protection rules enabled (direct git push is forbidden, and merging requires PR approval and passing status checks), you can combine the following three built-in capabilities of @neosjs/release to set up a complete closed loop:

  1. --pr (Release-via-PR): on your local machine (or triggered manually in CI), cut a release/v<version> branch, bump the version, update CHANGELOG.md, and automatically create/update the PR.
  2. --no-increment: after the PR is merged back into main, GitHub Actions tags the new version, pushes the tag, and publishes to npm.
  3. --sync-releases: reads the CHANGELOG.md section for the version that was reviewed and merged in the PR and automatically creates the GitHub Release.

Step 1: Project Configuration (release.config.ts) ​

ts
import { type ConfigOptions, defineConfig } from '@neosjs/release'

export default defineConfig({
  changelog: {
    generate: true,
    infile: 'CHANGELOG.md'
  },
  git: {
    commitMessage: 'chore: release v${version}',
    tagName: 'v${version}',
    requireBranch: 'main',
    requireCommits: true,
    requireCommitsFail: false
  },
  pr: {
    branch: 'release/v${version}',
    base: 'main',
    title: 'chore: release v${version}',
    forcePush: true
  },
  npm: {
    publish: false
  },
  github: {
    release: true
  }
}) satisfies ConfigOptions

Step 2: Open the Release PR ​

The maintainer runs the following on a clean local main branch that is in sync with upstream:

bash
pnpm release --pr

The tool then automatically:

  • Recommends and confirms the target version (for example 1.4.0);
  • Updates package.json and CHANGELOG.md;
  • Cuts the release/v1.4.0 branch, commits, and pushes it to the remote, then switches back to main;
  • Creates the Pull Request from release/v1.4.0 to main automatically through GITHUB_TOKEN (or a logged-in gh CLI, or the browser).

If additional commits land during review or you want to adjust the version number, simply run pnpm release --pr again; the tool updates the release branch with --force-with-lease and updates the existing PR in place.

Step 3: After the PR Is Merged, GitHub Actions Tags and Publishes Automatically ​

Add .github/workflows/publish-on-release-pr.yml:

yaml
name: Publish Release

on:
  push:
    branches:
      - main

permissions:
  contents: write
  id-token: write

jobs:
  publish:
    # Trigger only when the release commit is merged into main (avoids being triggered by ordinary commits)
    if: startsWith(github.event.head_commit.message, 'chore: release v')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm
          registry-url: https://registry.npmjs.org

      - name: Install & Build
        run: |
          pnpm install --frozen-lockfile
          pnpm build

      - name: Read version & check existing tag
        id: meta
        run: |
          VERSION=$(node -p "require('./package.json').version")
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
          if git rev-parse "v${VERSION}" >/dev/null 2>&1; then
            echo "exists=true" >> "$GITHUB_OUTPUT"
          else
            echo "exists=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Tag, Push & Publish to npm
        if: steps.meta.outputs.exists == 'false'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          pnpm release --no-increment --no-pr --ci --no-git.commit --no-git.requireCommits --npm.publish --no-github.release

      - name: Create GitHub Release from CHANGELOG.md
        if: steps.meta.outputs.exists == 'false'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          pnpm release --sync-releases="v${{ steps.meta.outputs.version }}" --github.release --ci

Why Run It in Two Steps? ​

  1. The first step uses --no-increment --no-git.commit --npm.publish --no-github.release: it leaves the package.json version and the CHANGELOG.md content that came in with the merged PR untouched, and only runs npm publish, creates the annotated tag v<version>, and pushes the tag.
  2. The second step uses --sync-releases="v<version>" --github.release: it parses the CHANGELOG.md section for that version, which has already been reviewed manually in the PR, and creates the GitHub Release precisely from it, keeping it fully consistent with CHANGELOG.md.