0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub Packagesでnpmパッケージを扱うための設定方法

Posted at

GitHub Packagesで公開されているnpmパッケージをインストールする方法

GitHub Packagesで公開されているnpmパッケージをインストールするにはGitHubへ認証する必要があります。GitHubへ認証するにはpersonal access token(classic)が必要です。また、personal access token(classic)にはread:packagesスコープが設定されている必要があります。personal access token(classic)がない場合は発行してください。New personal access token(classic)で発行できます。次に

~/.npmrc
//npm.pkg.github.com/:_authToken=TOKEN

のようなファイルを作成してください。TOKENをread:packagesスコープが設定されているトークンに置き換えてください。次にリポジトリのルートのpackage.jsonと同じディレクトリに

.npmrc
@NAMESPACE:registry=https://npm.pkg.github.com

を作成してください。@NAMESPACEはインストールしたいパッケージのスコープに置き換えてください。例えば、@tommy-ish/fooというパッケージをインストールしたい場合は@tommy-ish:registry=http://npm.pkg.github.comにしてください。後は通常通りインストールできます。例えば、@tommy-ish/fooというパッケージをインストールしたい場合は

$ npm install @tommy-ish/foo

を実行します。

GitHub ActionsからGitHub Packagesにアクセスする方法

GitHub ActionsからGitHub Packagesにアクセスするにはactions/setup-nodeのregistry-urlとNODE_AUTH_TOKENという環境変数を使うと簡単です。具体的には

steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with:
      node-version: 22.x
      registry-url: https://npm.pkg.github.com
  - run: npm ci
    env:
      NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

のように書くとGitHub Packagesからパッケージをインストールできます。また

steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with:
      node-version: 22.x
      registry-url: https://npm.pkg.github.com
  - run: npm ci
  - run: npm publish
    env:
      NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

のように書くとGitHub Packagesでパッケージを公開できます。

DependabotがGitHub Packagesで公開されているパッケージをアップデートできるようにする方法

Dependabotの設定ファイルを

.github/dependabot.yml
version: 2
registries:
  npm-github:
    type: npm-registry
    url: https://npm.pkg.github.com
    token: ${{secrets.MY_ARTIFACTORY_PASSWORD}}
    replaces-base: true
updates:
  - package-ecosystem: npm
    registries:
      - npm-github

のように設定してください。MY_ARTIFACTORY_PASSWORDはDependabotシークレットの名前に置き換えてください。そのDependabotシークレットはread:packagesスコープが設定されているトークンが設定されている必要があります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?