前一段时间 i18next 团队发布了新的 i18next-cli 工具,应该是想整合 key 的提取以及工作流,除了结束各种提取器配置不兼容、实现不一致等各种问题之外,还有(画了一张大饼)lint 检查、自动翻译等其他后续能发力的体验提升。于是想着把之前实现的 vue extracting 给用 i18next-cli 插件机制重新实现一下,相关包已发布到 https://github.com/PBK-B/i18next-cli-vue 有需要的小伙伴自取。

好了不扯远了,当我写完之后发布包的时候就想试试 npm (微软) 强推的可信发布流程,想着配置一下反正这个包是开源且托管在 Github 上的应该就配置一下 GitHub Actions OIDC 让它直接发布就可以了(文档也是这么说的),结果折腾两三个小时!(大半夜的越想越气) 我的建议是 NPM 好好写写您的文档吧实在不行让 AI 写,估计找到这里的小伙伴也是被坑半天了。不扯了,我直接完整写配置流程。

首先配置好 github 仓库以及 GitHub Actions (按照正常流程保证仓库能成功编译),打开 npm 包设置页面

配置 NPM 可信 (Trusted Publisher) 发布 GitHub Actions OpenID Connect (OIDC) 趟坑笔记-天真的小窝

第一个 Publisher 选择 GitHub Actions,用户或组织名以及仓库地址,最后 Workflow filename 填你用于编译的 GitHub Actions Workflow yml 文件。(Environment name 没有特殊需求不用填),最后保存即可。

第二步: 在你的编译 yml 文件中增加 permissions.id-token 配置(比如我这里是 publish.yml),使用 npm publish --provenance --access public 指令发布

permissions:
    id-token: write
…
jobs:
    publish:
        name: Build & Publish to npm & GitHub Release
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v6

            - name: Setup Node.js
              uses: actions/setup-node@v6
              with:
                  node-version: "25"
                  registry-url: "https://registry.npmjs.org"

            - name: Install dependencies
              run: npm ci

            - name: Build
              run: npm run build

            - name: Publish to npm
              run: npm publish --provenance --access public

这里需要注意如果之前就配置的 npm token 发布的我的建议是 删掉。镜像最好使用比较新的actions/setup-node@v6 以及 node (npm) 版本稍微高一点,如果项目没有什么版本锁定的话,建议直接上 25,如果实在无法升级 node 也建议 ci 中执行 `npm install -g npm@latest` 升级一下 npm

Note: Trusted publishing requires npm CLI version 11.5.1 or later.

第三步: 在 package.json 中添加 provenanceaccess 字段。

{
    …
	"publishConfig": {
		"registry": "https://registry.npmjs.org",
		"access": "public",
		"provenance": true
	}
}

前面全部配置好后,我发了好几个测试包都一直报错 404。本地手动发布又没有问题,我是 node 版本也换了好几个 npm 版本也换了好几次,甚至怀疑是包名的问题后面又另外注册了一个包。结果在 GitHub Actions 中一直报错 Access token expired or revoked. Please try logging in again。

npm notice
npm notice 📦  @i18next-plugin/vue@1.0.2
npm notice Tarball Contents
npm notice 1.1kB LICENSE
npm notice 4.7kB README.md
npm notice 4.5kB README.zh-CN.md
npm notice 437B dist/index.d.ts
npm notice 10.5kB dist/index.js
npm notice 26.7kB dist/index.js.map
npm notice 325B dist/options.d.ts
npm notice 433B dist/script/extract.d.ts
npm notice 239B dist/sfc/parser.d.ts
npm notice 161B dist/sfc/vue2.d.ts
npm notice 161B dist/sfc/vue3.d.ts
npm notice 148B dist/template/extract.d.ts
npm notice 2.1kB dist/types.d.ts
npm notice 299B dist/utils/index.d.ts
npm notice 1.7kB package.json
npm notice Tarball Details
npm notice name: @i18next-plugin/vue
npm notice version: 1.0.2
npm notice filename: i18next-plugin-vue-1.0.2.tgz
npm notice package size: 12.4 kB
npm notice unpacked size: 53.6 kB
npm notice shasum: 2405b0d3db651f29df6374a6013e5c1a873c7b22
npm notice integrity: sha512-79gB4FSQxwO8N[...]Uq0y9yA/XC5Ig==
npm notice total files: 15
npm notice
npm notice Publishing to https://registry.npmjs.org with tag latest and public access
npm notice publish Signed provenance statement with source and build information from GitHub Actions
npm notice publish Provenance statement published to transparency log: https://search.sigstore.dev/?logIndex=872351552
npm notice Access token expired or revoked. Please try logging in again.
npm error code E404
npm error 404 Not Found - PUT https://registry.npmjs.org/@i18next-plugin%2fvue - Not found
npm error 404
npm error 404  '@i18next-plugin/vue@1.0.2' is not in this registry.
npm error 404
npm error 404 Note that you can also install from a
npm error 404 tarball, folder, http url, or git url.
npm error A complete log of this run can be found in: /home/runner/.npm/_logs/2026-01-30T16_49_07_814Z-debug-0.log
Error: Process completed with exit code 1.

其他博客一直在说另外配置一个 npm token 用来保底(我建议千万别这么干!),然后各种处理手段也就是我上面说的几个来来回回,直到我终于想起了去 npm issue 看看。然后发现一老哥提到了 provenance 这个配置项 https://github.com/npm/cli/issues/8730#issuecomment-3538909998 才终于发布上去了。

我真是,哎。

参考文档:

https://docs.npmjs.com/trusted-publishers

https://docs.github.com/en/actions/concepts/security/openid-connect