【发布时间】:2021-01-15 08:57:26
【问题描述】:
当我尝试从 GitHub 操作安装我的 npm 模块时,我收到以下错误:
npm ERR! 401 Unauthorized - GET https://npm.pkg.github.com/@xxxx%2fxxxx-analytics - Your request could not be authenticated by the GitHub Packages service. Please ensure your access token is valid and has the appropriate scopes configured.
在您发表评论之前,我已经使用范围和访问令牌正确配置了 .npmrc,并且在本地安装私有包时一切正常。
这是我的 GitHub 工作流操作:
name: JavaScript workflow
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: '12.x'
- name: npmrc
run: cat .npmrc
- name: npm install
run: |
npm install
env:
CI: true
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
这是我的 .npmrc
@fortawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:_authToken=XXXXXXXXX
@colonynetworks:registry=https://npm.pkg.github.com
//npm.pkg.github.com:_authToken=XXXXXXXXX
always-auth=true
@react-admin:registry=https://registry.marmelab.com
//registry.marmelab.com:
_auth=XXXXXXXXX
email=software@XXXXXXXXX.com
always-auth=true
这是一个私人仓库,authTokens 目前被硬编码在 .npmrc 文件中。
然而,在尝试解决这个问题时,我确实看到了来自 Github 工作人员的随机评论:https://github.community/t/netlify-getting-401-from-github-package-registry-with-auth-token/16415/3
有点含糊,但听起来它不接受.npmrc文件中的硬编码authToken。
所以我尝试的第一件事是像这样使用我们的环境变量:
@xxxx=https://npm.pkg.github.com
//npm.pkg.github.com:_authToken=${NPM_AUTH_TOKEN}
env 变量在我们的 Github 存储库机密中是正确的,并且由工作流提供。
但是这仍然导致同样的 401 Unauthorized 错误。
通过查看其他解决方案,然后我尝试在 install 步骤之前在 Github 操作中手动生成 .npmrc,如下所示:
- name: npmrcgen
run: |
echo "//npm.pkg.github.com/:_authToken=XXXXXXX" > .npmrc
echo "@xxxxx=https://npm.pkg.github.com/" >> .npmrc
echo "@react-admin:registry=https://registry.marmelab.com" >> .npmrc
echo "//registry.marmelab.com:" >> .npmrc
echo "_auth=XXXXXXX" >> .npmrc
echo "email=software@xxxxx.com" >> .npmrc
echo "always-auth=true" >> .npmrc
在我添加的日志记录步骤中,_authToken(仅适用于 Github)仍然显示为 ***,我仍然收到 401 Unauthorized 错误。
此时我想确认 .npmrc 甚至被使用,所以我删除了我们用于 marmelab.com 的第二个私有注册表,果然,我收到一个错误,说它不再能够安装他们的 @ 987654335@ 包。这证明 .npmrc 文件确实被我的 Github 操作读取和使用,但它不接受我的 Github 个人访问令牌。
我也尝试生成一个新令牌。它可以完全访问repo: 以及write:packages 和read:packages 下的所有内容,这应该是必需的。
在 Github 操作中仍然 401 Unauthorized,并且在本地仍然可以正常工作。
最后我尝试使用yarn 而不是npm 安装它。不出所料,这也没有解决它。
我已经看到并尝试了以下解决方案,但没有成功:
- Download private module from Github Package Registry via Yarn within a Github Action? Publishing works, but installing is met with '401 Unauthorized'
- https://github.com/FerLuisxd/create-npmrc
- https://blog.bitsrc.io/install-npm-private-packages-in-ci-cd-with-github-actions-746db95017cc
我没有尝试过的一件事,因为我没有看到关于如何或这是一个好主意的建议,但我还没有在 Github 操作中完成npm login。由于没有其他人这样做过,并且以某种方式使其正常工作,我认为这没有必要。
【问题讨论】:
-
无解决方案,但类似(相同)问题:stackoverflow.com/questions/60346132/…
-
@riQQ 如果您找到解决方案,请告诉我。我在这个问题上提交了赏金,但仍然没有。还向 Github 发送了支持票,但仍在等待回复。
-
GitHub 回复了我的支持请求,说我的设置看起来正确,并请求访问我发布包的私有仓库:( 尚无解决方案,但他们说它可能与发布的包而不是安装步骤。
-
您可以尝试将您的 .npmrc 文件写入您的用户主目录,即 ~/.npmrc 吗?
-
如何使用 github 凭据,例如用户名(不是电子邮件)和密码?
标签: github npm yarnpkg github-actions github-package-registry