Skip to main content
Version: 10.x

将变更集与 pnpm 结合使用

注意

在撰写本文档时,最新的 pnpm 版本是 v10.4.1。最新的 变更集 版本是 v2.28.0。

¥At the time of writing this documentation, the latest pnpm version was v10.4.1. The latest Changesets version was v2.28.0.

设置

¥Setup

要在 pnpm 工作区上设置变更集,请将变更集作为开发依赖安装在工作区的根目录中:

¥To setup changesets on a pnpm workspace, install changesets as a dev dependency in the root of the workspace:

pnpm add -Dw @changesets/cli

然后运行变更集的 init 命令以生成变更集配置:

¥Then run changesets' init command to generate a changesets config:

pnpm changeset init

添加新的变更集

¥Adding new changesets

要生成新的变更集,请在存储库的根目录中运行 pnpm changeset.changeset 目录中生成的 markdown 文件应提交到存储库。

¥To generate a new changeset, run pnpm changeset in the root of the repository. The generated markdown files in the .changeset directory should be committed to the repository.

发布变更

¥Releasing changes

  1. 运行 pnpm changeset version。这将提升之前使用 pnpm changeset 指定的软件包的版本(以及这些软件包的任何依赖)并更新变更日志文件。

    ¥Run pnpm changeset version. This will bump the versions of the packages previously specified with pnpm changeset (and any dependents of those) and update the changelog files.

  2. 运行 pnpm install。这将更新锁定文件并重建包。

    ¥Run pnpm install. This will update the lockfile and rebuild packages.

  3. 提交更改。

    ¥Commit the changes.

  4. 运行 pnpm publish -r。此命令将发布注册表中尚未存在的已升级版本的所有软件包。

    ¥Run pnpm publish -r. This command will publish all packages that have bumped versions not yet present in the registry.

与 GitHub Actions 集成

¥Integration with GitHub Actions

要自动化该过程,你可以将 changeset version 与 GitHub Actions 结合使用。该操作将检测变更集文件何时到达 main 分支,然后打开一个新的 PR,列出所有版本已更改的软件包。每次新的变更集文件到达 main 时,PR 都会自动更新。合并后,包将被更新,如果在操作中指定了 publish 输入,则将使用给定的命令发布它们。

¥To automate the process, you can use changeset version with GitHub actions. The action will detect when changeset files arrive in the main branch, and then open a new PR listing all the packages with bumped versions. The PR will automatically update itself every time a new changeset file arrives in main. Once merged the packages will be updated, and if the publish input has been specified on the action they will be published using the given command.

添加发布脚本

¥Add a publish script

添加一个名为 ci:publish 的新脚本,该脚本执行 pnpm publish -r。一旦 changeset version 创建的 PR 被合并,这将发布到注册表。如果包是公共的和有作用域的,则可能需要添加 --access=public 以防止 npm 拒绝发布。

¥Add a new script called ci:publish which executes pnpm publish -r. This will publish to the registry once the PR created by changeset version has been merged. If the package is public and scoped, adding --access=public may be necessary to prevent npm rejecting the publish.

package.json

{
"scripts": {
"ci:publish": "pnpm publish -r"
},
...
}

添加工作流

¥Add the workflow

.github/workflows/changesets.yml 添加新的工作流。此工作流程将创建一个新的分支和 PR,因此应在 repo 设置 (github.com/<repo-owner>/<repo-name>/settings/actions) 中为 Actions 授予读写权限。如果在 changesets/action 步骤中包含 publish 输入,则 repo 还应包含 npm 的身份验证令牌作为名为 NPM_TOKEN 的存储库密钥。

¥Add a new workflow at .github/workflows/changesets.yml. This workflow will create a new branch and PR, so Actions should be given read and write permissions in the repo settings (github.com/<repo-owner>/<repo-name>/settings/actions). If including the publish input on the changesets/action step, the repo should also include an auth token for npm as a repository secret named NPM_TOKEN.

.github/workflows/changesets.yml

name: Changesets

on:
push:
branches:
- main

env:
CI: true

jobs:
version:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- name: Checkout code repository
uses: actions/checkout@v4

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

- name: Setup node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'

- name: Install dependencies
run: pnpm install

- name: Create and publish versions
uses: changesets/action@v1
with:
commit: "chore: update versions"
title: "chore: update versions"
publish: pnpm ci:publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

有关变更集操作的更多信息和文档可以在 此处 中找到。

¥More info and documentation regarding the changesets action can be found here.