无法从 travis 发布到 github 包注册表:401 未经授权尽管包含 github 个人访问令牌

Cannot publish to github package registry from travis: 401 unauthorized despite including github personal access token

我正在尝试从 Travis CI 将一个包发布到 github 包注册表。一切似乎都运行良好(构建和测试成功),但当它尝试发布时,我收到 401 错误。我已经在 travis 配置中添加了我的 github 个人访问令牌作为安全环境变量,并且我正在使用包含 github 注册表和访问令牌,但我仍然遇到权限问题。我应该如何授予 lerna/npm 发布 github 包的权限?

这是我的 .travis.yml

env:
  global:
    secure: "my encrypted GH_TOKEN value (github personal access token with package registry permissions)"
language: node_js
node_js:
  - "12"
cache:
  directories:
    - "node_modules"
before_install:
  - echo "@my-org:registry=https://npm.pkg.github.com/:_authToken=${GH_TOKEN}" > .npmrc
install:
  - npm ci
  - npm run bootstrap
script:
  - npm run test
  - npm run build
deploy:
  provider: script
  script: "npm run publish"
  skip_cleanup: true
  on:
    node: "12"
    tags: true

当 travis deploy 运行时,它会报错:

> lerna publish from-git --yes --npm-tag beta

WARN deprecated --npm-tag has been renamed --dist-tag

lerna notice cli v3.20.2

lerna info ci enabled

Found 1 package to publish:

 - @my-org/example-pkg => 1.0.2-beta.8

lerna info auto-confirmed 

lerna info publish Publishing packages to npm...

lerna notice Skipping all user and access validation due to third-party registry

lerna notice Make sure you're authenticated properly ¯\_(ツ)_/¯

lerna WARN ENOLICENSE Package @my-org/example-pkg is missing a license.

lerna WARN ENOLICENSE One way to fix this is to add a LICENSE.md file to the root of this repository.

lerna WARN ENOLICENSE See https://choosealicense.com for additional guidance.

lerna WARN lifecycle Skipping root "prepublish" because it has already been called

lerna http fetch PUT 401 https://npm.pkg.github.com/:_authToken=[secure]/@my-org%2fexample-pkg 153ms

lerna ERR! E401 Unable to authenticate, need: Basic realm="GitHub Package Registry"

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! ts-utilities@1.0.0 publish: `lerna publish from-git --yes --npm-tag beta`

npm ERR! Exit status 1

npm ERR! 

npm ERR! Failed at the ts-utilities@1.0.0 publish script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

npm ERR!     /home/travis/.npm/_logs/2020-02-25T19_46_08_862Z-debug.log

Script failed with status 1

failed to deploy

我做错了什么?

问题最终是我的 .npmrc 文件中需要两行。一种是将 org @my-org 与 github 包注册表相关联:另一种是为 github 包注册表提供身份验证令牌。所以整个 .npmrc 看起来像这样:

@my-org:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}

然后我能够将 GITHUB_TOKEN 环境变量设置为我的 github 个人访问令牌(具有包读取权限)然后我能够 运行 npm install @my-org/example-pkg.