タイトルでうまいこと表現できませんでしたがつまりこういう状況です。
状況 #
GitHub Packages(や npmjs)に作った組織内のプライベートなパッケージを利用しているプロジェクトがあるとして、利用する際は .npmrc でパッケージの在処とアクセストークンを指定している。
.npmrc の例
@example-team:registry=https://npm.pkg.github.com/example-repo
//npm.pkg.github.com/:_authToken=ghp_abcdefghijklmnopqrstuvwxyz0123456789
.npmrc にはアクセストークンを含んでいるため、Git 管理対象からは外している。
また、GitHub Actions を利用し、リントやテストが動くようにしている。
しかし GitHub Actions のワークフローで npm install が動いたときには .npmrc が存在しないため、うまくいかなくなってしまう。
対応 #
色々方法はあると思いますが、ワークフローの中で .npmrc を作ってしまうのが1つの手です。
.github/workflows/job.yml
jobs:
ESLint:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
cache: "npm"
- run: echo "@example-team:registry=https://npm.pkg.github.com/example-repo" >> .npmrc
- run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.ACCESS_TOKEN }}" >> .npmrc
- run: npm ci
# 以降処理が続く
echo しているところがポイントのところです。
echo "@example-team:registry=https://npm.pkg.github.com/example-repo" >> .npmrc
echo "//npm.pkg.github.com/:_authToken=${{ secrets.ACCESS_TOKEN }}" >> .npmrc
npm ci (or npm install)する前に上記で .npmrc を作成します。
アクセスキーはベタ書きしない方がよいため、シークレットを設定しておき、それを呼び出すようにします。以下の部分です。
${{ secrets.ACCESS_TOKEN }}
以上 #
現場からは以上です。