Skip to content

CI/CD 集成 ​

@neosjs/release 天然支持在各类 CI/CD 环境中以非交互方式自动执行发版。

自动 CI 检测与 --ci 模式 ​

工具使用 ci-info 自动检测当前是否处于 CI 环境(如 GitHub Actions、GitLab CI、CircleCI、Travis CI、Jenkins、Bitbucket Pipelines 等):

  • 检测到 CI 环境时,自动启用 ci: true;也可以随时通过 --ci 强制启用,或通过 --no-ci 强制关闭。
  • CI 模式下的行为差异:
    • 不清屏、不打印交互式欢迎框。
    • 跳过所有确认提示,全部启用步骤自动执行。
    • 未显式指定版本增量时,优先采用 changelog 插件根据可发布提交记录推荐的版本类型;若上个标签之后有新提交但没有可发布变更(如只有 docs / chore / ci),默认提示后以退出码 0 跳过发版(可通过 --no-changelog.requireReleasableCommits 关闭跳过)。
    • 界面语言跟随系统 locale:常见 CI Runner(如 Ubuntu / GitHub Actions)默认为 C.UTF-8 或 en_US.UTF-8,因此 CI 日志默认输出英文;如需中文日志,可设置 NEOS_RELEASE_LANG=zh-CN 或传 --lang zh-CN。
    • 任务列表在非 TTY 环境下仅输出状态,错误信息由顶层统一汇总打印一次。
    • npm publish 不继承终端交互流(如需在 CI 中免 2FA 提交包,见暂存发布 npm.stage)。
    • 若开启了 Git 标签签名但签名未生效,CI 模式会直接报错并删除该未签名标签。

混合模式(--only-version) ​

在本地手动发版时,如果只想自己确认版本号(以及 npm 发布确认 / OTP),其它步骤(更新日志、Git 提交/打标签/推送、创建 Release)全部自动执行,可以使用:

bash
release --only-version

脚本模式(--release-version / --only-changelog) ​

适合在自定义 CI 脚本中仅读取计算出的版本号或更新日志,而不直接触发发版流程:

bash
NEXT_VERSION=$(release --release-version)
release --only-changelog > CHANGELOG_SECTION.md

这两个选项会自动进入 CI 模式,且将所有辅助日志输出到 stderr,保证 stdout 只有纯净结果。

GitHub Actions 实战配置 ​

1. 手动触发自动发版(npm + GitHub Release) ​

yaml
name: Release

on:
  workflow_dispatch:
    inputs:
      increment:
        description: '版本递增类型(留空则按提交自动推荐)'
        required: false
        type: choice
        options:
          - ''
          - patch
          - minor
          - major
          - prepatch
          - preminor
          - premajor

permissions:
  contents: write
  issues: write
  pull-requests: write
  id-token: write

jobs:
  release:
    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 dependencies
        run: pnpm install --frozen-lockfile

      - name: Configure Git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

      - name: Run release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          pnpm release ${{ inputs.increment }} --ci --npm.publish --github.release

提示:在 GitHub Actions 中运行(存在 GITHUB_ACTIONS 环境变量)时,github 插件会自动跳过 Token 认证与协作者检查。

2. 开发分支自动发布 Canary 预览版 ​

yaml
name: Canary Release

on:
  push:
    branches:
      - develop

jobs:
  canary:
    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
      - run: pnpm build
      - name: Publish canary
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: pnpm release --canary --ci --npm.publish --npm.skipChecks

3. CI 暂存发布(npm.stage) ​

对于开启了强 2FA、不希望在 CI 中绕过审批直接上线的包,可以在 CI 中提交到 npm 暂存队列,再由维护者审批发布:

bash
pnpm release --ci --npm.publish --npm.stage

GitLab CI 实战配置 ​

GitLab CI 可直接使用内置 CI_JOB_TOKEN 创建 GitLab Release,无需申请额外个人令牌:

release.config.ts:

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

export default defineConfig({
  gitlab: {
    release: true,
    tokenRef: 'CI_JOB_TOKEN',
    tokenHeader: 'JOB-TOKEN'
  }
})

.gitlab-ci.yml:

yaml
release:
  image: node:22
  stage: deploy
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: manual
  variables:
    GIT_DEPTH: 0
  before_script:
    - corepack enable
    - pnpm install --frozen-lockfile
    - git config user.name "${GITLAB_USER_NAME}"
    - git config user.email "${GITLAB_USER_EMAIL}"
    - git remote set-url origin "https://oauth2:${GIT_PUSH_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
    - git checkout -B "${CI_COMMIT_REF_NAME}"
  script:
    - pnpm release --ci

更多 CI 场景指南 ​