6

对于我的项目,我正在尝试从 GitHub Packages 安装(而不是发布!)一个 npm 包。它托管在一个私有存储库中,该存储库位于一个组织中。我已经制作了一个个人访问令牌,具有从该 repo 和 org 读取所需的权限。所有这些都通过以下步骤在本地工作:

  • .npmrc文件中设置注册表:registry=https://npm.pkg.github.com/OWNER
  • 终端登录:npm login --registry=https://npm.pkg.github.com 然后会提示:
> Username: USERNAME
> Password: TOKEN
> Email: PUBLIC-EMAIL-ADDRESS
  • Username是你的标准 github 用户名
  • Token是使用正确权限创建的个人访问令牌
  • Email可以是随机的

填写完毕后,一切正常,我可以安装包,以及托管在正常 npm 注册表上的所有其他包。现在解决问题:我试图在 circleCI 上复制相同的内容,但它似乎不允许我覆盖 npm 注册表。该项目包含一个.npmrc文件,位于该文件夹中package.json。以下是圈子 ci conf 的一部分:

      - run:
          name: "Set NPM registry"
          command: npm config set registry https://npm.pkg.github.com/OWNER
      - run:
          name: "Authenticate with GitHub package registry"
          command: echo "//npm.pkg.github.com/:_authToken=${GITHUB_PACKAGES}" > web_ui/frontend/.npmrc
      - run:
          name: "Install frontend dependencies"
          command: npm run deps-frontend

GITHUB_PACKAGES只是一个存储在 circleCI 中的环境变量。

现在错误消息告诉我以下内容:

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

我尝试用谷歌搜索它,但没有任何效果。

4

2 回答 2

4

你可以尝试在run步骤中移动pre步骤吗?像这样:

pre:
  - npm config set registry https://npm.pkg.github.com/OWNER
  - echo "//npm.pkg.github.com/:_authToken=${GITHUB_PACKAGES}" > web_ui/frontend/.npmrc
  - npm run deps-frontend

另一件要尝试的事情可能是使用不同版本的npm.

于 2019-12-06T09:32:42.243 回答
2

创建用于设置.npmrc值的命令,请参见 orb source npm-config/set-registry

使用示例:

 steps:
  - checkout
  - npm-config/set-registry:
      registry-prurl: //your.registry.domain.net/path/
      scope: '@your-scope'
      auth-token: your-repo-token
  - run:
      name: Download depencies
      command: yarn

来源:

set-registry:
description: Sets a registry to .npmrc
parameters:
  registry-prurl:
    description: |
      protocol-relative URL (ex: //registry.domain.net/path/)
    type: string
  scope:
    description: registry scope
    type: string
  auth-token:
    description: authorization token for private repos
    type: string
    default: ""
steps:
  - run:
      name: Set NPM registry URL
      command: >
        scope=<< parameters.scope >>;
        registry_prurl=<< parameters.registry-prurl >>;
        npm config set "${scope}:registry" "https:$registry_prurl"
  - run:
      name: Set auth token for NPM registry if provided
      command: |
        if [ "<< parameters.auth-token >>" != "" ]; then
        registry_prurl=<< parameters.registry-prurl >>;
        auth_token=<< parameters.auth-token >>;
        echo "${registry_prurl}:_authToken=${auth_token}" >> ~/.npmrc
        fi;
于 2021-06-08T07:52:20.083 回答