GitHub Packages: 公開用コマンドをnpm publishからbun publishに置き換える

#Github Packages#bun
2025/01/30
ArticleImage:01JJWCWVFJMRREP29R9DA73MNP

v1.2でS3のサポートなどが話題になっていたbunですが
1.2.x初のパッチリリース v1.2.1がリリースされました

このリリースではnpm, verdaccio以外のレジストリを対象にした際にbun publishがコケるバグが修正されています

npm publish->bun publish

今回対象にするパッケージはこちら

ちなむとこのパッケージは当ブログの最初の投稿でやりたいことに挙げていた専用プラグインです

このパッケージではパッケージマネージャ兼トランスコンパイラとしてbunを使用しているのですが
件のバグが理由でpublishコマンドのみnpm publishを利用していました
せっかくなのでこれを機にpublishコマンドもbunに統一していきます

GitHub Actions

リリース用ワークフローを修正

Copy
name: Release
on:
  release:
    types: [published]
jobs:
  publish:
    runs-on: $
    timeout-minutes: 30
    strategy:
      matrix:
        os: [ubuntu-latest]
    permissions:
      packages: write
      contents: read

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2
        with:
-          bun-version: latest
+          bun-version: 1.2.1  # 元々latestなので敢えて変更する必要はない

      - name: Install dependencies
        run: bun install

-      - name: Setup Node.js
-        uses: actions/setup-node@v4
-      - run: npm publish
-        env:
-          GITHUB_TOKEN: $

+      - name: Bun Publish
+        run: bun publish
+        env:
+          GITHUB_TOKEN: $

npmrcを使用しないパターン

Copy
name: Release
on:
  release:
    types: [published]
jobs:
  publish:
    runs-on: $
    timeout-minutes: 30
    strategy:
      matrix:
        os: [ubuntu-latest]
    permissions:
      packages: write
      contents: read

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.2.1
          registry-url: 'https://npm.pkg.github.com/'
          scope: '@miyamo2'

      - name: Install dependencies
        run: bun install

      - name: Bun Publish
        run: bun publish
        env:
          BUN_AUTH_TOKEN: $

.npmrc

npm publishではpackage.jsonのpublishConfigにスコープとレジストリURLの設定、
.npmrcにトークンの設定という形で分けて記述していても問題なく公開できていたのですが
bun publishでは認証エラーが起きてしまいました

Copy
Run bun publish
  bun publish
  shell: /usr/bin/bash -e {0}
  env:
    GITHUB_TOKEN: ***
bun publish v1.2.1 (ce532901)
$ bun build ./src/index.ts ./src/graphqlgen.ts ./src/gatsby-node.ts --outdir ./ --target node

  index.js          0.00 KB

  gatsby-node.js    2052.43 KB

  graphqlgen.js     535.73 KB

[67ms] bundle 399 modules

error: missing authentication (run `bunx npm login`)
packed 1.0KB package.json
packed 130B .gitmodules
packed 1.1KB LICENSE
packed 83B README.md
packed 2.1MB gatsby-node.js
packed 0.54MB graphqlgen.js
packed 0KB index.js

Total files: 7
Shasum: b2be19a15573eaea021b2bcb20910d848f4acc8c
Integrity: sha512-YCID7BXIoBGkQ[...]iJNeCHYquOvaw==
Unpacked size: 2.59MB
Packed size: 0.50MB
Error: Process complete

パッと見だと bun が publishConfig.registry、 publishConfig.@scope:registry をサポートしていなさそうです
公開先のレジストリをnpmだと解釈されたっぽい

スコープ、レジストリURLの設定を追記

Copy
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
+ @miyamo2:registry=https://npm.pkg.github.com

package.json

変更なし
publishConfig.@scope:registry はもう使っていないので消してもよかったかも

Copy
{
  "name": "@miyamo2/gatsby-source-blogapi-miyamo-today",
  "version": "0.1.6",
  "repository": {
    "type": "git",
    "url": "https://github.com/miyamo2/gatsby-source-blogapi-miyamo-today.git"
  },
  "publishConfig": {
    "access": "restricted",
    "@miyamo2:registry": "https://npm.pkg.github.com"
  },
  "main": "index.js",
  "type": "module",
  "scripts": {
    "prepublishOnly": "bun build ./src/index.ts ./src/graphqlgen.ts ./src/gatsby-node.ts --outdir ./ --target node",
    "build": "bun build ./src/index.ts ./src/graphqlgen.ts ./src/gatsby-node.ts --outdir ./ --target node",
    "graphqlgen": "cd ./src && bunx graphql-codegen generate"
  },
  "devDependencies": {
    "@types/bun": "latest"
  },
  "peerDependencies": {
    "typescript": "^5.7.2"
  },
  "dependencies": {
    "@formkit/tempo": "^0.1.2",
    "@graphql-codegen/cli": "^5.0.3",
    "@graphql-codegen/typescript-graphql-request": "^6.2.0",
    "gatsby": "^5.14.1",
    "gatsby-source-filesystem": "^5.14.0",
    "graphql": "16.9.0"
  }
}

実行結果

無事パスしました

Copy
bun publish v1.2.1 (ce532901)
$ bun build ./src/index.ts ./src/graphqlgen.ts ./src/gatsby-node.ts --outdir ./ --target node

  index.js          0.00 KB

  gatsby-node.js    2052.43 KB

  graphqlgen.js     535.73 KB

[61ms] bundle 399 modules

packed 1.0KB package.json
packed 130B .gitmodules
packed 1.1KB LICENSE
packed 83B README.md
packed 2.1MB gatsby-node.js
packed 0.54MB graphqlgen.js
packed 0KB index.js

Total files: 7
Shasum: b2be19a15573eaea021b2bcb20910d848f4acc8c
Integrity: sha512-YCID7BXIoBGkQ[...]iJNeCHYquOvaw==
Unpacked size: 2.59MB
Packed size: 0.50MB
Tag: latest
Access: restricted
Registry: https://npm.pkg.github.com

 + @miyamo2/gatsby-source-blogapi-miyamo-today@0.1.7

参考


Recommend Articles

Gatsby.jsとgqlgenでブログを作った

はじめに 最初は単にGraphQL、gRPC、ECS、New Relic etc 自分が興味のある技術トピックでHello World Enterprise Editionをやるだけのつもりだったものの GraphQL -> ヘッドレスCMS -> ブログ という連想ゲームの結…

2024/12/23

ArticleImage:01JFT54WPPZ0ET3XTKZ0JTCMRE

S3 + CloudFrontで実現する独自ドメインGoパッケージ

はじめに go.uber.org/mock google.golang.org/grpc gorm.io/gorm 独自ドメインで配布されているGoパッケージはカッコいい 、 のような のPrefixがない分、importディレクティブがスッキリして見える 今回は当ブログのバ…

2025/01/26

ArticleImage:01JJHA5TFHVR4MP9ZWJSDD713A

Copyright © miyamo2 All rights reserved.