当我试图从一个npm操作安装我的GitHub模块时,我会得到以下错误:
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文件中硬编码的.npmrc。
因此,我首先尝试使用env变量,而不是这样:
@xxxx=https://npm.pkg.github.com
//npm.pkg.github.com:_authToken=${NPM_AUTH_TOKEN}env变量在我们的Github回购秘密中是正确的,并由工作流提供。
然而,这仍然导致了相同的401未经授权的错误。
然后,通过查看其他解决方案,我尝试在.npmrc步骤之前在install操作中手动生成install,如下所示:
- 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未经授权的错误。
在这一点上,我想确认.npmrc甚至被使用,所以我删除了我们为marmelab.com使用的第二个私有注册表,果然,我收到了一个错误,说它不能再安装他们的ra-realtime包了。这证明.npmrc文件确实正在被我的Github操作读取和使用,但它不接受我的Github个人访问令牌。
我也尝试过产生一个新的标记。它可以完全访问repo:下的所有东西,以及write:packages和read:packages,这是应该需要的。
仍然401未经授权的行动,并仍然在当地工作良好。
最后,我尝试用yarn而不是npm来安装它。毫不奇怪,这也没有解决这个问题。
我见过并尝试过以下解决办法,但没有取得任何成功:
有一件事我没有尝试过,因为我没有看到任何关于如何或这是一个好主意的建议,但我还没有在Github行动中做过一个npm login。由于没有其他人这样做,而且不知何故使它发挥作用,我认为这是不必要的。
发布于 2020-10-13 16:39:12
我联系了GitHub支持部门,他们设法找出了问题所在。
Github工作流比本地环境更严格,并且在auth令牌之前需要额外的/:
找出差异:
//npm.pkg.github.com:_authToken=XXXXXXXXX. # broken
//npm.pkg.github.com/:_authToken=XXXXXXXXX # works在/为我解决问题之前,添加额外的:_authToken=。
发布于 2021-05-21 19:36:30
在项目的根目录中有一个.npmrc文件。
.npmrc的内容:
registry=https://registry.npmjs.org/
@{scope}:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=********** (Token generated from github)@{scope}是您的组织名称或用户名。这是区分大小写的。
发布于 2022-09-23 07:14:22
只需使用操作/设置-节点动作即可。
- uses: actions/setup-node@v3
with:
node-version: 16
cache: "yarn"
registry-url: "https://npm.pkg.github.com"
- name: Build
env:
# also other environment variable
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
yarn
yarn buildhttps://stackoverflow.com/questions/64124831
复制相似问题