首页 > 解决方案 > 如何在 github 操作工作流 ci 中通过 npm 安装私有 github 存储库

问题描述

我正在尝试通过运行在 github 工作流 ci 中安装 npm 依赖项npm install。但是我收到以下错误:

npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git@github.com/private-org/private-repo.git
npm ERR! 
npm ERR! Warning: Permanently added the RSA host key for IP address 'removed' to the list of known hosts.
npm ERR! git@github.com: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.

ci.yml

name: CI

on:
  push:
    branches: [master ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js
      uses: actions/setup-node@v1
      with:
        node-version: '12.x'
    - run: node --version
    - run: npm install

包.json

  ...
  "dependencies": {
    "some-pacakage": "git+ssh://git@github.com/private-org/private-repo.gitt",
  },
  ...

some-package是由 npm 通过 github 安装的。存储库与运行工作流的组织位于同一组织内。要在本地解决此问题,您可以在与该组织相关联的 github 帐户上设置 ssh 密钥。

但是我该如何解决这个问题,以便它能够在我不使用我的个人 github 帐户的工作流 ci 中通过 github repo 安装该包。

标签: node.jsgitgithubgithub-actions

解决方案


私有存储库正在通过 ssh 安装。如果您在管道中设置了 ssh 密钥,它将在尝试安装时使用该 ssh 密钥。

幸运的是,有一个 github 操作允许我们这样做https://github.com/webfactory/ssh-agent

在 npm install 上方添加以下内容:

  - uses: webfactory/ssh-agent@v0.2.0
  with:
    ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} 

设置/先决条件

https://github.com/webfactory/ssh-agent#usage

  1. 创建具有足够访问权限的 SSH 密钥。出于安全原因,请勿使用您的个人 SSH 密钥,而是设置一个专用的用于 GitHub Actions 的密钥。如果您不确定此步骤,请参阅下面的一些提示。

  2. 确保您没有在私钥上设置密码。

  3. 在您的存储库中,转到“设置”>“机密”菜单并创建一个新机密。在本例中,我们将其命名为 SSH_PRIVATE_KEY。将私有 SSH 密钥文件的内容放入内容字段。此密钥应以 -----BEGIN ... PRIVATE KEY----- 开头,由多行组成并以 -----END ... PRIVATE KEY----- 结尾。


推荐阅读