Skip to content

仅在存在新提交时发版 ​

在定时 CI 任务(如每日/每周自动发版)或目录触发的流水线中,如果没有自上次发布以来的新提交,通常不希望发布空版本,也不希望 CI 任务因为「无新提交」而显示红色失败。

基础配置 ​

在 release.config.ts 中开启 git.requireCommits,并将 git.requireCommitsFail 设为 false:

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

export default defineConfig({
  git: {
    requireCommits: true,
    requireCommitsFail: false
  }
})

执行效果:

  • 工具在 git.init 阶段执行 git rev-list <latestTag>..HEAD --count 统计新增提交数。
  • 当新提交数大于 0 时,正常进入发版流程。
  • 当新提交数为 0 时:
    • 终止后续插件初始化与全部发布步骤;
    • 提示「无需发布 / 没有新的提交记录, 无需发版!」;
    • 进程以退出码 0 正常退出,CI 任务保持绿色通过状态。

如果你希望无新提交时让脚本报错中止(退出码 1),保持默认的 git.requireCommitsFail: true 即可。

仅关注特定目录的变更(git.commitsPath) ​

如果只关心核心代码目录的改动(例如文档或配置文件改动不触发发版),可以配合 git.commitsPath 指定统计路径:

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

export default defineConfig({
  git: {
    requireCommits: true,
    requireCommitsFail: false,
    commitsPath: 'src'
  }
})

此时执行的统计命令等价于 git rev-list <latestTag>..HEAD --count -- src:只有 src/ 目录下存在新提交时才会继续发版。

定时 GitHub Actions 示例 ​

yaml
name: Scheduled Release

on:
  schedule:
    # 每周一 UTC 02:00 检查是否需要发版
    - cron: '0 2 * * 1'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm
          registry-url: https://registry.npmjs.org
      - run: pnpm install --frozen-lockfile
      - name: Release if there are new commits
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          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 --ci --git.requireCommits --no-git.requireCommitsFail --npm.publish --github.release