在 pnpm 中使用 Changesets
在撰写本文档时,最新的 pnpm 版本是 v10.4.1。最新的 Changesets 版本是 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 工作区设置 changesets,请在工作区根目录将 changesets 安装为开发依赖:
🌐 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
- 运行
pnpm changeset version。这将提升之前使用pnpm changeset指定的软件包的版本(以及这些软件包的任何依赖),并更新变更日志文件。 - 运行
pnpm install。这将更新锁定文件并重新构建软件包。 - 提交更改。
- 运行
pnpm publish -r。此命令将发布所有版本已更新但尚未存在于注册表中的软件包。
与 GitHub Actions 集成
🌐 Integration with GitHub Actions
为了自动化这个流程,你可以在 GitHub Actions 中使用 changeset version。该 Action 会检测 main 分支中新提交的 changeset 文件,然后打开一个新的 PR,列出所有版本已升级的包。每当新的 changeset 文件提交到 main 时,PR 会自动更新。一旦合并,这些包就会被更新,如果在 Action 中指定了 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 添加一个新的工作流程。此工作流程将创建一个新的分支和拉取请求,因此应在仓库设置中为 Actions 授予 读写 权限(github.com/<repo-owner>/<repo-name>/settings/actions)。如果在 changesets/action 步骤中包含 publish 输入,仓库还应包含一个名为 NPM_TOKEN 的 npm 授权令牌作为仓库密钥。
.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 }}
关于 changesets 操作的更多信息和文档可以在这里找到。
🌐 More info and documentation regarding the changesets action can be found here.