首页 > 解决方案 > 从 GH 包安装 NPM:在本地工作,但不能在 GH 操作中使用

问题描述

我已经向我们组织的包存储库发布了一个私有包。

根据文档,我将 .npmrc 文件添加到另一个项目的根目录,内容如下:

registry=https://npm.pkg.github.com/my-org

在我的本地 ~/.npmrc 中,我的 Authtoken 为 npm.pkg.github.com

我在 package.json 中安装了已发布的包,在 package.json 中有以下条目

"@my-org/my-package": "^1.0.0",

在本地安装时,这工作正常。

通过 Github Actions 安装时,安装失败并出现以下错误:

npm ERR! code E404
npm ERR! 404 Not Found - GET https://npm.pkg.github.com/my-org/@my-org%2fmy-package - npm package "my-package" does not exist under owner "my-org"
npm ERR! 404 
npm ERR! 404  '@my-org/my-package@1.0.0' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)

似乎在包的路径中使用了两次“my-org”?为什么它在本地有效,但在 Github Actions 中无效?

最初我的 .npmrc 只是这样(没有 org 添加到注册表 URL):

registry=https://npm.pkg.github.com

这在本地也可以正常工作。

它也适用于 github Action 中的所有作用域包(如 @coogle-cloud),但不适用于非作用域包:

npm ERR! code E404
npm ERR! 404 Not Found - GET https://npm.pkg.github.com/chai
npm ERR! 404 
npm ERR! 404  'chai@4.2.0' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

更新:所以,我拿了我的个人访问令牌(在本地工作)并提交,现在安装工作流程成功了。

文档(https://help.github.com/en/github/managing-packages-with-github-packages/using-github-packages-with-github-actions)说:“GITHUB_TOKEN 已阅读:packages 和 write :包范围。” 这还不够吗? 

我的个人访问令牌有 delete:packages、read:packages、repo、write:packages。

标签: githubgithub-actions

解决方案


隐藏在一些任意 twitter 线程中的是 GITHUB_TOKEN 只能访问当前 repo 的信息。 https://twitter.com/char_fish/status/1191442780729556993?s=21

解决方案是显式定义个人(原文如此!)访问令牌,并使用此密钥将其放置在存储库的机密存储中,以针对包存储库进行身份验证。

  - name: 'authenticate with GH package registry'
    run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc

推荐阅读