在 Github Actions 上使用 yarn 安装私有 Github 包是 yarn.lock 未经授权的



已经有很多类似的问题浮出水面:

  • 在 Github Actions 上从 package.json 安装私有 github 包
  • 通过 Github Action 中的 Yarn 从 Github 包注册表下载私有模块?发布作品,但安装遇到"401 未经授权">
  • 使用 Yarn 从 Github 包注册表安装私有包失败,且未经授权

但是,我们的问题似乎有所不同,因为:

  • yarn install在本地计算机上运行良好
  • 问题仅在使用 Github 操作时
  • 如果我们删除yarn.lockyarn installGH 操作成功

以前有人遇到过这种情况吗?特别是它不适用于yarn.lock文件?

如果重要,以下是设置:

build.yml

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '10.x'
registry-url: 'https://npm.pkg.github.com'
- name: Install
run: yarn install
env:
# GITHUB_TOKEN can't access packages hosted in private repos,
# even within the same organisation
NODE_AUTH_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- name: Build
run: yarn build
- name: Test
run: yarn test --forbid-only

我们还有一个用于本地安装的.npmrc文件:

@<org>:registry=https://npm.pkg.github.com

但没有.yarnrc文件。

我正在创建一个文件.npmrc和.yarnrc。 类型:

name: Test
on: push
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v2
- name: Node ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Create NPMRC
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.PACKAGES_TOKEN }}" >> ~/.npmrc
echo "@you-scope:registry=https://npm.pkg.github.com" >> ~/.npmrc
echo 'registry "https://registry.yarnpkg.com"' >> ~/.yarnrc
- run: yarn install

小写形式替换 github 用户或 github 中组织的@you范围。

为此存储库创建 github 访问权限的PACKAGES_TOKEN秘密令牌。

我们通过在build.yml配置中显式复制.npmrc配置来解决此问题:

- uses: actions/setup-node@v1
with:
node-version: '10.x'
registry-url: 'https://npm.pkg.github.com'
# These following two lines are the key:
always-auth: true
scope: '@reedsy'

最新更新