Issue #70

使用GitHub Actions自动发布npm包需要注意的点人工主写

近 30 天0 次浏览0 位访客统计截至 2026-10-19 · GA4

以我经常使用的 antfu 的 https://github.com/antfu/starter-ts 这个 template 为例,这个项目的发版工作流是:

name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    uses: sxzz/workflows/.github/workflows/release.yml@v1
    with:
      publish: true
    permissions:
      contents: write
      id-token: write

https://github.com/sxzz/workflows/blob/v1/.github/workflows/release.yml 这个工作流具体的内容是:

name: Release

on:
  workflow_call:
    inputs:
      publish:
        required: false
        type: boolean
        default: false
      build:
        required: false
        type: string
        default: ''

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest

    permissions:
      contents: write
      id-token: write

    steps:
      - name: Setup JS
        uses: sxzz/workflows/setup-js@v1
        with:
          fetch-all: true

      - name: Generate GitHub Changelog
        run: pnpx changelogithub
        continue-on-error: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build
        run: ${{ inputs.build }}
        if: ${{ inputs.build && inputs.publish }}

      - name: Publish to NPM
        run: pnpm -r publish --access public --no-git-checks
        if: ${{ inputs.publish }}

完成了对比版本 tag 之间根据 commit msg 生成 changelog 、npm 发版的操作。

在 https://github.com/antfu/starter-ts/blob/main/README.md 有以下提示:

IMG-7BF0639551873F6F24C0419B456950D2.png

我们编写好了想要发布的发布的包之后,需要手动 npm publish 一下。

注意点 1:包名与权限配置

package.json 中的 name 就是我们的 npm 包名,不允许和其他包重复。并且即使没有重复也可能有 npm 不接受的关键词。

引用自 npm 发包失败的原因:

Npm 发包失败的原因

可能的原因

  1. 当前的账号没有发包权限。(比如没有某个包或者某个 scope 的权限)
  2. 当前的包名不符合规范。(需要 URL-safe characters)
  3. 当前的包名是屏蔽词。(例如 download
  4. 可能是当前的 npm 设置了镜像地址,而不是原始的 npm 地址,导致 login 的时候无法登录从而无法发布。解决方法就是在发布的时候先切换为原始的 npm 地址,再发布。

// 吐槽一下,npm 的 cli 给的信息很多都是无效信息,有时候很难从中知道到底是哪里有问题

Npm 包名屏蔽词

  1. download (我替换成 downloader 之后才成功发布)

参考

[BUG] npm publish get 400 Bad Request without any useful information · Issue #6090 · npm/cli · GitHub

注意点 2

如果你之前配置过 npm 的镜像源,此时要发布到 npm 上,则需要切换回 npm 原始的源。

方法 1:安装 nrm

npm install -g nrm

nrm use npm

# 发布完之后可以切换回镜像源
nrm use taobao

方法 2:手动切换 npm 源

npm config set registry https://registry.npmjs.org/

# 发布完之后可以手动切换回镜像
npm config set registry https://registry.npmmirror.com/

发布好之后可以去 npm 设置 Trusted Publisher

注意 3 Trusted Publisher 的 repo 只需要填写 repo name

需要注意的是:这里的 repo 只需要填写 repo name 即可,不需要填写完整路径。 比如 https://github.com/KazooTTT/qkpr 那么只需要填写 pkqr 即可,而不是 https://github.com/KazooTTT/qkpr 或者 github. com/KazooTTT/qkpr

IMG-987B468B36E82E9235A1098D11B310A6

总的来说,通过 GitHub Actions 自动发布 npm 包需要注意配置正确的权限、设置合适的镜像源、遵循包名规范,并正确配置 Trusted Publisher,这样才能确保发布流程顺利进行。

引用链接

  1. https://github.com/antfu/starter-ts
  2. https://github.com/sxzz/workflows/blob/v1/.github/workflows/release.yml
  3. https://github.com/antfu/starter-ts/blob/main/README.md
  4. [BUG] npm publish get 400 Bad Request without any useful information · Issue #6090 · npm/cli · GitHub
  5. https://github.com/KazooTTT/qkpr

讨论

0 条留言

去 GitHub 留言 ↗

还没有留言,欢迎来打个招呼。

回到顶部