使用 GitHub 操作从私有 GitHub 存储库安装 npm 模块

Gol*_*den 16 github node.js npm github-actions

我正在尝试使用 GitHub 操作为 Node.js 项目运行构建。作为 的一部分npm install,我需要直接从私有 GitHub 存储库(而不是来自 GPR!)安装 npm 模块。

package.json我有:

"dependencies": {
  ...
  "my-module": "github:<org>/<my-module>#master",
  ...
},
Run Code Online (Sandbox Code Playgroud)

但是,在运行时npm install,我得到:

npm 错误!git@github.com:权限被拒绝(公钥)。npm 错误!致命:无法从远程存储库读取。

存储库是我自己组织的一部分,它在本地(即从我的机器)工作。我怎样才能让这个运行?

我已经尝试设置NODE_AUTH_TOKEN环境变量,但没有任何区别。尽管您经常发现此建议,但它似乎只针对 GPR。我想避免的是必须将令牌硬编码到package.json文件中。对此有何想法?

pet*_*ans 18

这就是我设法从私有 GitHub 存储库安装依赖项的方式。

package.json 中的依赖可以添加如下。该github:前缀是可选的。指定#branchor#tag也是可选的。

    "dependencies": {
        ...
        "myrepo": "username/myrepo#master",
        "myotherrepo": "github:username/myotherrepo"
    },
Run Code Online (Sandbox Code Playgroud)

这是一个示例工作流程。PATrepo范围内的个人访问令牌。禁用持久凭据很重要actions/checkout,否则它们将覆盖您的PAT. 请注意,git config更改会在步骤之间持续存在,因此您只需为每个作业运行一次。

      - uses: actions/checkout@v2
        with:
          persist-credentials: false
      - uses: actions/setup-node@v1
        with:
          node-version: 12.x
      - run: git config --global url."https://${{ secrets.PAT }}@github.com/".insteadOf ssh://git@github.com/
      - run: npm ci
      ...
Run Code Online (Sandbox Code Playgroud)

  • 工作起来就像一个魅力,这在项目有模块但源是 github 私人存储库而不是 NPM 的情况下非常有用 (2认同)

Dmi*_*riy 9

一个非常简单的解决方案,只需要对存储库进行只读访问。使用 GitHub 的部署密钥和以下操作https://github.com/webfactory/ssh-agent

\n

package.json1.按以下格式提供私有存储库:

\n
"dependencies": {\n    "repo": "git+ssh://git@github.com:user/private_repo.git",\n}\n
Run Code Online (Sandbox Code Playgroud)\n

2. 在您的计算机上本地生成部署密钥。使用空密码

\n
ssh-keygen -f my_key\n
Run Code Online (Sandbox Code Playgroud)\n

3. 通过 GitHub UI 创建部署密钥(首选只读)。使用内容my_key.pub

\n

部署密钥

\n

4. 通过 GitHub UI 创建一个REPO_SSH_KEY以目标存储库命名的 GitHub Actions 密钥 - 将运行 GitHub Actions 的密钥。使用my_key\xe2\x80\x93 的内容它是私钥

\n

SSH 秘密

\n

5. 更新 GitHub Actions 工作流程文件,如下所示

\n
# ...\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - name: Add SSH key to chekout a private repo\n        uses: webfactory/ssh-agent@v0.5.4\n        with:\n          ssh-private-key: ${{ secrets.REPO_SSH_KEY }}\n\n      - name: Checkout code\n        uses: actions/checkout@v2\n      \n      # ...\n      # the rest of the steps, including npm install\n      # that will successfully access the private repo\n
Run Code Online (Sandbox Code Playgroud)\n


小智 -3

您应该编辑您的.npmrc文件。您还可以使用npm config

npm config set @myco:registry http://reg.example.com

请参阅以下线程以获取更多信息: Is there any way to configure multiple registries in a single npmrc file